|
1 |
| -# Copyright 2015 MongoDB, Inc. |
| 1 | +# Copyright 2015-present MongoDB, Inc. |
2 | 2 | #
|
3 | 3 | # Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | # you may not use this file except in compliance with the License.
|
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -"""Tests for the MongoDB Driver Performance Benchmarking Spec.""" |
| 15 | +"""Tests for the MongoDB Driver Performance Benchmarking Spec. |
| 16 | +
|
| 17 | +See https://github.com/mongodb/specifications/blob/master/source/benchmarking/benchmarking.md |
| 18 | +
|
| 19 | +
|
| 20 | +To set up the benchmarks locally:: |
| 21 | +
|
| 22 | + python -m pip install simplejson |
| 23 | + git clone --depth 1 https://github.com/mongodb/specifications.git |
| 24 | + pushd specifications/source/benchmarking/data |
| 25 | + tar xf extended_bson.tgz |
| 26 | + tar xf parallel.tgz |
| 27 | + tar xf single_and_multi_document.tgz |
| 28 | + popd |
| 29 | + export TEST_PATH="specifications/source/benchmarking/data" |
| 30 | + export OUTPUT_FILE="results.json" |
| 31 | +
|
| 32 | +Then to run all benchmarks quickly:: |
| 33 | +
|
| 34 | + FASTBENCH=1 python test/performance/perf_test.py -v |
| 35 | +
|
| 36 | +To run individual benchmarks quickly:: |
| 37 | +
|
| 38 | + FASTBENCH=1 python test/performance/perf_test.py -v TestRunCommand TestFindManyAndEmptyCursor |
| 39 | +""" |
16 | 40 | from __future__ import annotations
|
17 | 41 |
|
18 | 42 | import multiprocessing as mp
|
|
36 | 60 | from gridfs import GridFSBucket
|
37 | 61 | from pymongo import MongoClient
|
38 | 62 |
|
| 63 | +# Spec says to use at least 1 minute cumulative execution time and up to 100 iterations or 5 minutes but that |
| 64 | +# makes the benchmarks too slow. Instead, we use at least 30 seconds and at most 60 seconds. |
39 | 65 | NUM_ITERATIONS = 100
|
40 |
| -MAX_ITERATION_TIME = 300 |
| 66 | +MIN_ITERATION_TIME = 30 |
| 67 | +MAX_ITERATION_TIME = 60 |
41 | 68 | NUM_DOCS = 10000
|
| 69 | +# When debugging or prototyping it's often useful to run the benchmarks locally, set FASTBENCH=1 to run quickly. |
| 70 | +if bool(os.getenv("FASTBENCH")): |
| 71 | + NUM_ITERATIONS = 2 |
| 72 | + MIN_ITERATION_TIME = 0.1 |
| 73 | + MAX_ITERATION_TIME = 0.5 |
| 74 | + NUM_DOCS = 1000 |
42 | 75 |
|
43 | 76 | TEST_PATH = os.environ.get(
|
44 | 77 | "TEST_PATH", os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.join("data"))
|
@@ -88,7 +121,7 @@ def tearDown(self):
|
88 | 121 | megabytes_per_sec = self.data_size / median / 1000000
|
89 | 122 | print(
|
90 | 123 | f"Completed {self.__class__.__name__} {megabytes_per_sec:.3f} MB/s, MEDIAN={self.percentile(50):.3f}s, "
|
91 |
| - f"total time={duration:.3f}s" |
| 124 | + f"total time={duration:.3f}s, iterations={len(self.results)}" |
92 | 125 | )
|
93 | 126 | result_data.append(
|
94 | 127 | {
|
@@ -125,19 +158,25 @@ def percentile(self, percentile):
|
125 | 158 | def runTest(self):
|
126 | 159 | results = []
|
127 | 160 | start = time.monotonic()
|
128 |
| - for i in range(NUM_ITERATIONS): |
129 |
| - if time.monotonic() - start > MAX_ITERATION_TIME: |
130 |
| - with warnings.catch_warnings(): |
131 |
| - warnings.simplefilter("default") |
132 |
| - warnings.warn( |
133 |
| - f"Test timed out after {MAX_ITERATION_TIME}s, completed {i}/{NUM_ITERATIONS} iterations." |
134 |
| - ) |
135 |
| - break |
| 161 | + i = 0 |
| 162 | + while True: |
| 163 | + i += 1 |
136 | 164 | self.before()
|
137 | 165 | with Timer() as timer:
|
138 | 166 | self.do_task()
|
139 | 167 | self.after()
|
140 | 168 | results.append(timer.interval)
|
| 169 | + duration = time.monotonic() - start |
| 170 | + if duration > MIN_ITERATION_TIME and i >= NUM_ITERATIONS: |
| 171 | + break |
| 172 | + if duration > MAX_ITERATION_TIME: |
| 173 | + with warnings.catch_warnings(): |
| 174 | + warnings.simplefilter("default") |
| 175 | + warnings.warn( |
| 176 | + f"{self.__class__.__name__} timed out after {MAX_ITERATION_TIME}s, completed {i}/{NUM_ITERATIONS} iterations." |
| 177 | + ) |
| 178 | + |
| 179 | + break |
141 | 180 |
|
142 | 181 | self.results = results
|
143 | 182 |
|
|
0 commit comments