This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbenchmark.py
229 lines (176 loc) · 6.41 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
import argparse
import collections
import math
import string
import sys
import timeit
from functools import partial
import pymongo
import numpy as np
from bson import BSON, CodecOptions, Int64, ObjectId
from bson.raw_bson import RawBSONDocument
try:
import bsonnumpy
except (ImportError, OSError) as exc:
print(exc)
bsonnumpy = None
try:
import monary
except (ImportError, OSError) as exc:
monary = None
assert pymongo.has_c()
# Use large document in tests? If SMALL, no, if LARGE, then yes.
SMALL = False
LARGE = True
db = None
raw_bson = None
large_doc_keys = None
collection_names = {LARGE: "large", SMALL: "small"}
dtypes = {}
raw_bsons = {}
def _setup():
global db
global raw_bson
global large_doc_keys
db = pymongo.MongoClient().bsonnumpy_test
small = db[collection_names[SMALL]]
small.drop()
print("%d small docs, %d bytes each with 3 keys" % (
N_SMALL_DOCS,
len(BSON.encode({'_id': ObjectId(), 'x': 1, 'y': math.pi}))))
small.insert_many([
collections.OrderedDict([('x', 1), ('y', math.pi)])
for _ in range(N_SMALL_DOCS)])
dtypes[SMALL] = np.dtype([('x', np.int64), ('y', np.float64)])
large = db[collection_names[LARGE]]
large.drop()
# 2600 keys: 'a', 'aa', 'aaa', .., 'zz..z'
large_doc_keys = [c * i for c in string.ascii_lowercase
for i in range(1, 101)]
large_doc = collections.OrderedDict([(k, math.pi) for k in large_doc_keys])
print("%d large docs, %dk each with %d keys" % (
N_LARGE_DOCS, len(BSON.encode(large_doc)) // 1024, len(large_doc_keys)))
large.insert_many([large_doc.copy() for _ in range(N_LARGE_DOCS)])
dtypes[LARGE] = np.dtype([(k, np.float64) for k in large_doc_keys])
# Ignore for now that the first batch defaults to 101 documents.
raw_bson_docs_small = [{'x': 1, 'y': math.pi} for _ in range(N_SMALL_DOCS)]
raw_bson_small = BSON.encode({'ok': 1,
'cursor': {
'id': Int64(1234),
'ns': 'db.collection',
'firstBatch': raw_bson_docs_small}})
raw_bson_docs_large = [large_doc.copy() for _ in range(N_LARGE_DOCS)]
raw_bson_large = BSON.encode({'ok': 1,
'cursor': {
'id': Int64(1234),
'ns': 'db.collection',
'firstBatch': raw_bson_docs_large}})
raw_bsons[SMALL] = raw_bson_small
raw_bsons[LARGE] = raw_bson_large
def _teardown():
db.collection.drop()
bench_fns = collections.OrderedDict()
def bench(name):
def assign_name(fn):
bench_fns[name] = fn
return fn
return assign_name
@bench('conventional-to-ndarray')
def conventional_func(use_large):
collection = db[collection_names[use_large]]
cursor = collection.find()
dtype = dtypes[use_large]
if use_large:
np.array([tuple(doc[k] for k in large_doc_keys) for doc in cursor],
dtype=dtype)
else:
np.array([(doc['x'], doc['y']) for doc in cursor], dtype=dtype)
@bench('raw-bson-to-ndarray')
def bson_numpy_func(use_large):
raw_coll = db.get_collection(
collection_names[use_large],
codec_options=CodecOptions(document_class=RawBSONDocument))
cursor = raw_coll.find()
dtype = dtypes[use_large]
bsonnumpy.sequence_to_ndarray(
(doc.raw for doc in cursor), dtype, raw_coll.count())
@bench('raw-batches-to-ndarray')
def raw_bson_func(use_large):
c = db[collection_names[use_large]]
if not hasattr(c, 'find_raw_batches'):
print("Wrong PyMongo: no 'find_raw_batches' feature")
return
dtype = dtypes[use_large]
bsonnumpy.sequence_to_ndarray(c.find_raw_batches(), dtype, c.count())
@bench('monary')
def monary_func(use_large):
# Monary doesn't allow > 1024 keys, and it's too slow to benchmark anyway.
if use_large:
return
m = monary.Monary()
dtype = dtypes[use_large]
m.query(db.name, collection_names[use_large], {}, dtype.names,
["float64"] * len(dtype.names))
@bench('parse-dtype')
def raw_bson_func(use_large):
dtype = dtypes[use_large]
bsonnumpy.sequence_to_ndarray([], dtype, 0)
@bench('decoded-cmd-reply')
def bson_func(use_large):
for _ in BSON(raw_bsons[use_large]).decode()['cursor']['firstBatch']:
pass
@bench('raw-cmd-reply')
def raw_bson_func(use_large):
options = CodecOptions(document_class=RawBSONDocument)
for _ in BSON(raw_bsons[use_large]).decode(options)['cursor']['firstBatch']:
pass
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
epilog="""
Available benchmark functions:
%s
""" % ("\n ".join(bench_fns.keys()),))
parser.add_argument('--large', action='store_true',
help='only test with large documents')
parser.add_argument('--small', action='store_true',
help='only test with small documents')
parser.add_argument('--test', action='store_true',
help='quick test of benchmark.py')
parser.add_argument('funcs', nargs='*', default=bench_fns.keys())
options = parser.parse_args()
if options.test:
N_LARGE_DOCS = 2
N_SMALL_DOCS = 2
N_TRIALS = 1
else:
N_LARGE_DOCS = 1000
N_SMALL_DOCS = 100000
N_TRIALS = 5
# Run tests with both small and large documents.
sizes = [SMALL, LARGE]
if options.large and not options.small:
sizes.remove(SMALL)
if options.small and not options.large:
sizes.remove(LARGE)
for name in options.funcs:
if name not in bench_fns:
sys.stderr.write("Unknown function \"%s\"\n" % name)
sys.stderr.write("Available functions:\n%s\n" % ("\n".join(bench_fns)))
sys.exit(1)
_setup()
print()
print("%25s: %7s %7s" % ("BENCH", "SMALL", "LARGE"))
for name, fn in bench_fns.items():
if name in options.funcs:
sys.stdout.write("%25s: " % name)
sys.stdout.flush()
# Test with small and large documents.
for size in (SMALL, LARGE):
if size not in sizes:
sys.stdout.write("%7s" % "-")
else:
timer = timeit.Timer(partial(fn, size))
duration = min(timer.repeat(3, N_TRIALS)) / float(N_TRIALS)
sys.stdout.write("%7.2f " % duration)
sys.stdout.flush()
sys.stdout.write("\n")
_teardown()