|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Direct hop() microbenchmarks for common traversal shapes. |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import argparse |
| 9 | +import time |
| 10 | +from dataclasses import dataclass |
| 11 | +from typing import Iterable, List, Optional, Tuple |
| 12 | + |
| 13 | +import pandas as pd |
| 14 | + |
| 15 | +import graphistry |
| 16 | +from graphistry.Engine import Engine |
| 17 | + |
| 18 | + |
| 19 | +@dataclass(frozen=True) |
| 20 | +class Scenario: |
| 21 | + name: str |
| 22 | + hops: int |
| 23 | + direction: str |
| 24 | + seed_mode: str # "seed0" | "all" |
| 25 | + return_as_wave_front: bool = True |
| 26 | + |
| 27 | + |
| 28 | +@dataclass(frozen=True) |
| 29 | +class GraphSpec: |
| 30 | + name: str |
| 31 | + nodes: int |
| 32 | + edges: int |
| 33 | + kind: str # "linear" | "dense" |
| 34 | + |
| 35 | + |
| 36 | +@dataclass |
| 37 | +class ResultRow: |
| 38 | + graph: str |
| 39 | + scenario: str |
| 40 | + ms: Optional[float] |
| 41 | + |
| 42 | + |
| 43 | +def make_linear_graph(n_nodes: int, n_edges: int) -> Tuple[pd.DataFrame, pd.DataFrame]: |
| 44 | + nodes = pd.DataFrame({"id": list(range(n_nodes))}) |
| 45 | + edges_list = [] |
| 46 | + for i in range(min(n_edges, n_nodes - 1)): |
| 47 | + edges_list.append({"src": i, "dst": i + 1, "eid": i}) |
| 48 | + edges = pd.DataFrame(edges_list) |
| 49 | + return nodes, edges |
| 50 | + |
| 51 | + |
| 52 | +def make_dense_graph(n_nodes: int, n_edges: int) -> Tuple[pd.DataFrame, pd.DataFrame]: |
| 53 | + import random |
| 54 | + |
| 55 | + random.seed(42) |
| 56 | + nodes = pd.DataFrame({"id": list(range(n_nodes))}) |
| 57 | + edges_list = [] |
| 58 | + for i in range(n_edges): |
| 59 | + src = random.randint(0, n_nodes - 2) |
| 60 | + dst = random.randint(src + 1, n_nodes - 1) |
| 61 | + edges_list.append({"src": src, "dst": dst, "eid": i}) |
| 62 | + edges = pd.DataFrame(edges_list).drop_duplicates(subset=["src", "dst"]) |
| 63 | + return nodes, edges |
| 64 | + |
| 65 | + |
| 66 | +def build_graph(spec: GraphSpec, engine: Engine): |
| 67 | + if spec.kind == "dense": |
| 68 | + nodes_df, edges_df = make_dense_graph(spec.nodes, spec.edges) |
| 69 | + else: |
| 70 | + nodes_df, edges_df = make_linear_graph(spec.nodes, spec.edges) |
| 71 | + |
| 72 | + if engine == Engine.CUDF: |
| 73 | + import cudf # type: ignore |
| 74 | + |
| 75 | + nodes_df = cudf.from_pandas(nodes_df) |
| 76 | + edges_df = cudf.from_pandas(edges_df) |
| 77 | + |
| 78 | + return graphistry.nodes(nodes_df, "id").edges(edges_df, "src", "dst") |
| 79 | + |
| 80 | + |
| 81 | +def _time_call(fn, runs: int) -> float: |
| 82 | + times = [] |
| 83 | + for _ in range(runs): |
| 84 | + start = time.perf_counter() |
| 85 | + fn() |
| 86 | + times.append((time.perf_counter() - start) * 1000) |
| 87 | + return sum(times) / len(times) |
| 88 | + |
| 89 | + |
| 90 | +def run_scenarios(g, scenarios: List[Scenario], runs: int) -> Iterable[ResultRow]: |
| 91 | + for scenario in scenarios: |
| 92 | + seed_nodes = None |
| 93 | + if scenario.seed_mode == "seed0": |
| 94 | + seed_nodes = g._nodes[g._nodes["id"] == 0] |
| 95 | + |
| 96 | + def _call() -> None: |
| 97 | + g.hop( |
| 98 | + nodes=seed_nodes, |
| 99 | + hops=scenario.hops, |
| 100 | + to_fixed_point=False, |
| 101 | + direction=scenario.direction, |
| 102 | + return_as_wave_front=scenario.return_as_wave_front, |
| 103 | + ) |
| 104 | + |
| 105 | + ms = _time_call(_call, runs) |
| 106 | + yield ResultRow(graph="", scenario=scenario.name, ms=ms) |
| 107 | + |
| 108 | + |
| 109 | +def build_scenarios() -> List[Scenario]: |
| 110 | + return [ |
| 111 | + Scenario("2hop_forward_seed0", 2, "forward", "seed0", True), |
| 112 | + Scenario("2hop_forward_all", 2, "forward", "all", True), |
| 113 | + Scenario("2hop_undirected_seed0", 2, "undirected", "seed0", True), |
| 114 | + Scenario("2hop_undirected_all", 2, "undirected", "all", True), |
| 115 | + ] |
| 116 | + |
| 117 | + |
| 118 | +def build_graph_specs() -> List[GraphSpec]: |
| 119 | + return [ |
| 120 | + GraphSpec("small_linear", 1_000, 2_000, "linear"), |
| 121 | + GraphSpec("medium_linear", 10_000, 20_000, "linear"), |
| 122 | + GraphSpec("medium_dense", 10_000, 50_000, "dense"), |
| 123 | + ] |
| 124 | + |
| 125 | + |
| 126 | +def write_markdown(results: Iterable[ResultRow], output_path: str) -> None: |
| 127 | + header = [ |
| 128 | + "# Hop Microbench Results", |
| 129 | + "", |
| 130 | + "Notes:", |
| 131 | + "- Direct hop() calls; no WHERE predicates.", |
| 132 | + "", |
| 133 | + "| Graph | Scenario | Time |", |
| 134 | + "|-------|----------|------|", |
| 135 | + ] |
| 136 | + lines = header + [ |
| 137 | + f"| {row.graph} | {row.scenario} | {row.ms:.2f}ms |" for row in results |
| 138 | + ] |
| 139 | + with open(output_path, "w", encoding="utf-8") as f: |
| 140 | + f.write("\n".join(lines) + "\n") |
| 141 | + |
| 142 | + |
| 143 | +def main() -> None: |
| 144 | + parser = argparse.ArgumentParser(description="Hop microbenchmarks.") |
| 145 | + parser.add_argument("--engine", default="pandas", choices=["pandas", "cudf"]) |
| 146 | + parser.add_argument("--runs", type=int, default=3) |
| 147 | + parser.add_argument("--output", default="") |
| 148 | + args = parser.parse_args() |
| 149 | + |
| 150 | + engine = Engine.CUDF if args.engine == "cudf" else Engine.PANDAS |
| 151 | + scenarios = build_scenarios() |
| 152 | + results: List[ResultRow] = [] |
| 153 | + for spec in build_graph_specs(): |
| 154 | + g = build_graph(spec, engine) |
| 155 | + for row in run_scenarios(g, scenarios, args.runs): |
| 156 | + row.graph = spec.name |
| 157 | + results.append(row) |
| 158 | + |
| 159 | + if args.output: |
| 160 | + write_markdown(results, args.output) |
| 161 | + |
| 162 | + print("| Graph | Scenario | Time |") |
| 163 | + print("|-------|----------|------|") |
| 164 | + for row in results: |
| 165 | + print(f"| {row.graph} | {row.scenario} | {row.ms:.2f}ms |") |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + main() |
0 commit comments