/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 breezy/tests/test_lsprof.py

  • Committer: Jelmer Vernooij
  • Date: 2017-11-21 00:06:54 UTC
  • mfrom: (6816.3.4 py3_lsprof)
  • Revision ID: jelmer@jelmer.uk-20171121000654-74rn9g7pondybwcc
Merge lp:~jelmer/brz/py3_lsprof.

Show diffs side-by-side

added added

removed removed

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