forked from mongodb/mongo-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperf_test.py
More file actions
526 lines (389 loc) · 14.4 KB
/
perf_test.py
File metadata and controls
526 lines (389 loc) · 14.4 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# Copyright 2015 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the MongoDB Driver Performance Benchmarking Spec."""
import multiprocessing as mp
import os
import sys
import tempfile
import warnings
try:
import simplejson as json
except ImportError:
import json
sys.path[0:0] = [""]
from bson import decode, encode
from bson.json_util import loads
from gridfs import GridFSBucket
from pymongo import MongoClient
from pymongo.monotonic import time
from test import client_context, host, port, unittest
NUM_ITERATIONS = 100
MAX_ITERATION_TIME = 300
NUM_DOCS = 10000
TEST_PATH = os.environ.get('TEST_PATH', os.path.join(
os.path.dirname(os.path.realpath(__file__)),
os.path.join('data')))
OUTPUT_FILE = os.environ.get('OUTPUT_FILE')
result_data = []
def tearDownModule():
output = json.dumps({
'results': result_data
}, indent=4)
if OUTPUT_FILE:
with open(OUTPUT_FILE, 'w') as opf:
opf.write(output)
else:
print(output)
class Timer(object):
def __enter__(self):
self.start = time()
return self
def __exit__(self, *args):
self.end = time()
self.interval = self.end - self.start
class PerformanceTest(object):
@classmethod
def setUpClass(cls):
client_context.init()
def setUp(self):
pass
def tearDown(self):
name = self.__class__.__name__
median = self.percentile(50)
result = self.data_size / median
print('Running %s. MEDIAN=%s' % (self.__class__.__name__,
self.percentile(50)))
result_data.append({
'name': name,
'results': {
'1': {
'ops_per_sec': result
}
}
})
def before(self):
pass
def after(self):
pass
def percentile(self, percentile):
if hasattr(self, 'results'):
sorted_results = sorted(self.results)
percentile_index = int(len(sorted_results) * percentile / 100) - 1
return sorted_results[percentile_index]
else:
self.fail('Test execution failed')
def runTest(self):
results = []
start = time()
self.max_iterations = NUM_ITERATIONS
for i in range(NUM_ITERATIONS):
if time() - start > MAX_ITERATION_TIME:
warnings.warn('Test timed out, completed %s iterations.' % i)
break
self.before()
with Timer() as timer:
self.do_task()
self.after()
results.append(timer.interval)
self.results = results
# BSON MICRO-BENCHMARKS
class BsonEncodingTest(PerformanceTest):
def setUp(self):
# Location of test data.
with open(
os.path.join(TEST_PATH,
os.path.join('extended_bson', self.dataset))) as data:
self.document = loads(data.read())
def do_task(self):
for _ in range(NUM_DOCS):
encode(self.document)
class BsonDecodingTest(PerformanceTest):
def setUp(self):
# Location of test data.
with open(
os.path.join(TEST_PATH,
os.path.join('extended_bson', self.dataset))) as data:
self.document = encode(json.loads(data.read()))
def do_task(self):
for _ in range(NUM_DOCS):
decode(self.document)
class TestFlatEncoding(BsonEncodingTest, unittest.TestCase):
dataset = 'flat_bson.json'
data_size = 75310000
class TestFlatDecoding(BsonDecodingTest, unittest.TestCase):
dataset = 'flat_bson.json'
data_size = 75310000
class TestDeepEncoding(BsonEncodingTest, unittest.TestCase):
dataset = 'deep_bson.json'
data_size = 19640000
class TestDeepDecoding(BsonDecodingTest, unittest.TestCase):
dataset = 'deep_bson.json'
data_size = 19640000
class TestFullEncoding(BsonEncodingTest, unittest.TestCase):
dataset = 'full_bson.json'
data_size = 57340000
class TestFullDecoding(BsonDecodingTest, unittest.TestCase):
dataset = 'full_bson.json'
data_size = 57340000
# SINGLE-DOC BENCHMARKS
class TestRunCommand(PerformanceTest, unittest.TestCase):
data_size = 160000
def setUp(self):
self.client = client_context.client
self.client.drop_database('perftest')
def do_task(self):
command = self.client.perftest.command
for _ in range(NUM_DOCS):
command("ismaster")
class TestDocument(PerformanceTest):
def setUp(self):
# Location of test data.
with open(
os.path.join(
TEST_PATH, os.path.join(
'single_and_multi_document', self.dataset)), 'r') as data:
self.document = json.loads(data.read())
self.client = client_context.client
self.client.drop_database('perftest')
def tearDown(self):
super(TestDocument, self).tearDown()
self.client.drop_database('perftest')
def before(self):
self.corpus = self.client.perftest.create_collection('corpus')
def after(self):
self.client.perftest.drop_collection('corpus')
class TestFindOneByID(TestDocument, unittest.TestCase):
data_size = 16220000
def setUp(self):
self.dataset = 'tweet.json'
super(TestFindOneByID, self).setUp()
documents = [self.document.copy() for _ in range(NUM_DOCS)]
self.corpus = self.client.perftest.corpus
result = self.corpus.insert_many(documents)
self.inserted_ids = result.inserted_ids
def do_task(self):
find_one = self.corpus.find_one
for _id in self.inserted_ids:
find_one({'_id': _id})
def before(self):
pass
def after(self):
pass
class TestSmallDocInsertOne(TestDocument, unittest.TestCase):
data_size = 2750000
def setUp(self):
self.dataset = 'small_doc.json'
super(TestSmallDocInsertOne, self).setUp()
self.documents = [self.document.copy() for _ in range(NUM_DOCS)]
def do_task(self):
insert_one = self.corpus.insert_one
for doc in self.documents:
insert_one(doc)
class TestLargeDocInsertOne(TestDocument, unittest.TestCase):
data_size = 27310890
def setUp(self):
self.dataset = 'large_doc.json'
super(TestLargeDocInsertOne, self).setUp()
self.documents = [self.document.copy() for _ in range(10)]
def do_task(self):
insert_one = self.corpus.insert_one
for doc in self.documents:
insert_one(doc)
# MULTI-DOC BENCHMARKS
class TestFindManyAndEmptyCursor(TestDocument, unittest.TestCase):
data_size = 16220000
def setUp(self):
self.dataset = 'tweet.json'
super(TestFindManyAndEmptyCursor, self).setUp()
for _ in range(10):
self.client.perftest.command(
'insert', 'corpus',
documents=[self.document] * 1000)
self.corpus = self.client.perftest.corpus
def do_task(self):
list(self.corpus.find())
def before(self):
pass
def after(self):
pass
class TestSmallDocBulkInsert(TestDocument, unittest.TestCase):
data_size = 2750000
def setUp(self):
self.dataset = 'small_doc.json'
super(TestSmallDocBulkInsert, self).setUp()
self.documents = [self.document.copy() for _ in range(NUM_DOCS)]
def before(self):
self.corpus = self.client.perftest.create_collection('corpus')
def do_task(self):
self.corpus.insert_many(self.documents, ordered=True)
class TestLargeDocBulkInsert(TestDocument, unittest.TestCase):
data_size = 27310890
def setUp(self):
self.dataset = 'large_doc.json'
super(TestLargeDocBulkInsert, self).setUp()
self.documents = [self.document.copy() for _ in range(10)]
def before(self):
self.corpus = self.client.perftest.create_collection('corpus')
def do_task(self):
self.corpus.insert_many(self.documents, ordered=True)
class TestGridFsUpload(PerformanceTest, unittest.TestCase):
data_size = 52428800
def setUp(self):
self.client = client_context.client
self.client.drop_database('perftest')
gridfs_path = os.path.join(
TEST_PATH,
os.path.join('single_and_multi_document', 'gridfs_large.bin'))
with open(gridfs_path, 'rb') as data:
self.document = data.read()
self.bucket = GridFSBucket(self.client.perftest)
def tearDown(self):
super(TestGridFsUpload, self).tearDown()
self.client.drop_database('perftest')
def before(self):
self.bucket.upload_from_stream('init', b'x')
def do_task(self):
self.bucket.upload_from_stream('gridfstest', self.document)
class TestGridFsDownload(PerformanceTest, unittest.TestCase):
data_size = 52428800
def setUp(self):
self.client = client_context.client
self.client.drop_database('perftest')
gridfs_path = os.path.join(
TEST_PATH,
os.path.join('single_and_multi_document', 'gridfs_large.bin'))
self.bucket = GridFSBucket(self.client.perftest)
with open(gridfs_path, 'rb') as gfile:
self.uploaded_id = self.bucket.upload_from_stream(
'gridfstest', gfile)
def tearDown(self):
super(TestGridFsDownload, self).tearDown()
self.client.drop_database('perftest')
def do_task(self):
self.bucket.open_download_stream(self.uploaded_id).read()
proc_client = None
def proc_init(*dummy):
global proc_client
proc_client = MongoClient(host, port)
# PARALLEL BENCHMARKS
def mp_map(map_func, files):
pool = mp.Pool(initializer=proc_init)
pool.map(map_func, files)
pool.close()
def insert_json_file(filename):
with open(filename, 'r') as data:
coll = proc_client.perftest.corpus
coll.insert_many([json.loads(line) for line in data])
def insert_json_file_with_file_id(filename):
documents = []
with open(filename, 'r') as data:
for line in data:
doc = json.loads(line)
doc['file'] = filename
documents.append(doc)
coll = proc_client.perftest.corpus
coll.insert_many(documents)
def read_json_file(filename):
coll = proc_client.perftest.corpus
temp = tempfile.TemporaryFile()
try:
temp.writelines(
[json.dumps(doc) + '\n' for
doc in coll.find({'file': filename}, {'_id': False})])
finally:
temp.close()
def insert_gridfs_file(filename):
bucket = GridFSBucket(proc_client.perftest)
with open(filename, 'rb') as gfile:
bucket.upload_from_stream(filename, gfile)
def read_gridfs_file(filename):
bucket = GridFSBucket(proc_client.perftest)
temp = tempfile.TemporaryFile()
try:
bucket.download_to_stream_by_name(filename, temp)
finally:
temp.close()
class TestJsonMultiImport(PerformanceTest, unittest.TestCase):
data_size = 565000000
def setUp(self):
self.client = client_context.client
self.client.drop_database('perftest')
def before(self):
self.client.perftest.command({'create': 'corpus'})
self.corpus = self.client.perftest.corpus
ldjson_path = os.path.join(
TEST_PATH, os.path.join('parallel', 'ldjson_multi'))
self.files = [os.path.join(
ldjson_path, s) for s in os.listdir(ldjson_path)]
def do_task(self):
mp_map(insert_json_file, self.files)
def after(self):
self.client.perftest.drop_collection('corpus')
def tearDown(self):
super(TestJsonMultiImport, self).tearDown()
self.client.drop_database('perftest')
class TestJsonMultiExport(PerformanceTest, unittest.TestCase):
data_size = 565000000
def setUp(self):
self.client = client_context.client
self.client.drop_database('perftest')
self.client.perfest.corpus.create_index('file')
ldjson_path = os.path.join(
TEST_PATH, os.path.join('parallel', 'ldjson_multi'))
self.files = [os.path.join(
ldjson_path, s) for s in os.listdir(ldjson_path)]
mp_map(insert_json_file_with_file_id, self.files)
def do_task(self):
mp_map(read_json_file, self.files)
def tearDown(self):
super(TestJsonMultiExport, self).tearDown()
self.client.drop_database('perftest')
class TestGridFsMultiFileUpload(PerformanceTest, unittest.TestCase):
data_size = 262144000
def setUp(self):
self.client = client_context.client
self.client.drop_database('perftest')
def before(self):
self.client.perftest.drop_collection('fs.files')
self.client.perftest.drop_collection('fs.chunks')
self.bucket = GridFSBucket(self.client.perftest)
gridfs_path = os.path.join(
TEST_PATH, os.path.join('parallel', 'gridfs_multi'))
self.files = [os.path.join(
gridfs_path, s) for s in os.listdir(gridfs_path)]
def do_task(self):
mp_map(insert_gridfs_file, self.files)
def tearDown(self):
super(TestGridFsMultiFileUpload, self).tearDown()
self.client.drop_database('perftest')
class TestGridFsMultiFileDownload(PerformanceTest, unittest.TestCase):
data_size = 262144000
def setUp(self):
self.client = client_context.client
self.client.drop_database('perftest')
bucket = GridFSBucket(self.client.perftest)
gridfs_path = os.path.join(
TEST_PATH, os.path.join('parallel', 'gridfs_multi'))
self.files = [os.path.join(
gridfs_path, s) for s in os.listdir(gridfs_path)]
for fname in self.files:
with open(fname, 'rb') as gfile:
bucket.upload_from_stream(fname, gfile)
def do_task(self):
mp_map(read_gridfs_file, self.files)
def tearDown(self):
super(TestGridFsMultiFileDownload, self).tearDown()
self.client.drop_database('perftest')
if __name__ == "__main__":
unittest.main()