Skip to content

Commit

Permalink
Add benchmark for layers kept
Browse files Browse the repository at this point in the history
I think it's helpful to benchmark this separately,
as it might be possible to take a shortcut when the contract
is kept. We want to optimize for this case, since in most cases
most contracts will be kept (CI is green most of the time).
  • Loading branch information
Peter554 committed Jan 21, 2025
1 parent 4ef295e commit 572d605
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions tests/benchmarking/test_benchmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,21 @@ def test_build_django_from_cache(benchmark):


class TestFindIllegalDependenciesForLayers:
def test_top_level_large_graph(self, large_graph, benchmark):
@staticmethod
def _remove_package_dependencies(graph, package_dependencies):
graph = deepcopy(graph)
for dep in package_dependencies:
for route in dep.routes:
if route.middle:
for tail in route.tails:
graph.remove_import(importer=route.middle[-1], imported=tail)
else:
for head in route.heads:
for tail in route.tails:
graph.remove_import(importer=head, imported=tail)
return graph

def test_top_level_large_graph_violated(self, large_graph, benchmark):
result = _run_benchmark(
benchmark,
large_graph.find_illegal_dependencies_for_layers,
Expand All @@ -321,12 +335,33 @@ def test_top_level_large_graph(self, large_graph, benchmark):
)
assert result == TOP_LEVEL_PACKAGE_DEPENDENCIES

def test_deep_layers_large_graph(self, large_graph, benchmark):
def test_top_level_large_graph_kept(self, large_graph, benchmark):
large_graph = self._remove_package_dependencies(
large_graph, TOP_LEVEL_PACKAGE_DEPENDENCIES
)
result = _run_benchmark(
benchmark,
large_graph.find_illegal_dependencies_for_layers,
layers=TOP_LEVEL_LAYERS,
containers=("mypackage",),
)
assert result == set()

def test_deep_layers_large_graph_violated(self, large_graph, benchmark):
result = _run_benchmark(
benchmark, large_graph.find_illegal_dependencies_for_layers, layers=DEEP_LAYERS
)
assert result == DEEP_LAYER_PACKAGE_DEPENDENCIES

def test_deep_layers_large_graph_kept(self, large_graph, benchmark):
large_graph = self._remove_package_dependencies(
large_graph, DEEP_LAYER_PACKAGE_DEPENDENCIES
)
result = _run_benchmark(
benchmark, large_graph.find_illegal_dependencies_for_layers, layers=DEEP_LAYERS
)
assert result == set()


def test_find_descendants(large_graph, benchmark):
result = _run_benchmark(benchmark, large_graph.find_descendants, "mypackage")
Expand Down

0 comments on commit 572d605

Please sign in to comment.