-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKVBench.py
executable file
·439 lines (370 loc) · 13 KB
/
KVBench.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
#!/usr/bin/env python
import uuid
import random
import threading
import json
import requests
import time
import sys
__author__ = "David Amies"
__copyright__ = "Copyright 2015, David Amies"
__credits__ = ["David Amies"]
__license__ = "GPL"
__version__ = "1.0.3"
__maintainer__ = "David Amies"
__email__ = "[email protected]"
__status__ = "Beta"
PrettyName = "KV Bench v"+__version__
finalkey = "All"
# top 100 nouns
nouns = ["time","issue","year","side","people","kind","way","head","day","house","man","service","thing","friend","woman",
"father","life","power","child","hour","world","game","school","line","state","end","family","member","student","law",
"group","car","country","city","problem","community","hand","name","part","president","place","team","case","minute",
"week","idea","company","kid","system","body","program","information","question","back","work","parent","government",
"face","number","others","night","level","Mr","office","point","door","home","health","water","person","room","art",
"mother","war","area","history","money","party","storey","result","fact","change","month","morning","lot","reason",
"right","research","study","girl","book","guy","eye","food","job","moment","word","air","business","teacher"]
threads = []
results = {}
bresults = []
sleephi = 1.0
sleeplo = 0.1
runDataPrime = True
runPhase1 = True
runPhase2 = True
runPhase3 = True
host = "http://127.0.0.1:5984"
sess = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=5000, pool_maxsize=5000)
sess.mount("http://", adapter)
gTimeout = 1200
urlPrefix = "/kvbench/"
url31SecondsSuffix = '_design/KVB/_view/seconds?reduce=false&startkey="{StartNum}"&endkey="{EndNum}"'
url32SummListSuffix = '_design/KVB/_view/summary?group=true'
url33SummarySuffix = '_design/KVB/_view/summary?reduce=false&startkey="{Summary}"&endkey="{Summary}"'
def saveresult(phase, type, result):
global results
if phase not in results:
results[phase] = {}
if type not in results[phase]:
results[phase][type] = []
if finalkey not in results[phase]:
results[phase][finalkey] = []
results[phase][finalkey].append(result)
results[phase][type].append(result)
def GenerateSummary():
# >>> random.sample([1, 2, 3, 4, 5], 3) # Choose 3 elements
# [4, 1, 5]
# (1000*50)/100^2=5
# (1000*50)/100^3=0.05
# (2000*50)/100^2=10
# wordcount = random.randint(2, 3)
wordcount = 2
summarywords = random.sample(nouns, wordcount)
summary = " ".join(summarywords)
return summary
def GenerateDescription():
# >>> random.sample([1, 2, 3, 4, 5], 3) # Choose 3 elements
# [4, 1, 5]
paras = []
paracount = random.randint(0, 5)
for i in range(paracount):
wordcount = random.randint(10, 90)
summarywords = random.sample(nouns, wordcount)
summary = " ".join(summarywords)
paras.append(summary)
description = "\r\n".join(paras)
return description
def StoreKV(threadid, phase):
uu = uuid.uuid4()
# Key: KVB_<ThreadId>_<UUID>
key = 'KVB_'+str(threadid)+'_'+str(uu)
val = {}
val["key"] = key
val["seconds"] = random.uniform(0, 30)
val["summary"] = GenerateSummary()
val["description"] = GenerateDescription()
# print key
# print json.dumps(val)
url = host+urlPrefix+key
# url = urlPrefix+key
starttime = time.time()
# r = requests.put(url, data=json.dumps(val))
r = sess.put(url, data=json.dumps(val), timeout=gTimeout)
endtime = time.time()
saveresult(phase, "Write", endtime-starttime)
retdata = r.json()
def ReadKV(threadid, phase, key):
url = host+urlPrefix+key
# url = urlPrefix+key
starttime = time.time()
# r = requests.get(url)
r = sess.get(url, timeout=gTimeout)
endtime = time.time()
saveresult(phase, "Read", endtime-starttime)
retdata = r.json()
def getSecondsKVs(threadid, phase):
StartNum = 0
EndNum = 9
num0 = random.randint(StartNum, EndNum)
if num0>3:
StartNum = num0-1
EndNum = num0
else:
StartNum = num0
EndNum = num0+1
# print "url31SecondsSuffix: "+url31SecondsSuffix
urlSuffix = url31SecondsSuffix
# print "urlSuffix: "+urlSuffix
urlSuffix = urlSuffix.replace("{StartNum}", str(StartNum))
# print "urlSuffix: "+urlSuffix
urlSuffix = urlSuffix.replace("{EndNum}", str(EndNum))
# print "urlSuffix: "+urlSuffix
url = host+urlPrefix+urlSuffix
# url = urlPrefix+urlSuffix
# print "url: "+url
starttime = time.time()
# r = requests.get(url)
r = sess.get(url, timeout=gTimeout)
endtime = time.time()
saveresult(phase, "Query 3.1", endtime-starttime)
# print "r.json():"
# print r.json()
# print "json.loads(r):"
# print json.loads(r.json())
retdata = r.json()
return retdata
def getSummaryKVs(threadid, phase, summary):
urlSuffix = url33SummarySuffix
urlSuffix = urlSuffix.replace("{Summary}", summary)
url = host+urlPrefix+urlSuffix
# url = urlPrefix+urlSuffix
# print "url: "+url
starttime = time.time()
# r = requests.get(url)
r = sess.get(url, timeout=gTimeout)
endtime = time.time()
saveresult(phase, "Query 3.3", endtime-starttime)
retdata = r.json()
return retdata
def getSummaryList(threadid, phase):
url = host+urlPrefix+url32SummListSuffix
# url = urlPrefix+url32SummListSuffix
# print "url: "+url
starttime = time.time()
# r = requests.get(url)
r = sess.get(url, timeout=gTimeout)
endtime = time.time()
saveresult(phase, "Query 3.2", endtime-starttime)
retdata = r.json()
return retdata
def DataPrimeThread(threadid):
for i in range(2000):
StoreKV(threadid, "DataPrime")
# time.sleep(random.uniform(0.5, 1.5))
def DataPrime(threadcount):
starttime = time.time()
print "Started DataPrime("+str(threadcount)+") "+time.strftime("%x %X %Z")
for i in range(threadcount):
t = threading.Thread(target=DataPrimeThread, args=([i]))
t.start()
threads.append(t)
# print "Started thread :"+str(i)
for t in threads:
if t.isAlive():
t.join()
print "Finished DataPrime("+str(threadcount)+") "+time.strftime("%x %X %Z")
endtime = time.time()
print "DataPrime took "+str(endtime-starttime)+" Seconds"
def Phase1Thread(threadid):
phase = "Phase 1"
for i in range(10):
# 1. Write 3 new Key Value pairs.
for j in range(3):
time.sleep(random.uniform(sleeplo, sleephi))
StoreKV(threadid, phase)
# 2. Quety a list of documents based on seconds is between 2 values (3.1)
# print "sleep"
time.sleep(random.uniform(sleeplo, sleephi))
# print "getSecondsKVs"
ret = getSecondsKVs(threadid, phase)
# 3. Select 2 documents at random from list and retreive them
if "rows" not in ret:
print ret
randret = random.sample(ret["rows"], 1)
# print randret
# print randret[0]['id']
time.sleep(random.uniform(sleeplo, sleephi))
ReadKV(threadid, phase, randret[0]['id'])
# 4. Write 3 new Key Value pairs.
for j in range(3):
time.sleep(random.uniform(sleeplo, sleephi))
StoreKV(threadid, phase)
# 5. Quety a list of valid values for summary (3.2)
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSummaryList(threadid, phase)
if "rows" not in ret:
print ret
# 6. Write 3 new Key Value pairs.
for j in range(3):
time.sleep(random.uniform(sleeplo, sleephi))
StoreKV(threadid, phase)
# 7. Quety a list of documents based on summary selected at random from previous list (3.3)
randret = random.sample(ret["rows"], 1)
# print randret
# print randret[0]['key']
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSummaryKVs(threadid, phase, randret[0]['key'])
if "rows" not in ret:
print ret
# 8. Select 2 documents at random from list and retreive them
randret = random.sample(ret["rows"], 2)
# print randret
# print randret[0]['id']
for j in range(2):
time.sleep(random.uniform(sleeplo, sleephi))
ReadKV(threadid, phase, randret[j]['id'])
# 9. Write 3 new Key Value pairs.
for j in range(3):
time.sleep(random.uniform(sleeplo, sleephi))
StoreKV(threadid, phase)
# print "Phase1Thread("+str(threadid)+") completed i: "+str(i)+" "++time.strftime("%x %X %Z")
sys.stdout.write(".")
def Phase1(threadcount):
starttime = time.time()
print "Started Phase 1("+str(threadcount)+") "+time.strftime("%x %X %Z")
for i in range(threadcount):
t = threading.Thread(target=Phase1Thread, args=([i]))
t.start()
threads.append(t)
time.sleep(1)
# print "Started thread :"+str(i)
for t in threads:
if t.isAlive():
t.join()
print "."
print "Finished Phase 1("+str(threadcount)+") "+time.strftime("%x %X %Z")
endtime = time.time()
print "Phase 1 took "+str(endtime-starttime)+" Seconds"
def Phase2Thread(threadid):
phase = "Phase 2"
for i in range(10):
# 1. Write a new Key Value pair.
StoreKV(threadid, phase)
# 2. Quety a list of documents based on seconds is between 2 values (3.1) x2
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSecondsKVs(threadid, phase)
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSecondsKVs(threadid, phase)
# 3. Select a document at random from list and retreive it
if "rows" not in ret:
print ret
randret = random.sample(ret["rows"], 1)
time.sleep(random.uniform(sleeplo, sleephi))
ReadKV(threadid, phase, randret[0]['id'])
# 4. Quety a list of valid values for summary (3.2) x3
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSummaryList(threadid, phase)
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSummaryList(threadid, phase)
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSummaryList(threadid, phase)
if "rows" not in ret:
print ret
# 5. Quety a list of documents based on summary selected at random from previous list (3.3)
randret = random.sample(ret["rows"], 1)
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSummaryKVs(threadid, phase, randret[0]['key'])
if "rows" not in ret:
print ret
# 6. Select a document at random from list and retreive it
randret = random.sample(ret["rows"], 1)
time.sleep(random.uniform(sleeplo, sleephi))
ReadKV(threadid, phase, randret[0]['id'])
sys.stdout.write(".")
def Phase2(threadcount):
starttime = time.time()
print "Started Phase 2("+str(threadcount)+") "+time.strftime("%x %X %Z")
for i in range(threadcount):
t = threading.Thread(target=Phase2Thread, args=([i]))
t.start()
threads.append(t)
time.sleep(1)
# print "Started thread :"+str(i)
for t in threads:
if t.isAlive():
t.join()
print "."
print "Finished Phase 2("+str(threadcount)+") "+time.strftime("%x %X %Z")
endtime = time.time()
print "Phase 2 took "+str(endtime-starttime)+" Seconds"
def Phase3Thread(threadid):
phase = "Phase 3"
for i in range(10):
# 1. Write a new Key Value pair.
StoreKV(threadid, phase)
# 2. Quety a list of documents based on seconds is between 2 values (3.1)
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSecondsKVs(threadid, phase)
# 3. Select 5 documents at random from list and retreive them
if "rows" not in ret:
print ret
randret = random.sample(ret["rows"], 5)
for j in range(5):
time.sleep(random.uniform(sleeplo, sleephi))
ReadKV(threadid, phase, randret[j]['id'])
# 5. Quety a list of valid values for summary (3.2)
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSummaryList(threadid, phase)
if "rows" not in ret:
print ret
# 5. Quety a list of documents based on summary selected at random from previous list (3.3)
randret = random.sample(ret["rows"], 1)
time.sleep(random.uniform(sleeplo, sleephi))
ret = getSummaryKVs(threadid, phase, randret[0]['key'])
if "rows" not in ret:
print ret
# 6. Select 5 documents at random from list and retreive them
randret = random.sample(ret["rows"], 5)
for j in range(5):
time.sleep(random.uniform(sleeplo, sleephi))
ReadKV(threadid, phase, randret[j]['id'])
sys.stdout.write(".")
def Phase3(threadcount):
starttime = time.time()
print "Started Phase 3("+str(threadcount)+") "+time.strftime("%x %X %Z")
for i in range(threadcount):
t = threading.Thread(target=Phase3Thread, args=([i]))
t.start()
threads.append(t)
time.sleep(1)
# print "Started thread :"+str(i)
for t in threads:
if t.isAlive():
t.join()
print "."
print "Finished Phase 3("+str(threadcount)+") "+time.strftime("%x %X %Z")
endtime = time.time()
print "Phase 3 took "+str(endtime-starttime)+" Seconds"
if runDataPrime:
DataPrime(100)
if runPhase1:
Phase1(50)
if runPhase2:
Phase2(50)
if runPhase3:
Phase3(50)
if "DataPrime" in results:
del results["DataPrime"]
# print results
skeys1 = results.keys()
skeys1.sort()
for k1 in skeys1:
print k1
if finalkey in results[k1]:
bresults.append(sum(results[k1][finalkey])/float(len(results[k1][finalkey])))
skeys2 = results[k1].keys()
skeys2.sort()
for k2 in skeys2:
print " "+k2 +" : "+ str(sum(results[k1][k2])/float(len(results[k1][k2])))
print PrettyName+" score: "+str(sum(bresults)/float(len(bresults)))