21
21
import tempfile
22
22
import time
23
23
import warnings
24
- from typing import Any , List
24
+ from typing import Any , List , Optional
25
25
26
26
try :
27
27
import simplejson as json
@@ -70,9 +70,8 @@ def __exit__(self, *args):
70
70
71
71
72
72
class PerformanceTest :
73
- dataset : Any
74
- data_size : Any
75
- do_task : Any
73
+ dataset : str
74
+ data_size : int
76
75
fail : Any
77
76
78
77
@classmethod
@@ -87,7 +86,9 @@ def tearDown(self):
87
86
name = self .__class__ .__name__ [4 :]
88
87
median = self .percentile (50 )
89
88
megabytes_per_sec = self .data_size / median / 1000000
90
- print (f"Running { self .__class__ .__name__ } . MEDIAN={ self .percentile (50 )} " )
89
+ print (
90
+ f"Running { self .__class__ .__name__ } . MB/s={ megabytes_per_sec } , MEDIAN={ self .percentile (50 )} "
91
+ )
91
92
result_data .append (
92
93
{
93
94
"info" : {
@@ -105,6 +106,9 @@ def tearDown(self):
105
106
def before (self ):
106
107
pass
107
108
109
+ def do_task (self ):
110
+ raise NotImplementedError
111
+
108
112
def after (self ):
109
113
pass
110
114
@@ -120,12 +124,13 @@ def percentile(self, percentile):
120
124
def runTest (self ):
121
125
results = []
122
126
start = time .monotonic ()
123
- self .max_iterations = NUM_ITERATIONS
124
127
for i in range (NUM_ITERATIONS ):
125
128
if time .monotonic () - start > MAX_ITERATION_TIME :
126
129
with warnings .catch_warnings ():
127
130
warnings .simplefilter ("default" )
128
- warnings .warn ("Test timed out, completed %s iterations." % i )
131
+ warnings .warn (
132
+ f"Test timed out after { MAX_ITERATION_TIME } s, completed { i } /{ NUM_ITERATIONS } iterations."
133
+ )
129
134
break
130
135
self .before ()
131
136
with Timer () as timer :
@@ -142,6 +147,7 @@ def setUp(self):
142
147
# Location of test data.
143
148
with open (os .path .join (TEST_PATH , os .path .join ("extended_bson" , self .dataset ))) as data :
144
149
self .document = loads (data .read ())
150
+ self .data_size = len (encode (self .document )) * NUM_DOCS
145
151
146
152
def do_task (self ):
147
153
for _ in range (NUM_DOCS ):
@@ -154,44 +160,40 @@ def setUp(self):
154
160
with open (os .path .join (TEST_PATH , os .path .join ("extended_bson" , self .dataset ))) as data :
155
161
self .document = encode (json .loads (data .read ()))
156
162
163
+ self .data_size = len (self .document ) * NUM_DOCS
164
+
157
165
def do_task (self ):
158
166
for _ in range (NUM_DOCS ):
159
167
decode (self .document )
160
168
161
169
162
170
class TestFlatEncoding (BsonEncodingTest , unittest .TestCase ):
163
171
dataset = "flat_bson.json"
164
- data_size = 75310000
165
172
166
173
167
174
class TestFlatDecoding (BsonDecodingTest , unittest .TestCase ):
168
175
dataset = "flat_bson.json"
169
- data_size = 75310000
170
176
171
177
172
178
class TestDeepEncoding (BsonEncodingTest , unittest .TestCase ):
173
179
dataset = "deep_bson.json"
174
- data_size = 19640000
175
180
176
181
177
182
class TestDeepDecoding (BsonDecodingTest , unittest .TestCase ):
178
183
dataset = "deep_bson.json"
179
- data_size = 19640000
180
184
181
185
182
186
class TestFullEncoding (BsonEncodingTest , unittest .TestCase ):
183
187
dataset = "full_bson.json"
184
- data_size = 57340000
185
188
186
189
187
190
class TestFullDecoding (BsonDecodingTest , unittest .TestCase ):
188
191
dataset = "full_bson.json"
189
- data_size = 57340000
190
192
191
193
192
194
# SINGLE-DOC BENCHMARKS
193
195
class TestRunCommand (PerformanceTest , unittest .TestCase ):
194
- data_size = 160000
196
+ data_size = len ( encode ({ "hello" : True })) * NUM_DOCS
195
197
196
198
def setUp (self ):
197
199
self .client = client_context .client
@@ -200,7 +202,7 @@ def setUp(self):
200
202
def do_task (self ):
201
203
command = self .client .perftest .command
202
204
for _ in range (NUM_DOCS ):
203
- command ("ping" )
205
+ command ("hello" , True )
204
206
205
207
206
208
class TestDocument (PerformanceTest ):
@@ -225,116 +227,83 @@ def after(self):
225
227
self .client .perftest .drop_collection ("corpus" )
226
228
227
229
228
- class TestFindOneByID (TestDocument , unittest . TestCase ):
229
- data_size = 16220000
230
+ class FindTest (TestDocument ):
231
+ dataset = "tweet.json"
230
232
231
233
def setUp (self ):
232
- self .dataset = "tweet.json"
233
234
super ().setUp ()
234
-
235
+ self . data_size = len ( encode ( self . document )) * NUM_DOCS
235
236
documents = [self .document .copy () for _ in range (NUM_DOCS )]
236
237
self .corpus = self .client .perftest .corpus
237
238
result = self .corpus .insert_many (documents )
238
239
self .inserted_ids = result .inserted_ids
239
240
240
- def do_task (self ):
241
- find_one = self .corpus .find_one
242
- for _id in self .inserted_ids :
243
- find_one ({"_id" : _id })
244
-
245
241
def before (self ):
246
242
pass
247
243
248
244
def after (self ):
249
245
pass
250
246
251
247
252
- class TestSmallDocInsertOne (TestDocument , unittest .TestCase ):
253
- data_size = 2750000
248
+ class TestFindOneByID (FindTest , unittest .TestCase ):
249
+ def do_task (self ):
250
+ find_one = self .corpus .find_one
251
+ for _id in self .inserted_ids :
252
+ find_one ({"_id" : _id })
253
+
254
+
255
+ class SmallDocInsertTest (TestDocument ):
256
+ dataset = "small_doc.json"
254
257
255
258
def setUp (self ):
256
- self .dataset = "small_doc.json"
257
259
super ().setUp ()
258
-
260
+ self . data_size = len ( encode ( self . document )) * NUM_DOCS
259
261
self .documents = [self .document .copy () for _ in range (NUM_DOCS )]
260
262
263
+
264
+ class TestSmallDocInsertOne (SmallDocInsertTest , unittest .TestCase ):
261
265
def do_task (self ):
262
266
insert_one = self .corpus .insert_one
263
267
for doc in self .documents :
264
268
insert_one (doc )
265
269
266
270
267
- class TestLargeDocInsertOne (TestDocument , unittest . TestCase ):
268
- data_size = 27310890
271
+ class LargeDocInsertTest (TestDocument ):
272
+ dataset = "large_doc.json"
269
273
270
274
def setUp (self ):
271
- self .dataset = "large_doc.json"
272
275
super ().setUp ()
276
+ n_docs = 10
277
+ self .data_size = len (encode (self .document )) * n_docs
278
+ self .documents = [self .document .copy () for _ in range (n_docs )]
273
279
274
- self .documents = [self .document .copy () for _ in range (10 )]
275
280
281
+ class TestLargeDocInsertOne (LargeDocInsertTest , unittest .TestCase ):
276
282
def do_task (self ):
277
283
insert_one = self .corpus .insert_one
278
284
for doc in self .documents :
279
285
insert_one (doc )
280
286
281
287
282
288
# MULTI-DOC BENCHMARKS
283
- class TestFindManyAndEmptyCursor (TestDocument , unittest .TestCase ):
284
- data_size = 16220000
285
-
286
- def setUp (self ):
287
- self .dataset = "tweet.json"
288
- super ().setUp ()
289
-
290
- for _ in range (10 ):
291
- self .client .perftest .command ("insert" , "corpus" , documents = [self .document ] * 1000 )
292
- self .corpus = self .client .perftest .corpus
293
-
289
+ class TestFindManyAndEmptyCursor (FindTest , unittest .TestCase ):
294
290
def do_task (self ):
295
291
list (self .corpus .find ())
296
292
297
- def before (self ):
298
- pass
299
-
300
- def after (self ):
301
- pass
302
-
303
-
304
- class TestSmallDocBulkInsert (TestDocument , unittest .TestCase ):
305
- data_size = 2750000
306
-
307
- def setUp (self ):
308
- self .dataset = "small_doc.json"
309
- super ().setUp ()
310
- self .documents = [self .document .copy () for _ in range (NUM_DOCS )]
311
-
312
- def before (self ):
313
- self .corpus = self .client .perftest .create_collection ("corpus" )
314
293
294
+ class TestSmallDocBulkInsert (SmallDocInsertTest , unittest .TestCase ):
315
295
def do_task (self ):
316
296
self .corpus .insert_many (self .documents , ordered = True )
317
297
318
298
319
- class TestLargeDocBulkInsert (TestDocument , unittest .TestCase ):
320
- data_size = 27310890
321
-
322
- def setUp (self ):
323
- self .dataset = "large_doc.json"
324
- super ().setUp ()
325
- self .documents = [self .document .copy () for _ in range (10 )]
326
-
327
- def before (self ):
328
- self .corpus = self .client .perftest .create_collection ("corpus" )
329
-
299
+ class TestLargeDocBulkInsert (LargeDocInsertTest , unittest .TestCase ):
330
300
def do_task (self ):
331
301
self .corpus .insert_many (self .documents , ordered = True )
332
302
333
303
334
- class TestGridFsUpload (PerformanceTest , unittest .TestCase ):
335
- data_size = 52428800
336
-
304
+ class GridFsTest (PerformanceTest ):
337
305
def setUp (self ):
306
+ super ().setUp ()
338
307
self .client = client_context .client
339
308
self .client .drop_database ("perftest" )
340
309
@@ -343,44 +312,33 @@ def setUp(self):
343
312
)
344
313
with open (gridfs_path , "rb" ) as data :
345
314
self .document = data .read ()
346
-
315
+ self . data_size = len ( self . document )
347
316
self .bucket = GridFSBucket (self .client .perftest )
348
317
349
318
def tearDown (self ):
350
319
super ().tearDown ()
351
320
self .client .drop_database ("perftest" )
352
321
322
+
323
+ class TestGridFsUpload (GridFsTest , unittest .TestCase ):
353
324
def before (self ):
325
+ # Create the bucket.
354
326
self .bucket .upload_from_stream ("init" , b"x" )
355
327
356
328
def do_task (self ):
357
329
self .bucket .upload_from_stream ("gridfstest" , self .document )
358
330
359
331
360
- class TestGridFsDownload (PerformanceTest , unittest .TestCase ):
361
- data_size = 52428800
362
-
332
+ class TestGridFsDownload (GridFsTest , unittest .TestCase ):
363
333
def setUp (self ):
364
- self .client = client_context .client
365
- self .client .drop_database ("perftest" )
366
-
367
- gridfs_path = os .path .join (
368
- TEST_PATH , os .path .join ("single_and_multi_document" , "gridfs_large.bin" )
369
- )
370
-
371
- self .bucket = GridFSBucket (self .client .perftest )
372
- with open (gridfs_path , "rb" ) as gfile :
373
- self .uploaded_id = self .bucket .upload_from_stream ("gridfstest" , gfile )
374
-
375
- def tearDown (self ):
376
- super ().tearDown ()
377
- self .client .drop_database ("perftest" )
334
+ super ().setUp ()
335
+ self .uploaded_id = self .bucket .upload_from_stream ("gridfstest" , self .document )
378
336
379
337
def do_task (self ):
380
338
self .bucket .open_download_stream (self .uploaded_id ).read ()
381
339
382
340
383
- proc_client = None
341
+ proc_client : Optional [ MongoClient ] = None
384
342
385
343
386
344
def proc_init (* dummy ):
0 commit comments