|
2 | 2 | import json |
3 | 3 | import importlib |
4 | 4 | from pathlib import Path |
| 5 | + |
| 6 | +from tests.config import override_settings |
5 | 7 | from grimp.adaptors.graph import ImportGraph |
6 | 8 | from grimp import PackageDependency, Route |
7 | 9 | import grimp |
8 | 10 | from copy import deepcopy |
| 11 | +from .adaptors import PrefixMissingCache |
9 | 12 |
|
10 | 13 |
|
11 | 14 | def _run_benchmark(benchmark, fn, *args, **kwargs): |
@@ -325,15 +328,21 @@ def test_build_django_from_cache_a_few_misses(benchmark, number_of_misses): |
325 | 328 |
|
326 | 329 | This benchmark utilizes the cache except for a few modules, which we add. |
327 | 330 | """ |
328 | | - # Populate the cache first, before beginning the benchmark. |
329 | | - grimp.build_graph("django") |
330 | | - # Add a module which won't be in the cache. |
331 | | - django_path = Path(importlib.util.find_spec("django").origin).parent |
332 | | - for i in range(number_of_misses): |
333 | | - new_module = django_path / f"new_module_{i}.py" |
334 | | - new_module.write_text("from django.db import models") |
335 | | - |
336 | | - _run_benchmark(benchmark, grimp.build_graph, "django") |
| 331 | + # We must use a special cache class, otherwise the cache will be populated |
| 332 | + # by the first iteration. It would be better to do this using a setup function, |
| 333 | + # which is supported by pytest-benchmark's pedantic mode, but not codspeed. |
| 334 | + # This won't give us a truly accurate picture, but it's better than nothing. |
| 335 | + with override_settings(CACHE_CLASS=PrefixMissingCache): |
| 336 | + # Add some specially-named modules which will be treated as not in the cache. |
| 337 | + django_path = Path(importlib.util.find_spec("django").origin).parent |
| 338 | + for i in range(number_of_misses): |
| 339 | + new_module = django_path / f"{PrefixMissingCache.MISSING_PREFIX}{i}.py" |
| 340 | + new_module.write_text("from django.db import models") |
| 341 | + |
| 342 | + # Populate the cache. |
| 343 | + grimp.build_graph("django") |
| 344 | + |
| 345 | + _run_benchmark(benchmark, grimp.build_graph, "django") |
337 | 346 |
|
338 | 347 |
|
339 | 348 | class TestFindIllegalDependenciesForLayers: |
|
0 commit comments