|
| 1 | +# Copyright 2025-present MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for the MongoDB ODM Performance Benchmark Spec. |
| 16 | +
|
| 17 | +See https://github.com/mongodb/specifications/blob/master/source/benchmarking/odm-benchmarking.md |
| 18 | +
|
| 19 | +
|
| 20 | +To set up the benchmarks locally:: |
| 21 | + git clone --depth 1 https://github.com/mongodb/specifications.git |
| 22 | + pushd specifications/source/benchmarking/odm-data |
| 23 | + tar xf flat_models.tgz |
| 24 | + tar xf nested_models.tgz |
| 25 | + popd |
| 26 | + export TEST_PATH="specifications/source/benchmarking/odm-data" |
| 27 | + export OUTPUT_FILE="results.json" |
| 28 | +
|
| 29 | +Then to run all benchmarks quickly:: |
| 30 | + cd tests/performance |
| 31 | + FASTBENCH=1 python manage.py test |
| 32 | +
|
| 33 | +To run individual benchmarks quickly:: |
| 34 | + cd tests/performance |
| 35 | + FASTBENCH=1 python manage.py test perftest.tests.TestLargeNestedDocFilterArray |
| 36 | +""" |
| 37 | + |
1 | 38 | import json
|
2 | 39 | import os
|
3 | 40 | import time
|
4 | 41 | import warnings
|
| 42 | +from pathlib import Path |
5 | 43 |
|
6 | 44 | from bson import ObjectId, encode
|
7 | 45 | from django.test import (
|
|
20 | 58 |
|
21 | 59 | OUTPUT_FILE = os.environ.get("OUTPUT_FILE")
|
22 | 60 |
|
23 |
| -NUM_ITERATIONS = 10 |
24 |
| -MIN_ITERATION_TIME = 30 |
25 |
| -MAX_ITERATION_TIME = 60 |
26 |
| -NUM_DOCS = 10000 |
| 61 | +if os.environ.get("FASTBENCH"): |
| 62 | + NUM_ITERATIONS = 1 |
| 63 | + MIN_ITERATION_TIME = 5 |
| 64 | + MAX_ITERATION_TIME = 10 |
| 65 | + NUM_DOCS = 1000 |
| 66 | +else: |
| 67 | + NUM_ITERATIONS = 10 |
| 68 | + MIN_ITERATION_TIME = 30 |
| 69 | + MAX_ITERATION_TIME = 60 |
| 70 | + NUM_DOCS = 10000 |
| 71 | + |
| 72 | +TEST_PATH = os.environ.get("TEST_PATH", Path(os.path.realpath(__file__)).parent.parent / "odm-data") |
27 | 73 |
|
28 | 74 | result_data: list = []
|
29 | 75 |
|
@@ -135,7 +181,7 @@ class SmallFlatDocTest(PerformanceTest):
|
135 | 181 |
|
136 | 182 | def setUp(self):
|
137 | 183 | super().setUp()
|
138 |
| - with open(self.dataset) as data: # noqa: PTH123 |
| 184 | + with open(Path(TEST_PATH) / Path("flat_models") / self.dataset) as data: # noqa: PTH123 |
139 | 185 | self.document = json.load(data)
|
140 | 186 |
|
141 | 187 | self.data_size = len(encode(self.document)) * NUM_DOCS
|
@@ -215,7 +261,7 @@ class LargeFlatDocTest(PerformanceTest):
|
215 | 261 |
|
216 | 262 | def setUp(self):
|
217 | 263 | super().setUp()
|
218 |
| - with open(self.dataset) as data: # noqa: PTH123 |
| 264 | + with open(Path(TEST_PATH) / Path("flat_models") / self.dataset) as data: # noqa: PTH123 |
219 | 265 | self.document = json.load(data)
|
220 | 266 |
|
221 | 267 | self.data_size = len(encode(self.document)) * NUM_DOCS
|
@@ -255,7 +301,7 @@ class LargeNestedDocTest(PerformanceTest):
|
255 | 301 |
|
256 | 302 | def setUp(self):
|
257 | 303 | super().setUp()
|
258 |
| - with open(self.dataset) as data: # noqa: PTH123 |
| 304 | + with open(Path(TEST_PATH) / Path("nested_models") / self.dataset) as data: # noqa: PTH123 |
259 | 305 | self.document = json.load(data)
|
260 | 306 |
|
261 | 307 | self.data_size = len(encode(self.document)) * NUM_DOCS
|
@@ -298,14 +344,14 @@ def setUp(self):
|
298 | 344 | self.models = list(LargeNestedModel.objects.all())
|
299 | 345 | self.data_size = len(encode({"field1": "updated_value"})) * NUM_DOCS
|
300 | 346 |
|
301 |
| - def after(self): |
302 |
| - LargeNestedModel.objects.all().delete() |
303 |
| - |
304 | 347 | def do_task(self):
|
305 | 348 | for model in self.models:
|
306 | 349 | model.embedded_str_doc_1.field1 = "updated_value"
|
307 | 350 | model.save()
|
308 | 351 |
|
| 352 | + def after(self): |
| 353 | + LargeNestedModel.objects.all().delete() |
| 354 | + |
309 | 355 |
|
310 | 356 | class TestLargeNestedDocFilterById(LargeNestedDocTest, TestCase):
|
311 | 357 | def setUp(self):
|
|
0 commit comments