43
43
import os
44
44
import sys
45
45
import tempfile
46
+ import threading
46
47
import time
47
48
import warnings
48
49
from typing import Any , List , Optional
@@ -101,10 +102,19 @@ def __exit__(self, *args):
101
102
self .interval = self .end - self .start
102
103
103
104
105
+ def threaded (n_threads , func ):
106
+ threads = [threading .Thread (target = func ) for _ in range (n_threads )]
107
+ for t in threads :
108
+ t .start ()
109
+ for t in threads :
110
+ t .join ()
111
+
112
+
104
113
class PerformanceTest :
105
114
dataset : str
106
115
data_size : int
107
116
fail : Any
117
+ n_threads : int = 1
108
118
109
119
@classmethod
110
120
def setUpClass (cls ):
@@ -118,7 +128,7 @@ def tearDown(self):
118
128
# Remove "Test" so that TestFlatEncoding is reported as "FlatEncoding".
119
129
name = self .__class__ .__name__ [4 :]
120
130
median = self .percentile (50 )
121
- megabytes_per_sec = self .data_size / median / 1000000
131
+ megabytes_per_sec = ( self .data_size * self . n_threads ) / median / 1000000
122
132
print (
123
133
f"Completed { self .__class__ .__name__ } { megabytes_per_sec :.3f} MB/s, MEDIAN={ self .percentile (50 ):.3f} s, "
124
134
f"total time={ duration :.3f} s, iterations={ len (self .results )} "
@@ -128,7 +138,7 @@ def tearDown(self):
128
138
"info" : {
129
139
"test_name" : name ,
130
140
"args" : {
131
- "threads" : 1 ,
141
+ "threads" : self . n_threads ,
132
142
},
133
143
},
134
144
"metrics" : [
@@ -163,7 +173,10 @@ def runTest(self):
163
173
i += 1
164
174
self .before ()
165
175
with Timer () as timer :
166
- self .do_task ()
176
+ if self .n_threads == 1 :
177
+ self .do_task ()
178
+ else :
179
+ threaded (self .n_threads , self .do_task )
167
180
self .after ()
168
181
results .append (timer .interval )
169
182
duration = time .monotonic () - start
@@ -308,6 +321,10 @@ def do_task(self):
308
321
command ("hello" , True )
309
322
310
323
324
+ class TestRunCommand8Threads (TestRunCommand ):
325
+ n_threads = 8
326
+
327
+
311
328
class TestDocument (PerformanceTest ):
312
329
def setUp (self ):
313
330
super ().setUp ()
@@ -356,6 +373,10 @@ def do_task(self):
356
373
find_one ({"_id" : _id })
357
374
358
375
376
+ class TestFindOneByID8Threads (TestFindOneByID ):
377
+ n_threads = 8
378
+
379
+
359
380
class SmallDocInsertTest (TestDocument ):
360
381
dataset = "small_doc.json"
361
382
@@ -395,6 +416,10 @@ def do_task(self):
395
416
list (self .corpus .find ())
396
417
397
418
419
+ class TestFindManyAndEmptyCursor8Threads (TestFindManyAndEmptyCursor ):
420
+ n_threads = 8
421
+
422
+
398
423
class TestSmallDocBulkInsert (SmallDocInsertTest , unittest .TestCase ):
399
424
def do_task (self ):
400
425
self .corpus .insert_many (self .documents , ordered = True )
0 commit comments