def test_abcd(self): _timings = {"a_1":6,"b_1":5,"c_1":3, "d_1":1} _yappi._set_test_timings(_timings) def a(): b() def b(): c() def c(): d() def d(): pass stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') fsc = utils.find_stat_by_name(stats, 'c') fsd = utils.find_stat_by_name(stats, 'd') cfsab = fsa.children[fsb] cfsbc = fsb.children[fsc] cfscd = fsc.children[fsd] self.assertEqual(fsa.ttot , 6) self.assertEqual(fsa.tsub , 1) self.assertEqual(fsb.ttot , 5) self.assertEqual(fsb.tsub , 2) self.assertEqual(fsc.ttot , 3) self.assertEqual(fsc.tsub , 2) self.assertEqual(fsd.ttot , 1) self.assertEqual(fsd.tsub , 1) self.assertEqual(cfsab.ttot , 5) self.assertEqual(cfsab.tsub , 2) self.assertEqual(cfsbc.ttot , 3) self.assertEqual(cfsbc.tsub , 2) self.assertEqual(cfscd.ttot , 1) self.assertEqual(cfscd.tsub , 1)
def test_basic(self): yappi.set_clock_type('wall') def dummy(): pass def a(): time.sleep(0.2) class Worker1(threading.Thread): def a(self): time.sleep(0.3) def run(self): self.a() yappi.start(builtins=False, profile_threads=True) c = Worker1() c.start() c.join() a() stats = yappi.get_func_stats() fsa1 = utils.find_stat_by_name(stats, 'Worker1.a') fsa2 = utils.find_stat_by_name(stats, 'a') self.assertTrue(fsa1 is not None) self.assertTrue(fsa2 is not None) self.assertTrue(fsa1.ttot > 0.2) self.assertTrue(fsa2.ttot > 0.1) tstats = yappi.get_thread_stats() self.assertEqual(len(tstats), 2) tsa = utils.find_stat_by_name(tstats, 'Worker1') tsm = utils.find_stat_by_name(tstats, '_MainThread') dummy() # call dummy to force ctx name to be retrieved again. self.assertTrue(tsa is not None) # TODO: I put dummy() to fix below, remove the comments after a while. self.assertTrue( # FIX: I see this fails sometimes? tsm is not None, 'Could not find "_MainThread". Found: %s' % (', '.join(utils.get_stat_names(tstats))))
def test_merge_multithreaded_stats(self): import threading import _yappi timings = {"a_1":2, "b_1":1} _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("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("ystats2.ys") stats = yappi.YFuncStats(["ystats1.ys", "ystats2.ys",]) fsa = utils.find_stat_by_name(stats, "a") fsb = utils.find_stat_by_name(stats, "b") self.assertEqual(fsa.ncall, 2) self.assertEqual(fsb.ncall, 1) self.assertEqual(fsa.tsub, fsa.ttot, 4) self.assertEqual(fsb.tsub, fsb.ttot, 1)
def test_singlethread_profiling(self): import threading import time yappi.set_clock_type('wall') def a(): time.sleep(0.2) class Worker1(threading.Thread): def a(self): time.sleep(0.3) def run(self): self.a() yappi.start(profile_threads=False) c = Worker1() c.start() c.join() a() stats = yappi.get_func_stats() fsa1 = utils.find_stat_by_name(stats, 'Worker1.a') fsa2 = utils.find_stat_by_name(stats, 'a') self.assertTrue(fsa1 is None) self.assertTrue(fsa2 is not None) self.assertTrue(fsa2.ttot > 0.1)
def test_abaa(self): _timings = {"a_1":13,"b_1":10,"a_2":9,"a_3":5} _yappi._set_test_timings(_timings) self._ncall = 1 def a(): if self._ncall == 1: b() elif self._ncall == 2: self._ncall += 1 a() else: return def b(): self._ncall += 1 a() stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') cfsaa = fsa.children[fsa] cfsba = fsb.children[fsa] self.assertEqual(fsb.ttot , 10) self.assertEqual(fsb.tsub , 1) self.assertEqual(fsa.ttot , 13) self.assertEqual(fsa.tsub , 12) self.assertEqual(cfsaa.ttot , 5) self.assertEqual(cfsaa.tsub , 5) self.assertEqual(cfsba.ttot , 9) self.assertEqual(cfsba.tsub , 4)
def test_singlethread_profiling(self): yappi.set_clock_type("wall") def a(): time.sleep(0.2) class Worker1(threading.Thread): def a(self): time.sleep(0.3) def run(self): self.a() yappi.start(profile_threads=False) c = Worker1() c.start() c.join() a() stats = yappi.get_func_stats() fsa1 = utils.find_stat_by_name(stats, "Worker1.a") fsa2 = utils.find_stat_by_name(stats, "a") self.assertTrue(fsa1 is None) self.assertTrue(fsa2 is not None) self.assertTrue(fsa2.ttot > 0.1)
def test_aaab(self): _timings = {"a_1":13,"a_2":10,"a_3":6,"b_1":1} _yappi._set_test_timings(_timings) self._ncall = 1 def a(): if self._ncall == 3: b() return self._ncall += 1 a() def b(): return stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') cfsaa = fsa.children[fsa] cfsab = fsa.children[fsb] self.assertEqual(fsa.ttot , 13) self.assertEqual(fsa.tsub , 12) self.assertEqual(fsb.ttot , 1) self.assertEqual(fsb.tsub , 1) self.assertEqual(cfsaa.ttot , 10) self.assertEqual(cfsaa.tsub , 9) self.assertEqual(cfsab.ttot , 1) self.assertEqual(cfsab.tsub , 1)
def test_abbb(self): _timings = {"a_1": 13, "b_1": 10, "b_2": 6, "b_3": 1} _yappi._set_test_timings(_timings) self._ncall = 1 def a(): if self._ncall == 1: b() def b(): if self._ncall == 3: return self._ncall += 1 b() stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, "a") fsb = utils.find_stat_by_name(stats, "b") cfsab = fsa.children[fsb] cfsbb = fsb.children[fsb] self.assertEqual(fsa.ttot, 13) self.assertEqual(fsa.tsub, 3) self.assertEqual(fsb.ttot, 10) self.assertEqual(fsb.tsub, 10) self.assertEqual(fsb.ncall, 3) self.assertEqual(fsb.nactualcall, 1) self.assertEqual(cfsab.ttot, 10) self.assertEqual(cfsab.tsub, 4) self.assertEqual(cfsbb.ttot, 6) self.assertEqual(cfsbb.tsub, 6) self.assertEqual(cfsbb.nactualcall, 0) self.assertEqual(cfsbb.ncall, 2)
def test_abcabc(self): _timings = {"a_1":20,"b_1":19,"c_1":17, "a_2":13, "b_2":11, "c_2":9, "a_3":6} _yappi._set_test_timings(_timings) def a(n): if n == 3: return else: b(n) def b(n): c(n) def c(n): a(n+1) stats = utils.run_and_get_func_stats(a, 1) fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') fsc = utils.find_stat_by_name(stats, 'c') self.assertEqual(fsa.ncall , 3) self.assertEqual(fsa.nactualcall , 1) self.assertEqual(fsa.ttot , 20) self.assertEqual(fsa.tsub , 9) self.assertEqual(fsb.ttot , 19) self.assertEqual(fsb.tsub , 4) self.assertEqual(fsc.ttot , 17) self.assertEqual(fsc.tsub , 7) cfsab = fsa.children[fsb] cfsbc = fsb.children[fsc] cfsca = fsc.children[fsa] self.assertEqual(cfsab.ttot , 19) self.assertEqual(cfsab.tsub , 4) self.assertEqual(cfsbc.ttot , 17) self.assertEqual(cfsbc.tsub , 7) self.assertEqual(cfsca.ttot , 13) self.assertEqual(cfsca.tsub , 8)
def test_pstats_conversion(self): def pstat_id(fs): return (fs.module, fs.lineno, fs.name) def a(): d() def b(): d() def c(): pass def d(): pass _timings = {"a_1":12,"b_1":7,"c_1":5,"d_1":2} _yappi._set_test_timings(_timings) stats = utils.run_and_get_func_stats(a,) stats.strip_dirs() stats.save("a1.pstats", type="pstat") fsa_pid = pstat_id(utils.find_stat_by_name(stats, "a")) fsd_pid = pstat_id(utils.find_stat_by_name(stats, "d")) yappi.clear_stats() _yappi._set_test_timings(_timings) stats = utils.run_and_get_func_stats(a,) stats.strip_dirs() stats.save("a2.pstats", type="pstat") yappi.clear_stats() _yappi._set_test_timings(_timings) stats = utils.run_and_get_func_stats(b,) stats.strip_dirs() stats.save("b1.pstats", type="pstat") fsb_pid = pstat_id(utils.find_stat_by_name(stats, "b")) yappi.clear_stats() _yappi._set_test_timings(_timings) stats = utils.run_and_get_func_stats(c,) stats.strip_dirs() stats.save("c1.pstats", type="pstat") fsc_pid = pstat_id(utils.find_stat_by_name(stats, "c")) # merge saved stats and check pstats values are correct import pstats p = pstats.Stats('a1.pstats', 'a2.pstats', 'b1.pstats', 'c1.pstats') p.strip_dirs() # ct = ttot, tt = tsub (cc, nc, tt, ct, callers) = p.stats[fsa_pid] self.assertEqual(cc, nc, 2) self.assertEqual(tt, 20) self.assertEqual(ct, 24) (cc, nc, tt, ct, callers) = p.stats[fsd_pid] self.assertEqual(cc, nc, 3) self.assertEqual(tt, 6) self.assertEqual(ct, 6) self.assertEqual(len(callers), 2) (cc, nc, tt, ct) = callers[fsa_pid] self.assertEqual(cc, nc, 2) self.assertEqual(tt, 4) self.assertEqual(ct, 4) (cc, nc, tt, ct) = callers[fsb_pid] self.assertEqual(cc, nc, 1) self.assertEqual(tt, 2) self.assertEqual(ct, 2)
def test_profile_single_context(self): def id_callback(): return self.callback_count def a(): pass self.callback_count = 1 yappi.set_context_id_callback(id_callback) 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)
def test_merge_load_different_clock_types(self): import threading yappi.start(builtins=True) def a(): b() def b(): c() def c(): pass t = threading.Thread(target=a) t.start() t.join() yappi.get_func_stats().sort("name", "asc").save("ystats1.ys") yappi.stop() yappi.clear_stats() yappi.start(builtins=False) t = threading.Thread(target=a) t.start() t.join() yappi.get_func_stats().save("ystats2.ys") yappi.stop() self.assertRaises(_yappi.error, yappi.set_clock_type, "wall") yappi.clear_stats() yappi.set_clock_type("wall") yappi.start() t = threading.Thread(target=a) t.start() t.join() yappi.get_func_stats().save("ystats3.ys") self.assertRaises(yappi.YappiError, yappi.YFuncStats().add("ystats1.ys").add, "ystats3.ys") stats = yappi.YFuncStats(["ystats1.ys", "ystats2.ys"]).sort("name") fsa = utils.find_stat_by_name(stats, "a") fsb = utils.find_stat_by_name(stats, "b") fsc = utils.find_stat_by_name(stats, "c") self.assertEqual(fsa.ncall, 2) self.assertEqual(fsa.ncall, fsb.ncall, fsc.ncall)
def test_basic(self): import threading import time yappi.set_clock_type('wall') def a(): time.sleep(0.2) class Worker1(threading.Thread): def a(self): time.sleep(0.3) def run(self): self.a() yappi.start(builtins=False, profile_threads=True) c = Worker1() c.start() c.join() a() stats = yappi.get_func_stats() fsa1 = utils.find_stat_by_name(stats, 'Worker1.a') fsa2 = utils.find_stat_by_name(stats, 'a') self.assertTrue(fsa1 is not None) self.assertTrue(fsa2 is not None) self.assertTrue(fsa1.ttot > 0.2) self.assertTrue(fsa2.ttot > 0.1) tstats = yappi.get_thread_stats() self.assertEqual(len(tstats), 2) tsa = utils.find_stat_by_name(tstats, 'Worker1') tsm = utils.find_stat_by_name(tstats, '_MainThread') self.assertTrue(tsa is not None) self.assertTrue(tsm is not None) # FIX: I see this fails sometimes?
def test_abab(self): _timings = {"a_1":13,"b_1":10,"a_2":6,"b_2":1} _yappi._set_test_timings(_timings) self._ncall = 1 def a(): b() def b(): if self._ncall == 2: return self._ncall += 1 a() stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') cfsab = fsa.children[fsb] cfsba = fsb.children[fsa] self.assertEqual(fsa.ttot , 13) self.assertEqual(fsa.tsub , 8) self.assertEqual(fsb.ttot , 10) self.assertEqual(fsb.tsub , 5) self.assertEqual(cfsab.ttot , 10) self.assertEqual(cfsab.tsub , 5) self.assertEqual(cfsab.ncall , 2) self.assertEqual(cfsab.nactualcall , 1) self.assertEqual(cfsba.ttot , 6) self.assertEqual(cfsba.tsub , 5)
def test_aabccb(self): _timings = { "a_1": 13, "a_2": 11, "b_1": 9, "c_1": 5, "c_2": 3, "b_2": 1 } _yappi._set_test_timings(_timings) self._ncall = 1 def a(): if self._ncall == 1: self._ncall += 1 a() else: b() def b(): if self._ncall == 3: return else: c() def c(): if self._ncall == 2: self._ncall += 1 c() else: b() stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') fsc = utils.find_stat_by_name(stats, 'c') cfsaa = fsa.children[fsa.index] cfsab = fsa.children[fsb] cfsbc = fsb.children[fsc.full_name] cfscc = fsc.children[fsc] cfscb = fsc.children[fsb] self.assertEqual(fsb.ttot, 9) self.assertEqual(fsb.tsub, 5) self.assertEqual(cfsbc.ttot, 5) self.assertEqual(cfsbc.tsub, 2) self.assertEqual(fsa.ttot, 13) self.assertEqual(fsa.tsub, 4) self.assertEqual(cfsab.ttot, 9) self.assertEqual(cfsab.tsub, 4) self.assertEqual(cfsaa.ttot, 11) self.assertEqual(cfsaa.tsub, 2) self.assertEqual(fsc.ttot, 5) self.assertEqual(fsc.tsub, 4)
def test_subsequent_profile(self): import threading WORKER_COUNT = 5 def a(): pass def b(): pass def c(): pass _timings = {"a_1":3,"b_1":2,"c_1":1,} yappi.start() def g(): pass g() yappi.stop() yappi.clear_stats() _yappi._set_test_timings(_timings) yappi.start() _dummy = [] for i in range(WORKER_COUNT): t = threading.Thread(target=a) t.start() t.join() for i in range(WORKER_COUNT): t = threading.Thread(target=b) t.start() _dummy.append(t) t.join() for i in range(WORKER_COUNT): t = threading.Thread(target=a) t.start() t.join() for i in range(WORKER_COUNT): t = threading.Thread(target=c) t.start() t.join() yappi.stop() yappi.start() def f(): pass f() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') fsc = utils.find_stat_by_name(stats, 'c') self.assertEqual(fsa.ncall, 10) self.assertEqual(fsb.ncall, 5) self.assertEqual(fsc.ncall, 5) self.assertEqual(fsa.ttot, fsa.tsub, 30) self.assertEqual(fsb.ttot, fsb.tsub, 10) self.assertEqual(fsc.ttot, fsc.tsub, 5) # MACOSx optimizes by only creating one worker thread self.assertTrue(len(yappi.get_thread_stats()) >= 2)
def test_abcbca(self): _timings = { "a_1": 10, "b_1": 9, "c_1": 7, "b_2": 4, "c_2": 2, "a_2": 1 } _yappi._set_test_timings(_timings) self._ncall = 1 def a(): if self._ncall == 1: b() else: return def b(): c() def c(): if self._ncall == 1: self._ncall += 1 b() else: a() stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') fsc = utils.find_stat_by_name(stats, 'c') cfsab = fsa.children[fsb] cfsbc = fsb.children[fsc] cfsca = fsc.children[fsa] self.assertEqual(fsa.ttot, 10) self.assertEqual(fsa.tsub, 2) self.assertEqual(fsb.ttot, 9) self.assertEqual(fsb.tsub, 4) self.assertEqual(fsc.ttot, 7) self.assertEqual(fsc.tsub, 4) self.assertEqual(cfsab.ttot, 9) self.assertEqual(cfsab.tsub, 2) self.assertEqual(cfsbc.ttot, 7) self.assertEqual(cfsbc.tsub, 4) self.assertEqual(cfsca.ttot, 1) self.assertEqual(cfsca.tsub, 1) self.assertEqual(cfsca.ncall, 1) self.assertEqual(cfsca.nactualcall, 0)
def test_abcadc(self): _timings = { "a_1": 20, "b_1": 19, "c_1": 17, "a_2": 13, "d_1": 12, "c_2": 10, "a_3": 5 } _yappi._set_test_timings(_timings) def a(n): if n == 3: return if n == 1 + 1: d(n) else: b(n) def b(n): c(n) def c(n): a(n + 1) def d(n): c(n) stats = utils.run_and_get_func_stats(a, 1) fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') fsc = utils.find_stat_by_name(stats, 'c') fsd = utils.find_stat_by_name(stats, 'd') self.assertEqual(fsa.ncall, 3) self.assertEqual(fsa.nactualcall, 1) self.assertEqual(fsa.ttot, 20) self.assertEqual(fsa.tsub, 7) self.assertEqual(fsb.ttot, 19) self.assertEqual(fsb.tsub, 2) self.assertEqual(fsc.ttot, 17) self.assertEqual(fsc.tsub, 9) self.assertEqual(fsd.ttot, 12) self.assertEqual(fsd.tsub, 2) cfsca = fsc.children[fsa] self.assertEqual(cfsca.nactualcall, 0) self.assertEqual(cfsca.ncall, 2) self.assertEqual(cfsca.ttot, 13) self.assertEqual(cfsca.tsub, 6)
def test_callback(self): self.context_id = 0 yappi.set_context_id_callback(lambda: self.context_id) yappi.start() a() self.context_id = 1 a() self.context_id = 2 a() # Re-schedule context 1. self.context_id = 1 a() yappi.stop() threadstats = yappi.get_thread_stats().sort('id', 'ascending') self.assertEqual(3, len(threadstats)) self.assertEqual(0, threadstats[0].id) self.assertEqual(1, threadstats[1].id) self.assertEqual(2, threadstats[2].id) self.assertEqual(1, threadstats[0].sched_count) self.assertEqual(2, threadstats[1].sched_count) # Context 1 ran twice. self.assertEqual(1, threadstats[2].sched_count) funcstats = yappi.get_func_stats() self.assertEqual(4, utils.find_stat_by_name(funcstats, 'a').ncall)
def test_children_stat_functions(self): _timings = {"a_1":5, "b_1":3, "c_1":1} _yappi._set_test_timings(_timings) 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())
def test_slice_child_stats_and_strip_dirs(self): def b(): for i in range(10000000): pass def a(): b() yappi.start(builtins=True) a() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') self.assertTrue(fsa.children[0:1] is not None) prev_afullname = fsa.full_name prev_bchildfullname = fsa.children[fsb].full_name stats.strip_dirs() self.assertTrue(len(prev_afullname) > len(fsa.full_name)) self.assertTrue(len(prev_bchildfullname) > len(fsa.children[fsb].full_name))
def test_strip_dirs(self): def a(): pass stats = utils.run_and_get_func_stats(a,) stats.strip_dirs() fsa = utils.find_stat_by_name(stats, "a") self.assertEqual(fsa.module, os.path.basename(fsa.module))
def test_builtin_profiling(self): import threading def a(): import time time.sleep(0.4) # is a builtin function yappi.set_clock_type('wall') yappi.start(builtins=True) a() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, 'sleep') self.assertTrue(fsa is not None) self.assertTrue(fsa.ttot > 0.3) yappi.stop() yappi.clear_stats() def a(): pass yappi.start() t = threading.Thread(target=a) t.start() t.join() stats = yappi.get_func_stats()
def test_aabccb(self): _timings = {"a_1": 13, "a_2": 11, "b_1": 9, "c_1": 5, "c_2": 3, "b_2": 1} _yappi._set_test_timings(_timings) self._ncall = 1 def a(): if self._ncall == 1: self._ncall += 1 a() else: b() def b(): if self._ncall == 3: return else: c() def c(): if self._ncall == 2: self._ncall += 1 c() else: b() stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, "a") fsb = utils.find_stat_by_name(stats, "b") fsc = utils.find_stat_by_name(stats, "c") cfsaa = fsa.children[fsa.index] cfsab = fsa.children[fsb] cfsbc = fsb.children[fsc.full_name] cfscc = fsc.children[fsc] cfscb = fsc.children[fsb] self.assertEqual(fsb.ttot, 9) self.assertEqual(fsb.tsub, 5) self.assertEqual(cfsbc.ttot, 5) self.assertEqual(cfsbc.tsub, 2) self.assertEqual(fsa.ttot, 13) self.assertEqual(fsa.tsub, 4) self.assertEqual(cfsab.ttot, 9) self.assertEqual(cfsab.tsub, 4) self.assertEqual(cfsaa.ttot, 11) self.assertEqual(cfsaa.tsub, 2) self.assertEqual(fsc.ttot, 5) self.assertEqual(fsc.tsub, 4)
def test_lambda(self): f = lambda: time.sleep(0.3) yappi.set_clock_type("wall") yappi.start() f() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, "<lambda>") self.assertTrue(fsa.ttot > 0.1)
def test_lambda(self): f = lambda: time.sleep(0.3) yappi.set_clock_type("wall") yappi.start() f() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, '<lambda>') self.assertTrue(fsa.ttot > 0.1)
def test_clear_stats_while_running(self): def a(): pass yappi.start() a() yappi.clear_stats() a() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, 'a') self.assertEqual(fsa.ncall, 1)
def test_fibonacci(self): def fib(n): if n > 1: return fib(n-1) + fib(n-2) else: return n stats = utils.run_and_get_func_stats(fib, 22) fs = utils.find_stat_by_name(stats, 'fib') self.assertEqual(fs.ncall, 57313) self.assertEqual(fs.ttot, fs.tsub)
def test_subsequent_profile(self): _timings = {"a_1":1, "b_1":1} _yappi._set_test_timings(_timings) def a(): pass def b(): pass yappi.start() a() yappi.stop() yappi.start() b() yappi.stop() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') self.assertTrue(fsa is not None) self.assertTrue(fsb is not None) self.assertEqual(fsa.ttot, 1) self.assertEqual(fsb.ttot, 1)
def test_abcbca(self): _timings = {"a_1": 10, "b_1": 9, "c_1": 7, "b_2": 4, "c_2": 2, "a_2": 1} _yappi._set_test_timings(_timings) self._ncall = 1 def a(): if self._ncall == 1: b() else: return def b(): c() def c(): if self._ncall == 1: self._ncall += 1 b() else: a() stats = utils.run_and_get_func_stats(a) fsa = utils.find_stat_by_name(stats, "a") fsb = utils.find_stat_by_name(stats, "b") fsc = utils.find_stat_by_name(stats, "c") cfsab = fsa.children[fsb] cfsbc = fsb.children[fsc] cfsca = fsc.children[fsa] self.assertEqual(fsa.ttot, 10) self.assertEqual(fsa.tsub, 2) self.assertEqual(fsb.ttot, 9) self.assertEqual(fsb.tsub, 4) self.assertEqual(fsc.ttot, 7) self.assertEqual(fsc.tsub, 4) self.assertEqual(cfsab.ttot, 9) self.assertEqual(cfsab.tsub, 2) self.assertEqual(cfsbc.ttot, 7) self.assertEqual(cfsbc.tsub, 4) self.assertEqual(cfsca.ttot, 1) self.assertEqual(cfsca.tsub, 1) self.assertEqual(cfsca.ncall, 1) self.assertEqual(cfsca.nactualcall, 0)
def test_run_recursive(self): def profiled(): pass def not_profiled(): pass yappi.clear_stats() try: with yappi.run(): with yappi.run(): profiled() # Profiling stopped here not_profiled() stats = yappi.get_func_stats() finally: yappi.clear_stats() self.assertIsNotNone(utils.find_stat_by_name(stats, 'profiled')) self.assertIsNone(utils.find_stat_by_name(stats, 'not_profiled'))
def test_invalid_tag(self): def tag_cbk(): return -1 yappi.set_tag_callback(tag_cbk) yappi.start() tag_cbk() yappi.stop() stats = yappi.get_func_stats() stat = find_stat_by_name(stats, 'tag_cbk') self.assertEqual(stat.ncall, 1)
def test_abcadc(self): _timings = {"a_1": 20, "b_1": 19, "c_1": 17, "a_2": 13, "d_1": 12, "c_2": 10, "a_3": 5} _yappi._set_test_timings(_timings) def a(n): if n == 3: return if n == 1 + 1: d(n) else: b(n) def b(n): c(n) def c(n): a(n + 1) def d(n): c(n) stats = utils.run_and_get_func_stats(a, 1) fsa = utils.find_stat_by_name(stats, "a") fsb = utils.find_stat_by_name(stats, "b") fsc = utils.find_stat_by_name(stats, "c") fsd = utils.find_stat_by_name(stats, "d") self.assertEqual(fsa.ncall, 3) self.assertEqual(fsa.nactualcall, 1) self.assertEqual(fsa.ttot, 20) self.assertEqual(fsa.tsub, 7) self.assertEqual(fsb.ttot, 19) self.assertEqual(fsb.tsub, 2) self.assertEqual(fsc.ttot, 17) self.assertEqual(fsc.tsub, 9) self.assertEqual(fsd.ttot, 12) self.assertEqual(fsd.tsub, 2) cfsca = fsc.children[fsa] self.assertEqual(cfsca.nactualcall, 0) self.assertEqual(cfsca.ncall, 2) self.assertEqual(cfsca.ttot, 13) self.assertEqual(cfsca.tsub, 6)
def test_signals_with_blocking_calls(self): import signal, os, time # just to verify if signal is handled correctly and stats/yappi are not corrupted. def handler(signum, frame): raise Exception("Signal handler executed!") yappi.start() signal.signal(signal.SIGALRM, handler) signal.alarm(1) self.assertRaises(Exception, time.sleep, 2) stats = yappi.get_func_stats() fsh = utils.find_stat_by_name(stats, "handler") self.assertTrue(fsh is not None)
def test_stop_in_middle(self): _timings = {"a_1": 6, "b_1": 4} _yappi._set_test_timings(_timings) def a(): b() yappi.stop() def b(): time.sleep(0.2) yappi.start() a() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, 'a') fsb = utils.find_stat_by_name(stats, 'b') self.assertEqual(fsa.ncall, 1) self.assertEqual(fsa.nactualcall, 0) self.assertEqual(fsa.ttot, 0) # no call_leave called self.assertEqual(fsa.tsub, 0) # no call_leave called self.assertEqual(fsb.ttot, 4)
def test_stop_in_middle(self): _timings = {"a_1": 6, "b_1": 4} _yappi._set_test_timings(_timings) def a(): b() yappi.stop() def b(): time.sleep(0.2) yappi.start() a() stats = yappi.get_func_stats() fsa = utils.find_stat_by_name(stats, "a") fsb = utils.find_stat_by_name(stats, "b") self.assertEqual(fsa.ncall, 1) self.assertEqual(fsa.nactualcall, 0) self.assertEqual(fsa.ttot, 0) # no call_leave called self.assertEqual(fsa.tsub, 0) # no call_leave called self.assertEqual(fsb.ttot, 4)
def test_no_stats_different_clock_type_load(self): def a(): pass yappi.start() a() yappi.stop() yappi.get_func_stats().save("ystats1.ys") yappi.clear_stats() yappi.set_clock_type("WALL") yappi.start() yappi.stop() stats = yappi.get_func_stats().add("ystats1.ys") fsa = utils.find_stat_by_name(stats, 'a') self.assertTrue(fsa is not None)
def test_run(self): def profiled(): pass yappi.clear_stats() try: with yappi.run(): profiled() stats = yappi.get_func_stats() finally: yappi.clear_stats() self.assertIsNotNone(utils.find_stat_by_name(stats, 'profiled'))