From 075800844f380f0d48ead0cea033b77b6bee4aa2 Mon Sep 17 00:00:00 2001 From: David Seddon Date: Fri, 7 Feb 2025 13:50:41 +0000 Subject: [PATCH 1/2] Add benchmark for get_import_details --- tests/benchmarking/test_benchmarking.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/benchmarking/test_benchmarking.py b/tests/benchmarking/test_benchmarking.py index 85182f21..f271f883 100644 --- a/tests/benchmarking/test_benchmarking.py +++ b/tests/benchmarking/test_benchmarking.py @@ -438,3 +438,18 @@ def f(): _ = module _run_benchmark(benchmark, f) + + +def test_get_import_details(benchmark): + graph = ImportGraph() + iterations = 100 + for i in range(iterations, 1): + graph.add_import( + importer=f"blue_{i}", imported=f"green_{i}", line_contents="...", line_number=i + ) + + def f(): + for i in range(iterations): + graph.get_import_details(importer=f"blue_{i}", imported=f"green_{i}") + + _run_benchmark(benchmark, f) From 7e1c32484dda13118c9988191e8837f0add94c74 Mon Sep 17 00:00:00 2001 From: David Seddon Date: Fri, 7 Feb 2025 14:56:04 +0000 Subject: [PATCH 2/2] Bust cache in module property benchmarks --- tests/benchmarking/test_benchmarking.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/tests/benchmarking/test_benchmarking.py b/tests/benchmarking/test_benchmarking.py index f271f883..e5cdc964 100644 --- a/tests/benchmarking/test_benchmarking.py +++ b/tests/benchmarking/test_benchmarking.py @@ -424,18 +424,27 @@ def test_copy_graph(large_graph, benchmark): _run_benchmark(benchmark, lambda: deepcopy(large_graph)) -def test_graph_contains_module(large_graph, benchmark): - def f(n): - for i in range(n): - _ = f"foo{i}" in large_graph.modules +def test_modules_property_first_access(large_graph, benchmark): + def f(): + # Benchmarking runs multiple times over the same object, so we need + # to bust the cache first. The easiest way to do this is to add a module. + large_graph.add_module("cachebuster") + + # Accessing the modules property is what we're benchmarking. + _ = large_graph.modules - _run_benchmark(benchmark, f, 100) + _run_benchmark(benchmark, f) -def test_iterate_over_modules_in_graph(large_graph, benchmark): +def test_modules_property_many_accesses(large_graph, benchmark): def f(): - for module in large_graph.modules: - _ = module + # Benchmarking runs multiple times over the same object, so we need + # to bust the cache first. The easiest way to do this is to add a module. + large_graph.add_module("cachebuster") + + # Accessing the modules property is what we're benchmarking. + for i in range(1000): + _ = large_graph.modules _run_benchmark(benchmark, f)