Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def tearDown(self):
fstats = yappi.get_func_stats()
if not fstats._debug_check_sanity():
sys.stdout.write("ERR: Duplicates found in Func stats\r\n")
fstats.debug_print()
for fstat in fstats:
if not fstat.children._debug_check_sanity():
sys.stdout.write("ERR: Duplicates found in ChildFunc stats\r\n")
fstat.children.print_all()
tstats = yappi.get_func_stats()
if not tstats._debug_check_sanity():
sys.stdout.write("ERR: Duplicates found in Thread stats\r\n")
tstats.print_all()
yappi.start(profile_threads=False)
a() # context-id:1
self.callback_count = 2
a() # context-id:2
stats = yappi.get_func_stats()
fsa = utils.find_stat_by_name(stats, "a")
self.assertEqual(fsa.ncall, 1)
yappi.stop()
yappi.clear_stats()
self.callback_count = 1
yappi.start() # profile_threads=True
a() # context-id:1
self.callback_count = 2
a() # context-id:2
stats = yappi.get_func_stats()
fsa = utils.find_stat_by_name(stats, "a")
self.assertEqual(fsa.ncall, 2)
_yappi._set_test_timings(timings)
def a():
pass
def b():
pass
yappi.start()
t = threading.Thread(target=a)
t.start()
t.join()
t = threading.Thread(target=b)
t.start()
t.join()
yappi.get_func_stats().save("tests/ystats1.ys")
yappi.clear_stats()
_yappi._set_test_timings(timings)
self.assertEqual(len(yappi.get_func_stats()), 0)
self.assertEqual(len(yappi.get_thread_stats()), 1)
t = threading.Thread(target=a)
t.start()
t.join()
self.assertEqual(_yappi._get_start_flags()["profile_builtins"], 0)
self.assertEqual(_yappi._get_start_flags()["profile_multithread"], 1)
yappi.get_func_stats().save("tests/ystats2.ys")
stats = yappi.YFuncStats([
"tests/ystats1.ys",
"tests/ystats2.ys",
])
def b():
pass
def c():
pass
def a():
b()
c()
yappi.start()
a()
b() # non-child call
c() # non-child call
stats = yappi.get_func_stats()
fsa = utils.find_stat_by_name(stats, 'a')
childs_of_a = fsa.children.get().sort("tavg", "desc")
prev_item = None
for item in childs_of_a:
if prev_item:
self.assertTrue(prev_item.tavg > item.tavg)
prev_item = item
childs_of_a.sort("name", "desc")
prev_item = None
for item in childs_of_a:
if prev_item:
self.assertTrue(prev_item.name > item.name)
prev_item = item
childs_of_a.clear()
self.assertTrue(childs_of_a.empty())
recentPath = PreferencesManager().get('shell', 'recent sorted profile stats')
if recentPath is None:
defaultPath = os.path.join(os.path.expanduser('~'), filename)
else:
defaultPath = os.path.join(os.path.split(recentPath)[0], filename)
stats_path, _filter = QFileDialog.getSaveFileName(
self, "Export sorted stats text", defaultPath, "Text files (*.txt)",
options=QFileDialog.Options(QFileDialog.DontUseNativeDialog))
if stats_path:
pstats_path = os.path.splitext(stats_path)[0] + '.pstats'
PreferencesManager().set('shell', 'recent sorted profile stats', stats_path)
# Export the yappi stats to builtin pstats format,
# since pstats provides nicer printing IMHO
stats = yappi.get_func_stats()
stats.save(pstats_path, type='pstat')
with open(stats_path, 'w') as f:
import pstats
ps = pstats.Stats(pstats_path, stream=f)
ps.sort_stats(sortby)
ps.print_stats()
logger.info("Printed stats to file: {}".format(stats_path))
# As a convenience, go ahead and open it.
QDesktopServices.openUrl(QUrl.fromLocalFile(stats_path))
def stop_profiler(self):
"""
Stop yappi and write the stats to the output directory.
Return the path of the yappi statistics file.
"""
if not self.profiler_running:
raise RuntimeError("Profiler is not running")
if not HAS_YAPPI:
raise RuntimeError("Yappi cannot be found. Plase install the yappi library using your preferred package "
"manager and restart Tribler afterwards.")
yappi.stop()
yappi_stats = yappi.get_func_stats()
yappi_stats.sort("tsub")
log_dir = os.path.join(self.session.config.get_state_dir(), 'logs')
file_path = os.path.join(log_dir, 'yappi_%s.stats' % self.profiler_start_time)
# Make the log directory if it does not exist
if not os.path.exists(log_dir):
os.makedirs(log_dir)
yappi_stats.save(file_path, type='callgrind')
yappi.clear_stats()
self.profiler_running = False
return file_path
return 1
except KeyboardInterrupt:
self.print("Ctrl+C")
return 3
except AssertionError as e:
if e.args[0]:
self.print("ERROR: %s" % e.args[0])
else:
self.print("Assertion error")
return 4
except Exception:
error_report()
return 2
finally:
if enable_profiling:
i = yappi.get_func_stats()
i.print_all(
out=self.stdout,
columns={
0: ("name", 80),
1: ("ncall", 10),
2: ("tsub", 8),
3: ("ttot", 8),
4: ("tavg", 8)
}
)
if show_metrics:
from noc.core.perf import apply_metrics
d = apply_metrics({})
self.print("Internal metrics:")
for k in d:
self.print("%40s : %s" % (k, d[k]))
def run_tribler_with_yappi(run_function=None):
t1 = time()
# Start yappi
yappi.start()
# Do we have a custom run function?
if run_function:
run_function()
else: # Default to the normal run function
run()
# Stop yappi and get the results
yappi.stop()
logger.info("YAPPI: %s tribler has run for %s seconds", yappi.get_clock_type(), time() - t1)
yappi_stats = yappi.get_func_stats()
yappi_stats.sort("tsub")
# If a yappi output dir is specified, save the output in callgrind format.
if "YAPPI_OUTPUT_DIR" in os.environ:
output_dir = os.environ["YAPPI_OUTPUT_DIR"]
fname = os.path.join(output_dir, 'yappi.callgrind')
yappi_stats.save(fname, type='callgrind')
# Log the 50 most time consuming functions.
count = 0
for func_stat in yappi_stats:
logger.info("YAPPI: %10dx %10.3fs %s", func_stat.ncall, func_stat.tsub, func_stat.name)
count += 1
if count >= 50:
break
Shutdown is deferred to another thread to ensure the RPC reply
is sent before the poll loop is killed.
:param req: `EmptyMessage`
:param handle: `PendingRequest`
'''
if FLAGS.profile_worker:
util.log_info('Working shutting down... writing profile.', self.id, FLAGS.profile_worker)
if FLAGS.profile_worker:
try:
os.system('mkdir -p ./_worker_profiles/')
import yappi
yappi.get_func_stats().save('_worker_profiles/%d' % self.id, type='pstat')
except Exception, ex:
print 'Failed to write profile.', ex
handle.done()
threading.Thread(target=self._shutdown).start()
def stop_profiling():
yappi.stop()
yappi.get_func_stats().print_all()
yappi.get_thread_stats().print_all()
has_argv = hasattr(sys, "argv")
if not has_argv:
sys.argv = [""]
profile_path = os.path.join(local.HOME, "yappi_{}.callgrind.out".format(time.time()))
yappi.get_func_stats().save(profile_path, "callgrind")
yappi.clear_stats()