bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
5557.1.15
by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt |
1 |
# Copyright (C) 2007, 2009, 2010, 2011 Canonical Ltd
|
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
16 |
|
17 |
"""Tests for profiling data collection."""
|
|
18 |
||
19 |
||
|
6621.2.26
by Martin
Misc set of changes to get started with selftest on Python 3 |
20 |
try: |
21 |
import cPickle as pickle |
|
22 |
except ImportError: |
|
23 |
import pickle |
|
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
24 |
import threading |
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
25 |
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
26 |
from .. import ( |
27 |
errors, |
|
28 |
osutils, |
|
29 |
tests, |
|
|
5967.12.1
by Martin Pool
Move all test features into bzrlib.tests.features |
30 |
)
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
31 |
from ..tests import features |
32 |
||
33 |
||
34 |
lsprof = features.lsprof_feature.module |
|
|
1551.15.28
by Aaron Bentley
Improve Feature usage style w/ lsprof |
35 |
|
36 |
||
|
2493.2.5
by Ian Clatworthy
explicit format saving test |
37 |
_TXT_HEADER = " CallCount Recursive Total(ms) " + \ |
38 |
"Inline(ms) module:lineno(function)\n" |
|
39 |
||
40 |
||
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
41 |
def _junk_callable(): |
42 |
"A simple routine to profile."
|
|
43 |
result = sorted(['abc', 'def', 'ghi']) |
|
44 |
||
45 |
||
46 |
def _collect_stats(): |
|
47 |
"Collect and return some dummy profile data."
|
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
48 |
ret, stats = lsprof.profile(_junk_callable) |
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
49 |
return stats |
50 |
||
51 |
||
|
6816.3.2
by Martin
Test lsprof stat sorting and make pass on Python 3 |
52 |
class TestStats(tests.TestCaseInTempDir): |
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
53 |
|
|
5967.12.1
by Martin Pool
Move all test features into bzrlib.tests.features |
54 |
_test_needs_features = [features.lsprof_feature] |
|
1551.15.28
by Aaron Bentley
Improve Feature usage style w/ lsprof |
55 |
|
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
56 |
def setUp(self): |
|
2493.2.6
by Aaron Bentley
Make LSProf into a Feature required by the appropriate tests |
57 |
super(tests.TestCaseInTempDir, self).setUp() |
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
58 |
self.stats = _collect_stats() |
59 |
||
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
60 |
def _temppath(self, ext): |
61 |
return osutils.pathjoin(self.test_dir, "tmp_profile_data." + ext) |
|
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
62 |
|
|
6816.3.2
by Martin
Test lsprof stat sorting and make pass on Python 3 |
63 |
def test_save_to_txt(self): |
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
64 |
path = self._temppath("txt") |
65 |
self.stats.save(path) |
|
66 |
with open(path) as f: |
|
67 |
lines = f.readlines() |
|
68 |
self.assertEqual(lines[0], _TXT_HEADER) |
|
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
69 |
|
|
6816.3.2
by Martin
Test lsprof stat sorting and make pass on Python 3 |
70 |
def test_save_to_callgrind(self): |
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
71 |
path1 = self._temppath("callgrind") |
72 |
self.stats.save(path1) |
|
73 |
with open(path1) as f: |
|
74 |
self.assertEqual(f.readline(), "events: Ticks\n") |
|
75 |
||
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") |
|
80 |
||
|
2493.2.5
by Ian Clatworthy
explicit format saving test |
81 |
# Test explicit format nommination
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
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") |
|
|
2493.2.3
by Ian Clatworthy
changes requested in jameinel's review incorporated |
86 |
|
|
6816.3.2
by Martin
Test lsprof stat sorting and make pass on Python 3 |
87 |
def test_save_to_pickle(self): |
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
88 |
path = self._temppath("pkl") |
89 |
self.stats.save(path) |
|
90 |
with open(path, 'rb') as f: |
|
91 |
data1 = pickle.load(f) |
|
92 |
self.assertEqual(type(data1), lsprof.Stats) |
|
|
4641.3.2
by Robert Collins
lsprof support. |
93 |
|
|
6816.3.2
by Martin
Test lsprof stat sorting and make pass on Python 3 |
94 |
def test_sort(self): |
95 |
self.stats.sort() |
|
96 |
code_list = [d.inlinetime for d in self.stats.data] |
|
97 |
self.assertEqual(code_list, sorted(code_list, reverse=True)) |
|
98 |
||
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)) |
|
103 |
||
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') |
|
107 |
||
|
4641.3.2
by Robert Collins
lsprof support. |
108 |
|
109 |
class TestBzrProfiler(tests.TestCase): |
|
110 |
||
|
5967.12.1
by Martin Pool
Move all test features into bzrlib.tests.features |
111 |
_test_needs_features = [features.lsprof_feature] |
|
4641.3.2
by Robert Collins
lsprof support. |
112 |
|
113 |
def test_start_call_stuff_stop(self): |
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
114 |
profiler = lsprof.BzrProfiler() |
|
4641.3.2
by Robert Collins
lsprof support. |
115 |
profiler.start() |
116 |
try: |
|
117 |
def a_function(): |
|
118 |
pass
|
|
119 |
a_function() |
|
120 |
finally: |
|
121 |
stats = profiler.stop() |
|
122 |
stats.freeze() |
|
123 |
lines = [str(data) for data in stats.data] |
|
124 |
lines = [line for line in lines if 'a_function' in line] |
|
125 |
self.assertLength(1, lines) |
|
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
126 |
|
127 |
def test_block_0(self): |
|
128 |
# When profiler_block is 0, reentrant profile requests fail.
|
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
129 |
self.overrideAttr(lsprof.BzrProfiler, 'profiler_block', 0) |
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
130 |
inner_calls = [] |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
131 |
|
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
132 |
def inner(): |
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
133 |
profiler = lsprof.BzrProfiler() |
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
134 |
self.assertRaises(errors.BzrError, profiler.start) |
135 |
inner_calls.append(True) |
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
136 |
lsprof.profile(inner) |
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
137 |
self.assertLength(1, inner_calls) |
138 |
||
139 |
def test_block_1(self): |
|
140 |
# When profiler_block is 1, concurrent profiles serialise.
|
|
141 |
# This is tested by manually acquiring the profiler lock, then
|
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
142 |
# starting a thread that tries to profile, and releasing the lock.
|
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
143 |
# We know due to test_block_0 that two profiles at once hit the lock,
|
144 |
# so while this isn't perfect (we'd want a callback on the lock being
|
|
145 |
# entered to allow lockstep evaluation of the actions), its good enough
|
|
146 |
# to be confident regressions would be caught. Alternatively, if this
|
|
147 |
# is flakey, a fake Lock object can be used to trace the calls made.
|
|
148 |
calls = [] |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
149 |
|
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
150 |
def profiled(): |
151 |
calls.append('profiled') |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
152 |
|
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
153 |
def do_profile(): |
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
154 |
lsprof.profile(profiled) |
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
155 |
calls.append('after_profiled') |
156 |
thread = threading.Thread(target=do_profile) |
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
157 |
lsprof.BzrProfiler.profiler_lock.acquire() |
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
158 |
try: |
159 |
try: |
|
160 |
thread.start() |
|
161 |
finally: |
|
|
6816.3.1
by Martin
Make lsprof import on Python 3 but not pass all tests yet |
162 |
lsprof.BzrProfiler.profiler_lock.release() |
|
5331.1.1
by Robert Collins
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded |
163 |
finally: |
164 |
thread.join() |
|
165 |
self.assertLength(2, calls) |