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_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_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_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_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_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_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_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_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_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_stat_sorting(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) stats = stats.sort("totaltime", "desc") prev_stat = None for stat in stats: if prev_stat: self.assertTrue(prev_stat.ttot >= stat.ttot) prev_stat = stat stats = stats.sort("totaltime", "asc") prev_stat = None for stat in stats: if prev_stat: self.assertTrue(prev_stat.ttot <= stat.ttot) prev_stat = stat stats = stats.sort("avgtime", "asc") prev_stat = None for stat in stats: if prev_stat: self.assertTrue(prev_stat.tavg <= stat.tavg) prev_stat = stat stats = stats.sort("name", "asc") prev_stat = None for stat in stats: if prev_stat: self.assertTrue(prev_stat.name <= stat.name) prev_stat = stat stats = stats.sort("subtime", "asc") prev_stat = None for stat in stats: if prev_stat: self.assertTrue(prev_stat.tsub <= stat.tsub) prev_stat = stat self.assertRaises(yappi.YappiError, stats.sort, "invalid_func_sorttype_arg") self.assertRaises(yappi.YappiError, stats.sort, "totaltime", "invalid_func_sortorder_arg")
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_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_aaaa(self): _timings = {"d_1":9, "d_2":7, "d_3":3, "d_4":2} _yappi._set_test_timings(_timings) def d(n): if n == 3: return d(n+1) stats = utils.run_and_get_func_stats(d, 0) fsd = utils.find_stat_by_name(stats, 'd') self.assertEqual(fsd.ncall , 4) self.assertEqual(fsd.nactualcall , 1) self.assertEqual(fsd.ttot , 9) self.assertEqual(fsd.tsub , 9) cfsdd = fsd.children[fsd] self.assertEqual(cfsdd.ttot , 7) self.assertEqual(cfsdd.tsub , 7) self.assertEqual(cfsdd.ncall , 3) self.assertEqual(cfsdd.nactualcall , 0)
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_merge_aabab_aabbc(self): _timings = {"a_1":15,"a_2":14,"b_1":12,"a_3":10,"b_2":9, "c_1":4} _yappi._set_test_timings(_timings) def a(): if self._ncall == 1: self._ncall += 1 a() elif self._ncall == 5: self._ncall += 1 a() else: b() def b(): if self._ncall == 2: self._ncall += 1 a() elif self._ncall == 6: self._ncall += 1 b() elif self._ncall == 7: c() else: return def c(): pass self._ncall = 1 stats = utils.run_and_get_func_stats(a,) stats.save("ystats1.ys") yappi.clear_stats() _yappi._set_test_timings(_timings) #stats.print_all() self._ncall = 5 stats = utils.run_and_get_func_stats(a,) stats.save("ystats2.ys") #stats.print_all() def a(): # same name but another function(code object) pass yappi.start() a() stats = yappi.get_func_stats().add(["ystats1.ys", "ystats2.ys"]) #stats.print_all() self.assertEqual(len(stats), 4) fsa = None for stat in stats: if stat.name == "a" and stat.ttot == 45: fsa = stat break self.assertTrue(fsa is not None) self.assertEqual(fsa.ncall, 7) self.assertEqual(fsa.nactualcall, 3) self.assertEqual(fsa.ttot, 45) self.assertEqual(fsa.tsub, 10) fsb = utils.find_stat_by_name(stats, "b") fsc = utils.find_stat_by_name(stats, "c") self.assertEqual(fsb.ncall, 6) self.assertEqual(fsb.nactualcall, 3) self.assertEqual(fsb.ttot, 36) self.assertEqual(fsb.tsub, 27) self.assertEqual(fsb.tavg, 6) self.assertEqual(fsc.ttot, 8) self.assertEqual(fsc.tsub, 8) self.assertEqual(fsc.tavg, 4) self.assertEqual(fsc.nactualcall, fsc.ncall, 2)