forked from pyutils/line_profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_line_profiler.py
153 lines (126 loc) · 3.84 KB
/
test_line_profiler.py
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
import pytest
from line_profiler import LineProfiler
def f(x):
"""A docstring."""
y = x + 10
return y
def g(x):
y = yield x + 10
yield y + 20
class C:
@classmethod
def c(self, value):
print(value)
return 0
def test_init():
lp = LineProfiler()
assert lp.functions == []
assert lp.code_map == {}
lp = LineProfiler(f)
assert lp.functions == [f]
assert lp.code_map == {f.__code__: {}}
lp = LineProfiler(f, g)
assert lp.functions == [f, g]
assert lp.code_map == {
f.__code__: {},
g.__code__: {},
}
def test_enable_disable():
lp = LineProfiler()
assert lp.enable_count == 0
lp.enable_by_count()
assert lp.enable_count == 1
lp.enable_by_count()
assert lp.enable_count == 2
lp.disable_by_count()
assert lp.enable_count == 1
lp.disable_by_count()
assert lp.enable_count == 0
assert lp.last_time == {}
lp.disable_by_count()
assert lp.enable_count == 0
with lp:
assert lp.enable_count == 1
with lp:
assert lp.enable_count == 2
assert lp.enable_count == 1
assert lp.enable_count == 0
assert lp.last_time == {}
with pytest.raises(RuntimeError):
assert lp.enable_count == 0
with lp:
assert lp.enable_count == 1
raise RuntimeError()
assert lp.enable_count == 0
assert lp.last_time == {}
def test_function_decorator():
profile = LineProfiler()
f_wrapped = profile(f)
assert f_wrapped.__name__ == 'f'
assert profile.enable_count == 0
value = f_wrapped(10)
assert profile.enable_count == 0
assert value == f(10)
def test_gen_decorator():
profile = LineProfiler()
g_wrapped = profile(g)
assert g_wrapped.__name__ == 'g'
assert profile.enable_count == 0
i = g_wrapped(10)
assert profile.enable_count == 0
assert next(i) == 20
assert profile.enable_count == 0
assert i.send(30) == 50
assert profile.enable_count == 0
with pytest.raises(StopIteration):
next(i)
assert profile.enable_count == 0
def test_classmethod_decorator():
profile = LineProfiler()
c_wrapped = profile(C.c)
assert c_wrapped.__name__ == 'c'
assert profile.enable_count == 0
val = c_wrapped('test')
assert profile.enable_count == 0
assert val == C.c('test')
assert profile.enable_count == 0
def test_show_func_column_formatting():
from line_profiler.line_profiler import show_func
import line_profiler
import io
# Use a function in this module as an example
func = line_profiler.line_profiler.show_text
start_lineno = func.__code__.co_firstlineno
filename = func.__code__.co_filename
func_name = func.__name__
def get_func_linenos(func):
import sys
if sys.version_info[0:2] >= (3, 10):
return sorted(set([t[2] for t in func.__code__.co_lines()]))
else:
import dis
return sorted(set([t[1] for t in dis.findlinestarts(func.__code__)]))
line_numbers = get_func_linenos(func)
unit = 1.0
output_unit = 1.0
stripzeros = False
# Build fake timeings for each line in the example function
timings = [
(lineno, idx * 1e13, idx * (2e10 ** (idx % 3)))
for idx, lineno in enumerate(line_numbers, start=1)
]
stream = io.StringIO()
show_func(filename, start_lineno, func_name, timings, unit,
output_unit, stream, stripzeros)
text = stream.getvalue()
print(text)
timings = [
(lineno, idx * 1e15, idx * 2e19)
for idx, lineno in enumerate(line_numbers, start=1)
]
stream = io.StringIO()
show_func(filename, start_lineno, func_name, timings, unit,
output_unit, stream, stripzeros)
text = stream.getvalue()
print(text)
# TODO: write a check to verify columns are aligned nicely