From 00a4a4ae27f73c61f573adef9305bb2068d64da6 Mon Sep 17 00:00:00 2001 From: Erik Welch Date: Wed, 20 Nov 2024 20:59:46 -0600 Subject: [PATCH] Add basic benchmark --- .../nx-cugraph/pytest-based/bench_algos.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/benchmarks/nx-cugraph/pytest-based/bench_algos.py b/benchmarks/nx-cugraph/pytest-based/bench_algos.py index 015318c5a..17433e7c1 100644 --- a/benchmarks/nx-cugraph/pytest-based/bench_algos.py +++ b/benchmarks/nx-cugraph/pytest-based/bench_algos.py @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import random from collections.abc import Mapping import networkx as nx @@ -844,6 +845,36 @@ def bench_ego_graph(benchmark, graph_obj, backend_wrapper): assert isinstance(result, (nx.Graph, nxcg.Graph)) +def bench_lowest_common_ancestor(benchmark, graph_obj, backend_wrapper): + # Must be DAG + if not nx.is_directed_acyclic_graph(graph_obj): + new_graph_obj = nx.DiGraph() + new_graph_obj.add_nodes_from(graph_obj.nodes(data=True)) + new_graph_obj.add_edges_from( + (src, dst, *rest) + for src, dst, *rest in graph_obj.edges(data=True) + if src < dst + ) + new_graph_obj.graph.update(graph_obj.graph) + print( + f"WARNING: graph was changed and now had {new_graph_obj.number_of_nodes()} " + "nodes and {new_graph_obj.number_of_edges()} edges." + ) + graph_obj = new_graph_obj + + G = get_graph_obj_for_benchmark(graph_obj, backend_wrapper) + r = random.Random(42) + node1, node2 = r.sample(sorted(G), 2) + result = benchmark.pedantic( + target=backend_wrapper(nx.lowest_common_ancestor), + args=(G, node1, node2), + rounds=rounds, + iterations=iterations, + warmup_rounds=warmup_rounds, + ) + assert result is None or result in G + + @pytest.mark.skip(reason="benchmark not implemented") def bench_complete_bipartite_graph(benchmark, graph_obj, backend_wrapper): pass