/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/tests/test_lsprof.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tests for profiling data collection."""
18
18
 
19
19
 
20
 
try:
21
 
    import cPickle as pickle
22
 
except ImportError:
23
 
    import pickle
 
20
import cPickle
24
21
import threading
25
22
 
26
 
from .. import (
27
 
    errors,
28
 
    osutils,
29
 
    tests,
 
23
import brzlib
 
24
from brzlib import errors, tests
 
25
from brzlib.tests import (
 
26
    features,
30
27
    )
31
 
from ..tests import features
32
 
 
33
 
 
34
 
lsprof = features.lsprof_feature.module
35
28
 
36
29
 
37
30
_TXT_HEADER = "   CallCount    Recursive    Total(ms)   " + \
45
38
 
46
39
def _collect_stats():
47
40
    "Collect and return some dummy profile data."
48
 
    ret, stats = lsprof.profile(_junk_callable)
 
41
    from brzlib.lsprof import profile
 
42
    ret, stats = profile(_junk_callable)
49
43
    return stats
50
44
 
51
45
 
52
 
class TestStats(tests.TestCaseInTempDir):
 
46
class TestStatsSave(tests.TestCaseInTempDir):
53
47
 
54
48
    _test_needs_features = [features.lsprof_feature]
55
49
 
57
51
        super(tests.TestCaseInTempDir, self).setUp()
58
52
        self.stats = _collect_stats()
59
53
 
60
 
    def _temppath(self, ext):
61
 
        return osutils.pathjoin(self.test_dir, "tmp_profile_data." + ext)
62
 
 
63
 
    def test_save_to_txt(self):
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)
69
 
 
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")
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
 
 
 
54
    def _tempfile(self, ext):
 
55
        dir = self.test_dir
 
56
        return brzlib.osutils.pathjoin(dir, "tmp_profile_data." + ext)
 
57
 
 
58
    def test_stats_save_to_txt(self):
 
59
        f = self._tempfile("txt")
 
60
        self.stats.save(f)
 
61
        lines = open(f).readlines()
 
62
        self.assertEqual(lines[0], _TXT_HEADER)
 
63
 
 
64
    def test_stats_save_to_callgrind(self):
 
65
        f = self._tempfile("callgrind")
 
66
        self.stats.save(f)
 
67
        lines = open(f).readlines()
 
68
        self.assertEqual(lines[0], "events: Ticks\n")
 
69
        f = brzlib.osutils.pathjoin(self.test_dir, "callgrind.out.foo")
 
70
        self.stats.save(f)
 
71
        lines = open(f).readlines()
 
72
        self.assertEqual(lines[0], "events: Ticks\n")
81
73
        # 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")
86
 
 
87
 
    def test_save_to_pickle(self):
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)
93
 
 
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')
 
74
        f2 = self._tempfile("txt")
 
75
        self.stats.save(f2, format="callgrind")
 
76
        lines2 = open(f2).readlines()
 
77
        self.assertEqual(lines2[0], "events: Ticks\n")
 
78
 
 
79
    def test_stats_save_to_pickle(self):
 
80
        f = self._tempfile("pkl")
 
81
        self.stats.save(f)
 
82
        data1 = cPickle.load(open(f))
 
83
        self.assertEqual(type(data1), brzlib.lsprof.Stats)
107
84
 
108
85
 
109
86
class TestBzrProfiler(tests.TestCase):
111
88
    _test_needs_features = [features.lsprof_feature]
112
89
 
113
90
    def test_start_call_stuff_stop(self):
114
 
        profiler = lsprof.BzrProfiler()
 
91
        profiler = brzlib.lsprof.BzrProfiler()
115
92
        profiler.start()
116
93
        try:
117
94
            def a_function():
126
103
 
127
104
    def test_block_0(self):
128
105
        # When profiler_block is 0, reentrant profile requests fail.
129
 
        self.overrideAttr(lsprof.BzrProfiler, 'profiler_block', 0)
 
106
        self.overrideAttr(brzlib.lsprof.BzrProfiler, 'profiler_block', 0)
130
107
        inner_calls = []
131
 
 
132
108
        def inner():
133
 
            profiler = lsprof.BzrProfiler()
 
109
            profiler = brzlib.lsprof.BzrProfiler()
134
110
            self.assertRaises(errors.BzrError, profiler.start)
135
111
            inner_calls.append(True)
136
 
        lsprof.profile(inner)
 
112
        brzlib.lsprof.profile(inner)
137
113
        self.assertLength(1, inner_calls)
138
114
 
139
115
    def test_block_1(self):
140
116
        # When profiler_block is 1, concurrent profiles serialise.
141
117
        # This is tested by manually acquiring the profiler lock, then
142
 
        # starting a thread that tries to profile, and releasing the lock.
 
118
        # starting a thread that tries to profile, and releasing the lock. 
143
119
        # We know due to test_block_0 that two profiles at once hit the lock,
144
120
        # so while this isn't perfect (we'd want a callback on the lock being
145
121
        # entered to allow lockstep evaluation of the actions), its good enough
146
122
        # to be confident regressions would be caught. Alternatively, if this
147
123
        # is flakey, a fake Lock object can be used to trace the calls made.
148
124
        calls = []
149
 
 
150
125
        def profiled():
151
126
            calls.append('profiled')
152
 
 
153
127
        def do_profile():
154
 
            lsprof.profile(profiled)
 
128
            brzlib.lsprof.profile(profiled)
155
129
            calls.append('after_profiled')
156
130
        thread = threading.Thread(target=do_profile)
157
 
        lsprof.BzrProfiler.profiler_lock.acquire()
 
131
        brzlib.lsprof.BzrProfiler.profiler_lock.acquire()
158
132
        try:
159
133
            try:
160
134
                thread.start()
161
135
            finally:
162
 
                lsprof.BzrProfiler.profiler_lock.release()
 
136
                brzlib.lsprof.BzrProfiler.profiler_lock.release()
163
137
        finally:
164
138
            thread.join()
165
139
        self.assertLength(2, calls)