|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""vs_kuzu.py — head-to-head against a REAL embedded graph database (KuzuDB, Cypher + native PageRank). |
| 3 | +
|
| 4 | +This is the comparison that usually "needs a cluster": an actual graph DB, not a linear-algebra library. |
| 5 | +Kuzu runs in-process (no server, no docker), so we can put it head-to-head with hg_analytics on the SAME |
| 6 | +Graph500 graph, on the SAME machine. We report Kuzu's bulk-load time and its PageRank compute time |
| 7 | +separately (the fair algo-vs-algo comparison is compute-to-compute), plus top-100 ranking agreement. |
| 8 | +
|
| 9 | +Prereqs: |
| 10 | + pip install kuzu |
| 11 | + HG_SCALE=17 cargo run -p hg_analytics --release --example vs_baseline # emits the shared graph |
| 12 | + HG_OUT=/tmp/hg_vs17 python3 scripts/bench/vs_kuzu.py |
| 13 | +""" |
| 14 | +import os |
| 15 | +import time |
| 16 | +import tempfile |
| 17 | +import numpy as np |
| 18 | +import kuzu |
| 19 | + |
| 20 | +OUT = os.environ.get("HG_OUT", "/tmp/hg_vs17") |
| 21 | +meta = open(f"{OUT}/meta.txt").read().split() |
| 22 | +n, m = int(meta[0]), int(meta[1]) |
| 23 | +rust_parallel_s = float(meta[5]) |
| 24 | +edges = np.fromfile(f"{OUT}/edges.bin", dtype=np.uint32).reshape(-1, 2) |
| 25 | +rust_top = [int(x) for x in open(f"{OUT}/rust_top.txt").read().split()] |
| 26 | + |
| 27 | +print(f"graph: n={n} m={m} (same RMAT graph hg_analytics used)") |
| 28 | +print(f"hg_analytics parallel PageRank: {rust_parallel_s*1000:.1f} ms (in-memory, no load step)") |
| 29 | + |
| 30 | +# ── write the graph as CSVs for Kuzu's bulk COPY ───────────────────────────────────────────────────── |
| 31 | +d = tempfile.mkdtemp() |
| 32 | +nodes_csv, rels_csv = f"{d}/nodes.csv", f"{d}/rels.csv" |
| 33 | +t = time.perf_counter() |
| 34 | +np.savetxt(nodes_csv, np.arange(n), fmt="%d") |
| 35 | +np.savetxt(rels_csv, edges, fmt="%d,%d") |
| 36 | +csv_s = time.perf_counter() - t |
| 37 | + |
| 38 | +# ── load into Kuzu (bulk COPY) ─────────────────────────────────────────────────────────────────────── |
| 39 | +db = kuzu.Database(f"{d}/db") |
| 40 | +con = kuzu.Connection(db) |
| 41 | +con.execute("CREATE NODE TABLE V(id INT64, PRIMARY KEY(id))") |
| 42 | +con.execute("CREATE REL TABLE E(FROM V TO V)") |
| 43 | +t = time.perf_counter() |
| 44 | +con.execute(f'COPY V FROM "{nodes_csv}"') |
| 45 | +con.execute(f'COPY E FROM "{rels_csv}"') |
| 46 | +load_s = time.perf_counter() - t |
| 47 | + |
| 48 | +# ── Kuzu native PageRank (algo extension) ──────────────────────────────────────────────────────────── |
| 49 | +con.execute("INSTALL algo") |
| 50 | +con.execute("LOAD algo") |
| 51 | +con.execute("CALL project_graph('G', ['V'], ['E'])") |
| 52 | +t = time.perf_counter() |
| 53 | +res = con.execute("CALL page_rank('G') RETURN node.id AS id, rank ORDER BY rank DESC") |
| 54 | +kuzu_s = time.perf_counter() - t |
| 55 | +kuzu_order = [] |
| 56 | +while res.has_next(): |
| 57 | + kuzu_order.append(int(res.get_next()[0])) |
| 58 | + |
| 59 | +agree = len(set(kuzu_order[:100]) & set(rust_top)) / 100.0 |
| 60 | + |
| 61 | +print(f"\nKuzu (embedded graph DB, native PageRank):") |
| 62 | +print(f" bulk load (COPY) : {load_s*1000:8.1f} ms (+ {csv_s*1000:.0f} ms to write CSV)") |
| 63 | +print(f" PageRank compute : {kuzu_s*1000:8.1f} ms") |
| 64 | +print(f"\nhead-to-head (compute-to-compute, same graph, same machine):") |
| 65 | +print(f" hg_analytics : {rust_parallel_s*1000:7.1f} ms") |
| 66 | +print(f" Kuzu : {kuzu_s*1000:7.1f} ms → hg_analytics is {kuzu_s/rust_parallel_s:.1f}x faster") |
| 67 | +print(f" top-100 ranking agreement: {agree*100:.0f}%") |
| 68 | +print("\nNote: exact PageRank values differ (Kuzu's default damping/iterations vs ours); the ranking is " |
| 69 | + "what agrees. Kuzu also pays a load step we don't (in-memory). Compute-to-compute is the fair line.") |
0 commit comments