1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
import unittest
import yappi
import threading
import time
from utils import YappiUnitTestCase, find_stat_by_name, burn_cpu, burn_io
class MultiThreadTests(YappiUnitTestCase):
def test_tagging_walltime(self):
tlocal = threading.local()
def tag_cbk():
try:
return tlocal._tag
except Exception as e:
#print(e)
return -1
def a(tag):
tlocal._tag = tag
burn_io(0.1)
_TCOUNT = 20
yappi.set_clock_type("wall")
tlocal._tag = 0
yappi.set_tag_callback(tag_cbk)
yappi.start()
ts = []
for i in range(_TCOUNT):
t = threading.Thread(target=a, args=(i + 1, ))
ts.append(t)
for t in ts:
t.start()
for t in ts:
t.join()
yappi.stop()
traces = yappi.get_func_stats()
t1 = '''
..p/yappi/tests/utils.py:134 burn_io 20 0.000638 2.004059 0.100203
'''
self.assert_traces_almost_equal(t1, traces)
traces = yappi.get_func_stats(filter={'tag': 3})
t1 = '''
..p/yappi/tests/utils.py:134 burn_io 1 0.000038 0.100446 0.100446
'''
self.assert_traces_almost_equal(t1, traces)
def test_tagging_cputime(self):
tlocal = threading.local()
def tag_cbk():
try:
return tlocal._tag
except Exception as e:
#print(e)
return -1
def a(tag):
tlocal._tag = tag
burn_cpu(0.1)
_TCOUNT = 5
ts = []
yappi.set_clock_type("cpu")
tlocal._tag = 0
yappi.set_tag_callback(tag_cbk)
yappi.start()
for i in range(_TCOUNT):
t = threading.Thread(target=a, args=(i + 1, ))
ts.append(t)
for t in ts:
t.start()
for t in ts:
t.join()
yappi.stop()
traces = yappi.get_func_stats()
t1 = '''
..op/p/yappi/tests/test_tags.py:21 a 5 0.000137 0.500562 0.000000
../yappi/tests/utils.py:125 burn_cpu 5 0.000000 0.500424 0.000000
'''
self.assert_traces_almost_equal(t1, traces)
traces = yappi.get_func_stats(filter={'tag': 1})
t1 = '''
../yappi/tests/utils.py:125 burn_cpu 1 0.000000 0.100125 0.100125
'''
self.assert_traces_almost_equal(t1, traces)
traces = yappi.get_func_stats(filter={'tag': 3})
t1 = '''
../yappi/tests/utils.py:125 burn_cpu 1 0.000000 0.100128 0.100128
'''
self.assert_traces_almost_equal(t1, traces)
class SingleThreadTests(YappiUnitTestCase):
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_simple_tagging(self):
def tag_cbk():
return 1
def tag_cbk2():
return 2
# test cpu-time
yappi.set_tag_callback(tag_cbk)
yappi.start()
burn_cpu(0.1)
yappi.set_tag_callback(tag_cbk2)
burn_cpu(0.1)
yappi.stop()
traces = yappi.get_func_stats()
t1 = '''
../yappi/tests/utils.py:125 burn_cpu 2 0.000000 0.200156 0.100078
'''
self.assert_traces_almost_equal(t1, traces)
tagged_traces = yappi.get_func_stats(filter={'tag': 1})
t1 = '''
../yappi/tests/utils.py:125 burn_cpu 1 0.000000 0.100062 0.100062
'''
self.assert_traces_almost_equal(t1, tagged_traces)
yappi.clear_stats()
# test wall
yappi.set_clock_type("wall")
yappi.set_tag_callback(tag_cbk)
yappi.start()
burn_io(0.1)
yappi.set_tag_callback(tag_cbk2)
burn_io(0.1)
yappi.stop()
traces = yappi.get_func_stats()
t1 = '''
..p/yappi/tests/utils.py:134 burn_io 2 0.000000 0.208146 0.104073
'''
self.assert_traces_almost_equal(t1, traces)
tagged_traces = yappi.get_func_stats(filter={'tag': 2})
t1 = '''
..p/yappi/tests/utils.py:134 burn_io 1 0.000000 0.105063 0.105063
'''
self.assert_traces_almost_equal(t1, tagged_traces)
yappi.clear_stats()
|