From 5b42986a45256d833fd4b115ba80b1e5d0d095d0 Mon Sep 17 00:00:00 2001 From: Ralph Liu Date: Mon, 25 Nov 2024 13:41:01 -0800 Subject: [PATCH] Updates to bench_algos.py --- benchmarks/pytest-based/bench_algos.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/benchmarks/pytest-based/bench_algos.py b/benchmarks/pytest-based/bench_algos.py index d5257e2ef..783d2bb5e 100644 --- a/benchmarks/pytest-based/bench_algos.py +++ b/benchmarks/pytest-based/bench_algos.py @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from collections.abc import Mapping import networkx as nx @@ -224,6 +225,17 @@ def build_personalization_dict(pagerank_dict): return pers_dict +# Used to return a function that calls the original function inside a try-except block +# which is Useful to still save pytest-benchmark numbers if failure is correct behavior for +# the function being tested. +def possible_to_fail(exception, function): + def nested_func(*args, **kwargs): + try: + return function(*args, **kwargs) + except exception: + pass + return nested_func + ################################################################################ # Benchmarks @@ -366,7 +378,10 @@ def bench_in_degree_centrality(benchmark, graph_obj, backend_wrapper): def bench_katz_centrality(benchmark, graph_obj, backend_wrapper, normalized): G = get_graph_obj_for_benchmark(graph_obj, backend_wrapper) result = benchmark.pedantic( - target=backend_wrapper(nx.katz_centrality), + target=possible_to_fail( + nx.PowerIterationFailedConvergence, + backend_wrapper(nx.katz_centrality) + ), args=(G,), kwargs=dict( normalized=normalized, @@ -375,7 +390,7 @@ def bench_katz_centrality(benchmark, graph_obj, backend_wrapper, normalized): iterations=iterations, warmup_rounds=warmup_rounds, ) - assert type(result) is dict + assert type(result) is dict or result is None def bench_k_truss(benchmark, graph_obj, backend_wrapper): @@ -691,7 +706,7 @@ def bench_descendants_at_distance(benchmark, graph_obj, backend_wrapper): ) assert type(result) is set - +@pytest.mark.skip(reason="benchmark not implemented") def bench_is_bipartite(benchmark, graph_obj, backend_wrapper): G = get_graph_obj_for_benchmark(graph_obj, backend_wrapper) result = benchmark.pedantic( @@ -704,6 +719,7 @@ def bench_is_bipartite(benchmark, graph_obj, backend_wrapper): assert type(result) is bool +@pytest.mark.skip(reason="benchmark not implemented") def bench_is_strongly_connected(benchmark, graph_obj, backend_wrapper): G = get_graph_obj_for_benchmark(graph_obj, backend_wrapper) result = benchmark.pedantic( @@ -728,6 +744,7 @@ def bench_is_weakly_connected(benchmark, graph_obj, backend_wrapper): assert type(result) is bool +@pytest.mark.skip(reason="benchmark not implemented") def bench_number_strongly_connected_components(benchmark, graph_obj, backend_wrapper): G = get_graph_obj_for_benchmark(graph_obj, backend_wrapper) result = benchmark.pedantic( @@ -780,6 +797,7 @@ def bench_reciprocity(benchmark, graph_obj, backend_wrapper): assert type(result) is float +@pytest.mark.skip(reason="benchmark not implemented") def bench_strongly_connected_components(benchmark, graph_obj, backend_wrapper): G = get_graph_obj_for_benchmark(graph_obj, backend_wrapper) result = benchmark.pedantic( @@ -850,7 +868,7 @@ def bench_ego_graph(benchmark, graph_obj, backend_wrapper): iterations=iterations, warmup_rounds=warmup_rounds, ) - assert isinstance(result, (nx.Graph, nxcg.Graph)) + assert type(result) is type(G) @pytest.mark.skip(reason="benchmark not implemented")