-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbenchmark.py
283 lines (221 loc) · 7.93 KB
/
benchmark.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
#!/usr/bin/env python
import time
import random
import sys
options = None
total_time = None
def run_test(func, name):
sys.stdout.write(name + ': ')
sys.stdout.flush()
start_time = time.time()
try:
func()
except:
print "failed or not supported"
global options
if options.verbose:
import traceback; traceback.print_exc()
else:
end_time = time.time()
global total_time
total_time += end_time - start_time
print "%f seconds" % (end_time - start_time)
class BigObject(object):
def __init__(self, letter='1', size=10000):
self.object = letter * size
def __eq__(self, other):
return self.object == other.object
class Benchmark(object):
def __init__(self, module, options):
self.module = module
self.options = options
self.init_server()
self.test_set()
self.test_set_get()
self.test_random_get()
self.test_set_same()
self.test_set_big_object()
self.test_set_get_big_object()
self.test_set_big_string()
self.test_set_get_big_string()
self.test_get()
self.test_get_big_object()
self.test_get_multi()
self.test_get_list()
def init_server(self):
#self.mc = self.module.Client([self.options.server_address])
self.mc = self.module.Client(["faramir:11217"])
self.mc.set_behavior(self.module.BEHAVIOR_BINARY_PROTOCOL, 1)
self.mc.set('bench_key', "E" * 50)
num_tests = self.options.num_tests
self.keys = ['key%d' % i for i in xrange(num_tests)]
self.values = ['value%d' % i for i in xrange(num_tests)]
self.random_keys = ['key%d' % random.randint(0, num_tests) for i in xrange(num_tests * 3)]
def test_set(self):
set_ = self.mc.set
pairs = zip(self.keys, self.values)
def test():
for key, value in pairs:
set_(key, value)
def test_loop():
for i in range(10):
for key, value in pairs:
set_(key, value)
run_test(test, 'test_set')
for key, value in pairs:
self.mc.delete(key)
def test_set_get(self):
set_ = self.mc.set
get_ = self.mc.get
pairs = zip(self.keys, self.values)
def test():
for key, value in pairs:
set_(key, value)
result = get_(key)
assert result == value
run_test(test, 'test_set_get')
#for key, value in pairs:
# self.mc.delete(key)
def test_random_get(self):
get_ = self.mc.get
set_ = self.mc.set
value = "chenyin"
def test():
index = 0
for key in self.random_keys:
result = get_(key)
index += 1
if(index % 5 == 0):
set_(key, value)
run_test(test, 'test_random_get')
def test_set_same(self):
set_ = self.mc.set
def test():
for i in xrange(self.options.num_tests):
set_('key', 'value')
def test_loop():
for i in range(10):
for i in xrange(self.options.num_tests):
set_('key', 'value')
run_test(test, 'test_set_same')
self.mc.delete('key')
def test_set_big_object(self):
set_ = self.mc.set
# libmemcached is slow to store large object, so limit the
# number of objects here to make tests not stall.
pairs = [('key%d' % i, BigObject()) for i in xrange(100)]
def test():
for key, value in pairs:
set_(key, value)
run_test(test, 'test_set_big_object (100 objects)')
for key, value in pairs:
self.mc.delete(key)
def test_set_get_big_object(self):
set_ = self.mc.set
get_ = self.mc.get
# libmemcached is slow to store large object, so limit the
# number of objects here to make tests not stall.
pairs = [('key%d' % i, BigObject()) for i in xrange(100)]
def test():
for key, value in pairs:
set_(key, value)
result = get_(key)
assert result == value
run_test(test, 'test_set_get_big_object (100 objects)')
#for key, value in pairs:
# self.mc.delete(key)
def test_set_get_big_string(self):
set_ = self.mc.set
get_ = self.mc.get
# libmemcached is slow to store large object, so limit the
# number of objects here to make tests not stall.
pairs = [('key%d' % i, 'x' * 10000) for i in xrange(100)]
def test():
for key, value in pairs:
set_(key, value)
result = get_(key)
assert result == value
run_test(test, 'test_set_get_big_string (100 objects)')
def test_set_big_string(self):
set_ = self.mc.set
# libmemcached is slow to store large object, so limit the
# number of objects here to make tests not stall.
pairs = [('key%d' % i, 'x' * 10000) for i in xrange(100)]
def test():
for key, value in pairs:
set_(key, value)
run_test(test, 'test_set_big_string (100 objects)')
for key, value in pairs:
self.mc.delete(key)
def test_get(self):
pairs = zip(self.keys, self.values)
for key, value in pairs:
self.mc.set(key, value)
get = self.mc.get
def test():
for key, value in pairs:
result = get(key)
assert result == value
run_test(test, 'test_get')
for key, value in pairs:
self.mc.delete(key)
def test_get_big_object(self):
pairs = [('bkey%d' % i, BigObject('x')) for i in xrange(100)]
for key, value in pairs:
self.mc.set(key, value)
get = self.mc.get
expected_values = [BigObject('x') for i in xrange(100)]
def test():
for i in xrange(100):
result = get('bkey%d' % i)
assert result == expected_values[i]
run_test(test, 'test_get_big_object (100 objects)')
for key, value in pairs:
self.mc.delete(key)
def test_get_multi(self):
pairs = zip(self.keys, self.values)
for key, value in pairs:
self.mc.set(key, value)
keys = self.keys
expected_result = dict(pairs)
def test():
result = self.mc.get_multi(keys)
assert result == expected_result
run_test(test, 'test_get_multi')
for key, value in pairs:
self.mc.delete(key)
def test_get_list(self):
pairs = zip(self.keys, self.values)
for key, value in pairs:
self.mc.set(key, value)
keys = self.keys
expected_result = self.values
def test():
result = self.mc.get_list(keys)
assert result == expected_result
run_test(test, 'test_get_list')
for key in self.keys:
self.mc.delete(key)
def main():
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-a', '--server-address', dest='server_address',
default='127.0.0.1:11211',
help="address:port of memcached [default: 127.0.0.1:11211]")
parser.add_option('-n', '--num-tests', dest='num_tests', type='int',
default=1000,
help="repeat counts of each test [default: 1000]")
parser.add_option('-v', '--verbose', dest='verbose',
action='store_true', default=False,
help="show traceback infomation if a test fails")
global options
options, args = parser.parse_args()
global total_time
total_time = 0
print "Benchmarking cmemcached..."
import cmemcached
Benchmark(cmemcached, options)
if __name__ == '__main__':
main()
global total_time
print "total_time is %f" % total_time