57
54
super(tests.TestCaseInTempDir, self).setUp()
58
55
self.stats = _collect_stats()
60
def _temppath(self, ext):
61
return osutils.pathjoin(self.test_dir, "tmp_profile_data." + ext)
63
def test_save_to_txt(self):
64
path = self._temppath("txt")
68
self.assertEqual(lines[0], _TXT_HEADER)
70
def test_save_to_callgrind(self):
71
path1 = self._temppath("callgrind")
72
self.stats.save(path1)
73
with open(path1) as f:
74
self.assertEqual(f.readline(), "events: Ticks\n")
76
path2 = osutils.pathjoin(self.test_dir, "callgrind.out.foo")
77
self.stats.save(path2)
78
with open(path2) as f:
79
self.assertEqual(f.readline(), "events: Ticks\n")
57
def _tempfile(self, ext):
59
return breezy.osutils.pathjoin(dir, "tmp_profile_data." + ext)
61
def test_stats_save_to_txt(self):
62
f = self._tempfile("txt")
64
lines = open(f).readlines()
65
self.assertEqual(lines[0], _TXT_HEADER)
67
def test_stats_save_to_callgrind(self):
68
f = self._tempfile("callgrind")
70
lines = open(f).readlines()
71
self.assertEqual(lines[0], "events: Ticks\n")
72
f = breezy.osutils.pathjoin(self.test_dir, "callgrind.out.foo")
74
lines = open(f).readlines()
75
self.assertEqual(lines[0], "events: Ticks\n")
81
76
# Test explicit format nommination
82
path3 = self._temppath("txt")
83
self.stats.save(path3, format="callgrind")
84
with open(path3) as f:
85
self.assertEqual(f.readline(), "events: Ticks\n")
87
def test_save_to_pickle(self):
88
path = self._temppath("pkl")
90
with open(path, 'rb') as f:
91
data1 = pickle.load(f)
92
self.assertEqual(type(data1), lsprof.Stats)
96
code_list = [d.inlinetime for d in self.stats.data]
97
self.assertEqual(code_list, sorted(code_list, reverse=True))
99
def test_sort_totaltime(self):
100
self.stats.sort('totaltime')
101
code_list = [d.totaltime for d in self.stats.data]
102
self.assertEqual(code_list, sorted(code_list, reverse=True))
104
def test_sort_code(self):
105
"""Cannot sort by code object would need to get filename etc."""
106
self.assertRaises(ValueError, self.stats.sort, 'code')
77
f2 = self._tempfile("txt")
78
self.stats.save(f2, format="callgrind")
79
lines2 = open(f2).readlines()
80
self.assertEqual(lines2[0], "events: Ticks\n")
82
def test_stats_save_to_pickle(self):
83
f = self._tempfile("pkl")
85
data1 = pickle.load(open(f))
86
self.assertEqual(type(data1), breezy.lsprof.Stats)
109
89
class TestBzrProfiler(tests.TestCase):
127
107
def test_block_0(self):
128
108
# When profiler_block is 0, reentrant profile requests fail.
129
self.overrideAttr(lsprof.BzrProfiler, 'profiler_block', 0)
109
self.overrideAttr(breezy.lsprof.BzrProfiler, 'profiler_block', 0)
133
profiler = lsprof.BzrProfiler()
112
profiler = breezy.lsprof.BzrProfiler()
134
113
self.assertRaises(errors.BzrError, profiler.start)
135
114
inner_calls.append(True)
136
lsprof.profile(inner)
115
breezy.lsprof.profile(inner)
137
116
self.assertLength(1, inner_calls)
139
118
def test_block_1(self):
140
119
# When profiler_block is 1, concurrent profiles serialise.
141
120
# This is tested by manually acquiring the profiler lock, then
142
# starting a thread that tries to profile, and releasing the lock.
121
# starting a thread that tries to profile, and releasing the lock.
143
122
# We know due to test_block_0 that two profiles at once hit the lock,
144
123
# so while this isn't perfect (we'd want a callback on the lock being
145
124
# entered to allow lockstep evaluation of the actions), its good enough
146
125
# to be confident regressions would be caught. Alternatively, if this
147
126
# is flakey, a fake Lock object can be used to trace the calls made.
151
129
calls.append('profiled')
153
130
def do_profile():
154
lsprof.profile(profiled)
131
breezy.lsprof.profile(profiled)
155
132
calls.append('after_profiled')
156
133
thread = threading.Thread(target=do_profile)
157
lsprof.BzrProfiler.profiler_lock.acquire()
134
breezy.lsprof.BzrProfiler.profiler_lock.acquire()
162
lsprof.BzrProfiler.profiler_lock.release()
139
breezy.lsprof.BzrProfiler.profiler_lock.release()
165
142
self.assertLength(2, calls)