Skip to content

Commit 9d43606

Browse files
authored
PYTHON-4325 Add multi-threaded benchmarks for Find and RunCommand (#1576)
1 parent 4e5813c commit 9d43606

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

test/performance/perf_test.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import os
4444
import sys
4545
import tempfile
46+
import threading
4647
import time
4748
import warnings
4849
from typing import Any, List, Optional
@@ -101,10 +102,19 @@ def __exit__(self, *args):
101102
self.interval = self.end - self.start
102103

103104

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+
104113
class PerformanceTest:
105114
dataset: str
106115
data_size: int
107116
fail: Any
117+
n_threads: int = 1
108118

109119
@classmethod
110120
def setUpClass(cls):
@@ -118,7 +128,7 @@ def tearDown(self):
118128
# Remove "Test" so that TestFlatEncoding is reported as "FlatEncoding".
119129
name = self.__class__.__name__[4:]
120130
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
122132
print(
123133
f"Completed {self.__class__.__name__} {megabytes_per_sec:.3f} MB/s, MEDIAN={self.percentile(50):.3f}s, "
124134
f"total time={duration:.3f}s, iterations={len(self.results)}"
@@ -128,7 +138,7 @@ def tearDown(self):
128138
"info": {
129139
"test_name": name,
130140
"args": {
131-
"threads": 1,
141+
"threads": self.n_threads,
132142
},
133143
},
134144
"metrics": [
@@ -163,7 +173,10 @@ def runTest(self):
163173
i += 1
164174
self.before()
165175
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)
167180
self.after()
168181
results.append(timer.interval)
169182
duration = time.monotonic() - start
@@ -308,6 +321,10 @@ def do_task(self):
308321
command("hello", True)
309322

310323

324+
class TestRunCommand8Threads(TestRunCommand):
325+
n_threads = 8
326+
327+
311328
class TestDocument(PerformanceTest):
312329
def setUp(self):
313330
super().setUp()
@@ -356,6 +373,10 @@ def do_task(self):
356373
find_one({"_id": _id})
357374

358375

376+
class TestFindOneByID8Threads(TestFindOneByID):
377+
n_threads = 8
378+
379+
359380
class SmallDocInsertTest(TestDocument):
360381
dataset = "small_doc.json"
361382

@@ -395,6 +416,10 @@ def do_task(self):
395416
list(self.corpus.find())
396417

397418

419+
class TestFindManyAndEmptyCursor8Threads(TestFindManyAndEmptyCursor):
420+
n_threads = 8
421+
422+
398423
class TestSmallDocBulkInsert(SmallDocInsertTest, unittest.TestCase):
399424
def do_task(self):
400425
self.corpus.insert_many(self.documents, ordered=True)

0 commit comments

Comments
 (0)