-
Notifications
You must be signed in to change notification settings - Fork 76
/
runbench.py
452 lines (355 loc) · 13.1 KB
/
runbench.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python
# encoding: utf-8
"""Run benchmarks with build/lib.* in sys.path"""
import sys
import math
import logging
import threading
from functools import wraps
from collections import namedtuple
from contextlib import contextmanager
from queue import Queue
import pylibmc
import libmc
if sys.version_info.major == 2:
from time import clock as process_time
else:
from time import process_time
logger = logging.getLogger('libmc.bench')
Benchmark = namedtuple('Benchmark', 'name f args kwargs')
Participant = namedtuple('Participant', 'name factory threads', defaults=(1,))
BENCH_TIME = 1.0
N_SERVERS = 20
NTHREADS = 4
POOL_SIZE = 4
# setting (eg) NTHREADS to 40 and POOL_SIZE to 4 illustrates a failure case of a
# simpler python solution to thread pools for clients
#NTHREADS = 40
#POOL_SIZE = 4
class Prefix(object):
'''add prefix for key in mc command'''
def __init__(self, mc_client, prefix):
self.mc = mc_client
self.prefix = prefix
def __repr__(self):
return "Prefix " + str(self.mc)
def __prepare_prefix_keys(self, keys):
if isinstance(keys, dict):
prepared_keys = {}
for key, value in keys.items():
prepared_keys[self.prefix + key] = value
return prepared_keys
elif isinstance(keys, (list, tuple)):
return [self.prefix + v for v in keys]
else:
return self.prefix + keys
def get_multi(self, keys):
r = {}
for key, value in self.mc.get_multi(self.__prepare_prefix_keys(keys)).items():
r[key.replace(self.prefix, '', 1)] = value
return r
def __getattr__(self, name):
if name in ('get', 'add', 'replace', 'set', 'cas', 'delete', 'incr', 'decr', 'prepend', 'append', 'touch',
'expire', 'gets'):
def func(key, *args, **kwargs):
key = self.__prepare_prefix_keys(key)
return getattr(self.mc, name)(key, *args, **kwargs)
return func
elif name in ('get_multi', 'append_multi', 'prepend_multi', 'delete_multi', 'set_multi', 'get_list'):
def func(keys, *args, **kwargs):
keys = self.__prepare_prefix_keys(keys)
return getattr(self.mc, name)(keys, *args, **kwargs)
return func
elif not name.startswith('__'):
def func(*args, **kwargs):
return getattr(self.mc, name)(*args, **kwargs)
return func
raise AttributeError(name)
def ratio(a, b):
if a > b:
return (a / b, 1)
elif a < b:
return (1, b / a)
else:
return (1, 1)
class Stopwatch(object):
"A stopwatch that never stops"
def __init__(self):
self.t0 = process_time()
self.laps = []
def __unicode__(self):
m = self.mean()
d = self.stddev()
fmt = u"%.3gs, \u03C3=%.3g, n=%d, snr=%.3g:%.3g".__mod__
return fmt((m, d, len(self.laps)) + ratio(m, d))
__str__ = __unicode__
def mean(self):
return sum(self.laps) / len(self.laps)
def stddev(self):
mean = self.mean()
return math.sqrt(sum((lap - mean)**2 for lap in self.laps) / len(self.laps))
def total(self):
return process_time() - self.t0
@contextmanager
def timing(self):
t0 = process_time()
try:
yield
finally:
te = process_time()
self.laps.append(te - t0)
class DelayedStopwatch(Stopwatch):
def __init__(self, laps=None, bound=0):
super().__init__()
self.laps = laps or []
self._bound = bound
@property
def bound(self):
return self._bound or sum(self.laps)
def timing(self):
self.t0 = process_time()
self.timing = super().timing
return super().timing()
def __add__(self, other):
bound = ((self.bound or other.bound) + (other.bound or self.bound)) / 2
return DelayedStopwatch(self.laps + other.laps, bound)
def mean(self):
return self.bound / len(self.laps)
def stddev(self):
boundless = DelayedStopwatch(self.laps) if self._bound else super()
return boundless.stddev()
def benchmark_method(f):
"decorator to turn f into a factory of benchmarks"
@wraps(f)
def inner(name, *args, **kwargs):
return Benchmark(name, f, args, kwargs)
return inner
@benchmark_method
def bench_get(mc, key, data):
if mc.get(key) != data:
logger.warn('%r.get(%r) fail', mc, key)
@benchmark_method
def bench_set(mc, key, data):
if any(isinstance(mc.mc, client) for client in libmc_clients):
if not mc.set(key, data):
logger.warn('%r.set(%r, ...) fail', mc, key)
else:
if not mc.set(key, data, min_compress_len=4001):
logger.warn('%r.set(%r, ...) fail', mc, key)
@benchmark_method
def bench_get_multi(mc, keys, pairs):
if len(mc.get_multi(keys)) != len(pairs):
logger.warn('%r.get_multi() incomplete', mc)
@benchmark_method
def bench_set_multi(mc, keys, pairs):
ret = mc.set_multi(pairs)
if any(isinstance(mc.mc, client) for client in libmc_clients):
if not ret:
logger.warn('%r.set_multi fail', mc)
else:
if ret:
logger.warn('%r.set_multi(%r) fail', mc, ret)
def multi_pairs(n, val_len):
d = {('multi_key_%d' % i): ('i' * val_len) for i in range(n)}
return (list(d.keys()), d)
complex_data_type = ([], {}, __import__('fractions').Fraction(3, 4))
# NOTE: set and get should be in order
benchmarks = [
bench_set_multi('Multi set 10 keys with value size 100', *multi_pairs(10, 100)),
bench_get_multi('Multi get 10 keys with value size 100', *multi_pairs(10, 100)),
bench_set_multi('Multi set 100 keys with value size 100', *multi_pairs(100, 100)),
bench_get_multi('Multi get 100 keys with value size 100', *multi_pairs(100, 100)),
bench_set_multi('Multi set 10 keys with value size 1000', *multi_pairs(10, 1000)),
bench_get_multi('Multi get 10 keys with value size 1000', *multi_pairs(10, 1000)),
bench_set('Small set', 'abc', 'all work no play jack is a dull boy'),
bench_get('Small get', 'abc', 'all work no play jack is a dull boy'),
bench_set('4k uncompressed set', 'abc' * 8, 'defb' * 1000),
bench_get('4k uncompressed get', 'abc' * 8, 'defb' * 1000),
bench_set('4k compressed set', 'abc' * 8, 'a' + 'defb' * 1000),
bench_get('4k compressed get', 'abc' * 8, 'a' + 'defb' * 1000),
bench_set('1M compressed set', 'abc', '1' * 1000000),
bench_get('1M compressed get', 'abc', '1' * 1000000),
bench_set('Complex data set', 'abc', complex_data_type),
bench_get('Complex data get', 'abc', complex_data_type),
]
def make_pylibmc_client(servers, **kw):
tcp_type = __import__('_pylibmc').server_type_tcp
servers_ = []
for addr in servers:
host, port = addr.split(':')
port = int(port)
servers_.append((tcp_type, host, port))
prefix = kw.pop('prefix')
return Prefix(__import__('pylibmc').Client(servers_, **kw), prefix)
class Pool:
''' adapted from pylibmc '''
client = libmc.ClientUnsafe
def __init__(self, *args, **kwargs):
self.args, self.kwargs = args, kwargs
def clone(self):
return self.client(*self.args, **self.kwargs)
def __getattr__(self, key):
if not hasattr(libmc.Client, key):
raise AttributeError
result = getattr(libmc.Client, key)
if callable(result):
@wraps(result)
def wrapper(*args, **kwargs):
with self.reserve() as mc:
return getattr(mc, key)(*args, **kwargs)
return wrapper
return result
class ThreadMappedPool(Pool):
client = libmc.Client
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.clients = {}
@property
def current_key(self):
return threading.current_thread().native_id
@contextmanager
def reserve(self):
key = self.current_key
mc = self.clients.pop(key, None)
if mc is None:
mc = self.clone()
try:
yield mc
finally:
self.clients[key] = mc
class ThreadPool(Pool):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.clients = Queue()
for _ in range(POOL_SIZE):
self.clients.put(self.clone())
@contextmanager
def reserve(self):
mc = self.clients.get()
try:
yield mc
finally:
self.clients.put(mc)
class BenchmarkThreadedClient(libmc.ThreadedClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.config(libmc.MC_INITIAL_CLIENTS, POOL_SIZE)
self.config(libmc.MC_MAX_CLIENTS, POOL_SIZE)
class FIFOThreadPool(ThreadPool):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.waiting = Queue()
self.semaphore = Queue(1) # sorry
self.semaphore.put(1)
@contextmanager
def reserve(self):
try:
self.semaphore.get()
mc = self.clients.get(False)
self.semaphore.put(1)
except:
channel = Queue(1)
self.waiting.put(channel)
self.semaphore.put(1)
channel.get()
mc = self.clients.get(False)
self.semaphore.put(1)
try:
yield mc
finally:
self.semaphore.get()
self.clients.put(mc)
try:
self.waiting.get(False).put(1)
except:
self.semaphore.put(1)
host = '127.0.0.1'
servers = ['%s:%d' % (host, 21211 + i) for i in range(N_SERVERS)]
libmc_clients = (libmc.Client, BenchmarkThreadedClient, ThreadMappedPool, ThreadPool)
libmc_kwargs = {"servers": servers, "comp_threshold": 4000}
participants = [
Participant(
name='pylibmc (md5 / ketama)',
factory=lambda:
make_pylibmc_client(servers, behaviors={
'verify_keys': True,
'hash': 'md5',
'ketama': True
}, prefix='pylibmc1')
),
Participant(
name='pylibmc (md5 / ketama / nodelay / nonblocking)',
factory=lambda: make_pylibmc_client(
servers,
behaviors={
'tcp_nodelay': True,
'verify_keys': True,
'hash': 'md5',
'ketama': True,
'no_block': True
},
prefix='pylibmc2'
)
),
Participant(name='python-memcached', factory=lambda: Prefix(__import__('memcache').Client(servers), 'memcache1')),
Participant(
name='libmc(md5 / ketama / nodelay / nonblocking, from douban)',
factory=lambda: Prefix(__import__('libmc').Client(**libmc_kwargs), 'libmc1')
),
Participant(
name='libmc(md5 / ketama / nodelay / nonblocking / C++ thread pool, from douban)',
factory=lambda: Prefix(BenchmarkThreadedClient(**libmc_kwargs), 'libmc2'),
threads=NTHREADS
),
]
def bench(participants=participants, benchmarks=benchmarks, bench_time=BENCH_TIME):
"""Do you even lift?"""
mcs = [p.factory() for p in participants]
means = [[] for p in participants]
stddevs = [[] for p in participants]
# Have each lifter do one benchmark each
last_fn = None
for benchmark_name, fn, args, kwargs in benchmarks:
logger.info('')
logger.info('%s', benchmark_name)
for i, (participant, mc) in enumerate(zip(participants, mcs)):
def loop(sw):
while sw.total() < bench_time:
with sw.timing():
fn(mc, *args, **kwargs)
# FIXME: set before bench for get
if 'get' in fn.__name__:
last_fn(mc, *args, **kwargs)
if participant.threads == 1:
sw = [DelayedStopwatch()]
loop(sw[0])
else:
sw = [DelayedStopwatch() for i in range(participant.threads)]
ts = [threading.Thread(target=loop, args=[i]) for i in sw]
for t in ts:
t.start()
for t in ts:
t.join()
total = sum(sw, DelayedStopwatch())
means[i].append(total.mean())
stddevs[i].append(total.stddev())
logger.info(u'%76s: %s', participant.name, total)
last_fn = fn
return means, stddevs
def main(args=sys.argv[1:]):
logger.info('pylibmc: %s', pylibmc.__file__)
logger.info('libmc: %s', libmc.__file__)
logger.info('Running %s servers, %s threads, and a %s client pool',
N_SERVERS, NTHREADS, POOL_SIZE)
ps = [p for p in participants if p.name in args]
ps = ps if ps else participants
bs = benchmarks[:]
logger.info('%d participants in %d benchmarks', len(ps), len(bs))
means, stddevs = bench(participants=ps, benchmarks=bs)
print('labels =', [p.name for p in ps])
print('benchmarks =', [b.name for b in bs])
print('means =', means)
print('stddevs =', stddevs)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()