-
Notifications
You must be signed in to change notification settings - Fork 19
/
perf.py
292 lines (245 loc) · 8.75 KB
/
perf.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
import argparse
import logging
import multiprocessing
import os
import persistent
import re
import time
import ZODB
from BTrees.IOBTree import BTree
import ZEO
#logging.basicConfig(level='DEBUG')
class P(persistent.Persistent):
children = ()
parser = argparse.ArgumentParser("zodbshootout-inspired performance exercise")
parser.add_argument('--concurrency', '-c', type=int, default=1)
parser.add_argument('--client-hosts', '-C', type=int, default=1)
parser.add_argument('--object-size', '-o', type=int, default=999)
parser.add_argument('--transaction-size', '-t', type=int, default=3)
parser.add_argument('--repetitions', '-r', type=int, default=1000)
parser.add_argument('--demo-storage-baseline', '-d', action='store_true')
parser.add_argument('--address', '-a')
parser.add_argument('--ssl', action='store_true')
parser.add_argument('--server-sync', action='store_true')
parser.add_argument('--profile')
parser.add_argument('--save', '-s', help='save results to file')
parser.add_argument('--name', '-n', help='Run name')
parser.add_argument('--read-only', '-W', action='store_true')
def shoot():
"""zodbshootout-inspired performance exercise.
"""
options = parser.parse_args()
concurrency = options.concurrency
object_size = options.object_size
transaction_size = options.transaction_size
repetitions = options.repetitions
if options.ssl:
from ZEO.tests.testssl import server_config, client_ssl
else:
server_config = None
client_ssl = lambda : None
if options.demo_storage_baseline:
db_factory = None
headings = ('add', 'update', 'read')
stop = lambda : None
else:
if options.address:
addr = options.address
if ':' in addr:
host, port = addr.split(':')
addr = host, int(port)
else:
addr = '127.0.0.1', int(addr)
stop = lambda : None
else:
if os.path.exists('perf.fs'):
os.remove('perf.fs')
try:
addr, stop = ZEO.server(
threaded=False, path='perf.fs', zeo_conf=server_config)
except TypeError:
# ZEO 4
addr, stop = ZEO.server()
db_factory = lambda : ZEO.DB(
addr,
ssl=client_ssl(),
wait_timeout=9999,
server_sync=options.server_sync)
if options.read_only:
headings = ('read', 'prefetch')
else:
headings = ('add', 'update', 'cached', 'read', 'prefetch')
# Initialize database
db = db_factory()
with db.transaction() as conn:
conn.root.speedtest = speedtest = BTree()
for ic in range(concurrency):
speedtest[ic] = data = BTree()
for ir in range(repetitions):
data[ir] = P()
db.pack()
db.close()
print('Times per operation in microseconds (o=%s, t=%s, c=%s)' % (
object_size, transaction_size, concurrency))
print(' %12s' * 5 % ('op', 'min', 'mean', 'median', 'max'))
queues = [(multiprocessing.Queue(), multiprocessing.Queue())
for ip in range(concurrency)]
if options.save:
save_file = open(options.save, 'a')
else:
save_file = None
if concurrency > 1 or save_file:
processes = [
multiprocessing.Process(
target=run_test,
args=(db_factory, ip, queues[ip][0], queues[ip][1],
object_size, transaction_size, repetitions,
options.read_only),
)
for ip in range(concurrency)
]
for p in processes:
p.daemon = True
p.start()
for iqueue, oqueue in queues:
oqueue.get(timeout=9) # ready?
for name in headings:
for iqueue, oqueue in queues:
iqueue.put(None)
data = [oqueue.get(timeout=999) / repetitions
for _, oqueue in queues]
summarize(name, data)
if save_file:
save_file.write('\t'.join(
map(str,
(options.name,
object_size, transaction_size,
concurrency * options.client_hosts, repetitions,
options.server_sync, options.ssl,
options.demo_storage_baseline,
options.address,
name, sum(data)/len(data))
)
) + '\n')
for p in processes:
p.join(1)
else:
[(iqueue, oqueue)] = queues
for name in headings:
iqueue.put(None)
if options.profile:
import cProfile
profiler = cProfile.Profile()
profiler.enable()
else:
profiler = None
run_test(db_factory, 0, iqueue, oqueue,
object_size, transaction_size, repetitions,
options.read_only)
oqueue.get(timeout=9) # ready?
if profiler is not None:
profiler.disable()
profiler.dump_stats(options.profile)
for name in headings:
summarize(name, [oqueue.get(timeout=999) / repetitions])
stop()
def summarize(name, results):
results = [int(t*1000000) for t in results]
l = len(results)
if l > 1:
print(' %12s' * 5 % (
name, min(results), round(sum(results)/l), results[l//2],
max(results)
))
else:
print(' %12s' * 2 % (name, results[0]))
def run_test(db_factory, process_index, iqueue, oqueue,
object_size, transaction_size, repetitions, read_only):
if db_factory is None:
# No factory. Compute baseline numbers using DemoStorage
db = ZODB.DB(None)
with db.transaction() as conn:
conn.root.speedtest = speedtest = BTree()
speedtest[process_index] = data = BTree()
for ir in range(repetitions):
data[ir] = P()
else:
db = db_factory()
conn = db.open()
data = conn.root.speedtest[process_index]
slots = list(data.values()) # Get data slots loaded
oqueue.put('ready')
if not read_only:
# add test
iqueue.get()
start = time.time()
for slot in slots:
conn.transaction_manager.begin()
for it in range(transaction_size):
p = P()
p.data = 'x' * object_size
slot.children += (p, )
conn.transaction_manager.commit()
conn.transaction_manager.commit()
oqueue.put(time.time() - start)
iqueue.get()
# update test
start = time.time()
for slot in slots:
conn.transaction_manager.begin()
for it in range(transaction_size):
slot.children[it].data = 'y' * object_size
conn.transaction_manager.commit()
oqueue.put(time.time() - start)
iqueue.get()
# hot, With db cache, but not object cache
conn.cacheMinimize()
for slot in slots:
_ = slot.children
start = time.time()
for slot in slots:
conn.transaction_manager.begin()
for it in range(transaction_size):
assert slot.children[it].data == 'y' * object_size
conn.transaction_manager.commit()
oqueue.put(time.time() - start)
if db_factory is None:
return
storage = db.storage
# cold, With no cache
storage._cache.clear()
conn.cacheMinimize()
for slot in slots[:repetitions]:
_ = slot.children
iqueue.get()
start = time.time()
for slot in slots[:repetitions]:
conn.transaction_manager.begin()
for it in range(transaction_size):
assert slot.children[it].data == 'y' * object_size
conn.transaction_manager.commit()
oqueue.put(time.time() - start)
# cold, With no cache, but with prefetch
oids = []
for slot in slots[:repetitions]:
soids = []
oids.append(soids)
for it in range(transaction_size):
soids.append(slot.children[it]._p_oid)
storage._cache.clear()
conn.cacheMinimize()
for slot in slots[:repetitions]:
_ = slot.children
iqueue.get()
start = time.time()
for slot, soids in zip(slots[:repetitions], oids):
conn.transaction_manager.begin()
storage.prefetch(soids, conn._storage._start)
for it in range(transaction_size):
assert slot.children[it].data == 'y' * object_size
conn.transaction_manager.commit()
oqueue.put(time.time() - start)
conn.close()
db.close()
if __name__ == '__main__':
shoot()