Skip to content

Commit 89ffa6f

Browse files
committed
Use PrefixMissingCache in test_build_django_from_cache_a_few_misses
Prior to this, the cache would get populated after the first iteration and after that it would behave as if it was a fully-populated cache.
1 parent a318714 commit 89ffa6f

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

tests/benchmarking/adaptors.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from grimp.adaptors.caching import Cache, CacheMiss
2+
from typing import Set
3+
4+
from grimp.application.ports.modulefinder import ModuleFile
5+
from grimp.domain.valueobjects import DirectImport
6+
7+
8+
class PrefixMissingCache(Cache):
9+
"""
10+
Test double of the real cache that will miss caching any module that begins with
11+
a special prefix.
12+
"""
13+
14+
MISSING_PREFIX = "miss_marker_8772f06d64b6_" # Arbitrary prefix.
15+
16+
def read_imports(self, module_file: ModuleFile) -> Set[DirectImport]:
17+
leaf_name = module_file.module.name.split(".")[-1]
18+
if leaf_name.startswith(self.MISSING_PREFIX):
19+
raise CacheMiss
20+
return super().read_imports(module_file)

tests/benchmarking/test_benchmarking.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
import json
33
import importlib
44
from pathlib import Path
5+
6+
from tests.config import override_settings
57
from grimp.adaptors.graph import ImportGraph
68
from grimp import PackageDependency, Route
79
import grimp
810
from copy import deepcopy
11+
from .adaptors import PrefixMissingCache
912

1013

1114
def _run_benchmark(benchmark, fn, *args, **kwargs):
@@ -325,15 +328,21 @@ def test_build_django_from_cache_a_few_misses(benchmark, number_of_misses):
325328
326329
This benchmark utilizes the cache except for a few modules, which we add.
327330
"""
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")
337346

338347

339348
class TestFindIllegalDependenciesForLayers:

0 commit comments

Comments
 (0)