forked from oracle/graalpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_microbench.py
More file actions
226 lines (184 loc) · 5.66 KB
/
Copy pathtest_microbench.py
File metadata and controls
226 lines (184 loc) · 5.66 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
These are not real tests, but microbenchmarks. The machinery to record the
timing and display the results is inside conftest.py
"""
import pytest
import _valgrind
API_PARAMS = [
pytest.param('cpy', marks=pytest.mark.cpy),
pytest.param('hpy', marks=pytest.mark.hpy)
]
@pytest.fixture(params=API_PARAMS)
def api(request):
return request.param
@pytest.fixture
def simple(request, api):
if api == 'cpy':
import cpy_simple
return cpy_simple
elif api == 'hpy':
import hpy_simple
return hpy_simple
else:
assert False, 'Unkown param: %s' % request.param
@pytest.fixture
def N(request):
n = 10000000
if request.config.option.fast:
n //= 100
if request.config.option.slow:
n *= 10
if _valgrind.lib.is_running_on_valgrind():
n //= 100
return n
class TestModule:
def test_noargs(self, simple, timer, N):
with timer:
for i in range(N):
simple.noargs()
def test_onearg_None(self, simple, timer, N):
with timer:
for i in range(N):
simple.onearg(None)
def test_onearg_int(self, simple, timer, N):
with timer:
for i in range(N):
simple.onearg(i)
def test_varargs(self, simple, timer, N):
with timer:
for i in range(N):
simple.varargs(None, None)
def test_call_with_tuple(self, simple, timer, N):
def f(a, b):
return a + b
with timer:
for i in range(N):
simple.call_with_tuple(f, (1, 2))
def test_call_with_tuple_and_dict(self, simple, timer, N):
def f(a, b):
return a + b
with timer:
for i in range(N):
simple.call_with_tuple_and_dict(f, (1,), {"b": 2})
def test_allocate_int(self, simple, timer, N):
with timer:
for i in range(N):
simple.allocate_int()
def test_allocate_tuple(self, api, simple, timer, N):
with timer:
for i in range(N):
simple.allocate_tuple()
class TestType:
""" Compares the performance of operations on types.
The kinds of type used are:
* cpy: a static type
* hpy: a heap type (HPy only has heap types)
The type is named `simple.Foo` in both cases.
"""
def test_allocate_obj(self, simple, timer, N):
import gc
Foo = simple.Foo
objs = [None] * N
gc.collect()
with timer:
for i in range(N):
objs[i] = Foo()
del objs
gc.collect()
def test_method_lookup(self, simple, timer, N):
obj = simple.Foo()
with timer:
for i in range(N):
# note: here we are NOT calling it, we want to measure just
# the lookup
obj.noargs
def test_noargs(self, simple, timer, N):
obj = simple.Foo()
with timer:
for i in range(N):
obj.noargs()
def test_onearg_None(self, simple, timer, N):
obj = simple.Foo()
with timer:
for i in range(N):
obj.onearg(None)
def test_onearg_int(self, simple, timer, N):
obj = simple.Foo()
with timer:
for i in range(N):
obj.onearg(i)
def test_varargs(self, simple, timer, N):
obj = simple.Foo()
with timer:
for i in range(N):
obj.varargs(None, None)
def test_len(self, simple, timer, N):
obj = simple.Foo()
with timer:
for i in range(N):
len(obj)
def test_getitem(self, simple, timer, N):
obj = simple.Foo()
with timer:
for i in range(N):
obj[0]
class TestHeapType:
""" Compares the performance of operations on heap types.
The type is named `simple.HTFoo` and is a heap type in all cases.
"""
def test_allocate_obj_and_survive(self, simple, timer, N):
import gc
HTFoo = simple.HTFoo
objs = [None] * N
gc.collect()
with timer:
for i in range(N):
objs[i] = HTFoo()
del objs
gc.collect()
def test_allocate_obj_and_die(self, simple, timer, N):
import gc
HTFoo = simple.HTFoo
gc.collect()
with timer:
for i in range(N):
obj = HTFoo()
obj.onearg(None)
gc.collect()
def test_method_lookup(self, simple, timer, N):
obj = simple.HTFoo()
with timer:
for i in range(N):
# note: here we are NOT calling it, we want to measure just
# the lookup
obj.noargs
def test_noargs(self, simple, timer, N):
obj = simple.HTFoo()
with timer:
for i in range(N):
obj.noargs()
def test_onearg_None(self, simple, timer, N):
obj = simple.HTFoo()
with timer:
for i in range(N):
obj.onearg(None)
def test_onearg_int(self, simple, timer, N):
obj = simple.HTFoo()
with timer:
for i in range(N):
obj.onearg(i)
def test_varargs(self, simple, timer, N):
obj = simple.HTFoo()
with timer:
for i in range(N):
obj.varargs(None, None)
def test_len(self, simple, timer, N):
obj = simple.HTFoo()
with timer:
for i in range(N):
len(obj)
def test_getitem(self, simple, timer, N):
obj = simple.HTFoo()
with timer:
for i in range(N):
obj[0]