-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
lockbench.py
271 lines (218 loc) · 6.73 KB
/
lockbench.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import sys
from threading import Thread
from threading import RLock
from fastrlock.rlock import FastRLock as FLock
# Benchmark functions:
cython_code = []
def lock_unlock(l):
l.acquire()
l.release()
l.acquire()
l.release()
l.acquire()
l.release()
l.acquire()
l.release()
l.acquire()
l.release()
cython_code.append("""
def lock_unlock(lock):
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
""")
def reentrant_lock_unlock(l):
l.acquire()
l.acquire()
l.acquire()
l.acquire()
l.acquire()
l.release()
l.release()
l.release()
l.release()
l.release()
cython_code.append("""
def reentrant_lock_unlock(lock):
lock_fastrlock(lock, -1, True)
lock_fastrlock(lock, -1, True)
lock_fastrlock(lock, -1, True)
lock_fastrlock(lock, -1, True)
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
unlock_fastrlock(lock)
unlock_fastrlock(lock)
unlock_fastrlock(lock)
unlock_fastrlock(lock)
""")
def mixed_lock_unlock(l):
l.acquire()
l.release()
l.acquire()
l.acquire()
l.release()
l.acquire()
l.release()
l.acquire()
l.release()
l.release()
cython_code.append("""
def mixed_lock_unlock(lock):
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
lock_fastrlock(lock, -1, True)
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
lock_fastrlock(lock, -1, True)
unlock_fastrlock(lock)
unlock_fastrlock(lock)
""")
def context_manager(l):
with l: pass
with l:
with l:
with l: pass
with l: pass
with l:
with l: pass
with l:
with l: pass
with l: pass
with l: pass
with l:
with l:
with l: pass
with l: pass
with l:
with l: pass
with l: pass
def lock_unlock_nonblocking(l):
if l.acquire(False):
l.release()
if l.acquire(False):
l.release()
if l.acquire(False):
l.release()
if l.acquire(False):
l.release()
if l.acquire(False):
l.release()
cython_code.append("""
def lock_unlock_nonblocking(lock):
if lock_fastrlock(lock, -1, False):
unlock_fastrlock(lock)
if lock_fastrlock(lock, -1, False):
unlock_fastrlock(lock)
if lock_fastrlock(lock, -1, False):
unlock_fastrlock(lock)
if lock_fastrlock(lock, -1, False):
unlock_fastrlock(lock)
if lock_fastrlock(lock, -1, False):
unlock_fastrlock(lock)
""")
# End of benchmark functions
def threaded(l, test_func, tcount=10):
threads = [ Thread(target=test_func, args=(l,)) for _ in range(tcount) ]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def run_benchmark(name, lock, version, functions, repeat_count, repeat_count_t):
print('Testing %s (%s)' % (name, version))
from timeit import Timer
from functools import partial
print("sequential (x%d):" % repeat_count)
for function in functions:
timer = Timer(partial(function, lock))
print('%-25s: %9.2f msec' % (function.__name__, max(timer.repeat(repeat=4, number=repeat_count)) * 1000.0))
print("threaded 10T (x%d):" % repeat_count_t)
for function in functions:
timer = Timer(partial(threaded, lock, function))
print('%-25s: %9.2f msec' % (function.__name__, max(timer.repeat(repeat=4, number=repeat_count_t)) * 1000.0))
if sys.version_info < (3, 5):
import imp
def load_dynamic(name, module_path):
return imp.load_dynamic(name, module_path)
else:
import importlib.util as _importlib_util
def load_dynamic(name, module_path):
spec = _importlib_util.spec_from_file_location(name, module_path)
module = _importlib_util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def main():
functions = [
lock_unlock,
reentrant_lock_unlock,
mixed_lock_unlock,
lock_unlock_nonblocking,
context_manager,
]
import fastrlock
import glob
import os.path
import re
import tempfile
repeat_count = 100000
repeat_count_t = 1000
rlock = (RLock(), "%d.%d.%d" % sys.version_info[:3])
flock = (FLock(), fastrlock.__version__)
locks = []
args = sys.argv[1:]
if 'rlock' in args:
locks.append(rlock)
if 'flock' in args:
locks.append(flock)
if not args:
locks = [rlock, flock]
for _ in range(args.count('quick')):
repeat_count = max(10, repeat_count // 100)
repeat_count_t = max(5, repeat_count_t // 10)
for lock, version in locks:
name = type(lock).__name__
run_benchmark(name, lock, version, functions, repeat_count, repeat_count_t)
if 'cython' in args or not args:
lock, version = flock
from Cython.Build.Cythonize import cython_compile, parse_args
basepath = None
try:
with tempfile.NamedTemporaryFile(mode="w", suffix='.pyx') as f:
code = '\n'.join(cython_code)
cy_function_names = re.findall(r"def (\w+)\(", code)
f.write("from fastrlock.rlock cimport lock_fastrlock, unlock_fastrlock\n\n")
f.write(code)
f.flush()
options, _ = parse_args(["", f.name, "-3", "--force", "--inplace"])
cython_compile(f.name, options)
basepath = os.path.splitext(f.name)[0]
for ext in [".*.so", ".*.pyd", ".so", ".pyd"]:
so_paths = glob.glob(basepath + ext)
if so_paths:
so_path = so_paths[0]
break
else:
print("Failed to find Cython compiled module")
sys.exit(1)
module = load_dynamic(os.path.basename(basepath), so_path)
cy_functions = [getattr(module, name) for name in cy_function_names]
run_benchmark("Cython interface of %s" % type(lock).__name__, lock, version,
cy_functions, repeat_count, repeat_count_t)
finally:
if basepath:
files = glob.glob(basepath + ".*") # .c, .so / .pyd
if len(files) > 3:
print("Found too many artefacts, not deleting temporary files")
else:
for filename in files:
os.unlink(filename)
if __name__ == '__main__':
main()