-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbench.py
executable file
·64 lines (58 loc) · 1.78 KB
/
bench.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
import random
import argparse
from time import time
from pymongo import Connection, ASCENDING
parser = argparse.ArgumentParser(description='Benchmark mongo')
parser.add_argument('--keys', type=int,
help='Number of keys to put in to Mongo')
parser.add_argument('--host',
help='Host to connect to')
parser.add_argument('--gets', type=int,
help='Number of gets to perform')
parser.add_argument('--focus', type=int,
help='Percentage to focus reads on')
parser.add_argument('--do_insert', const=True, default=False,
action='store_const',
help='Do inserts')
args = parser.parse_args()
host = args.host
keys = args.keys
gets = args.gets
focus = args.focus
do_insert = args.do_insert
# Connect to mongo
connection = Connection(host)
db = connection.bench
# Add all the keys if desired
if do_insert:
db.drop_collection('docs')
docs = db.docs
docs.create_index([('key', ASCENDING)])
batch_size = 100
i = 0
start = time()
while i < keys:
to_insert = []
for j in range(batch_size):
to_insert.append({
'key': i,
'text': 'Mary had a little lamb. '*100
})
i += 1
docs.insert(to_insert)
i += batch_size
end = time()
print '%d documents inserted took %.2fs' % (keys, end - start)
else:
docs = db.docs
# Now get the keys in a random order
start = time()
for i in range(gets):
focussed_get = random.randint(0, 100) != 0
if focussed_get:
key = random.randint(0, keys * focus / 100.0)
else:
key = random.randint(0, keys)
docs.find_one({'key': key})
end = time()
print '%d gets (focussed on bottom %d%%) took %.2fs' % (gets, focus, end - start)