graph_api_benches/
edge.rs

1use crate::generators::generate_test_graph;
2
3use criterion::{BenchmarkGroup, Throughput, measurement::WallTime};
4use graph_api_lib::{EdgeReference, Graph};
5use graph_api_test::{Edge, Knows, Vertex};
6
7/// Run all edge operation benchmarks
8pub fn run_benchmarks<G: Graph<Vertex = Vertex, Edge = Edge>>(
9    group: &mut BenchmarkGroup<WallTime>,
10    setup: impl Fn() -> G + Clone,
11) {
12    bench_edge_add(group, setup.clone());
13    bench_edge_retrieve(group, setup.clone());
14    bench_edge_remove(group, setup.clone());
15    bench_edge_property_access(group, setup.clone());
16}
17
18/// Benchmark adding an edge
19fn bench_edge_add<G: Graph<Vertex = Vertex, Edge = Edge>>(
20    group: &mut BenchmarkGroup<WallTime>,
21    setup: impl Fn() -> G + Clone,
22) {
23    group.throughput(Throughput::Elements(1));
24    group.bench_function("edge_add", |b| {
25        b.iter_batched(
26            || {
27                // Setup: Create graph with test data
28                let mut graph = setup();
29                let refs = generate_test_graph(&mut graph);
30                (graph, refs.bryn, refs.julia)
31            },
32            |(mut graph, source, target)| {
33                graph.add_edge(source, target, Edge::Knows { since: 2023 })
34            },
35            criterion::BatchSize::SmallInput,
36        )
37    });
38}
39
40/// Benchmark retrieving an edge by ID
41fn bench_edge_retrieve<G: Graph<Vertex = Vertex, Edge = Edge>>(
42    group: &mut BenchmarkGroup<WallTime>,
43    setup: impl Fn() -> G + Clone,
44) {
45    group.throughput(Throughput::Elements(1));
46    group.bench_function("edge_retrieve", |b| {
47        // Setup: Create graph with test data
48        let mut graph = setup();
49        let refs = generate_test_graph(&mut graph);
50
51        b.iter(|| graph.edge(refs.bryn_knows_julia))
52    });
53}
54
55/// Benchmark removing an edge
56fn bench_edge_remove<G: Graph<Vertex = Vertex, Edge = Edge>>(
57    group: &mut BenchmarkGroup<WallTime>,
58    setup: impl Fn() -> G + Clone,
59) {
60    group.throughput(Throughput::Elements(1));
61    group.bench_function("edge_remove", |b| {
62        b.iter_batched(
63            || {
64                // Setup: Create graph with random data
65                let mut graph = setup();
66                let refs = generate_test_graph(&mut graph);
67                // Add a new edge that we'll remove during the benchmark
68                let edge_id = graph.add_edge(refs.bryn, refs.julia, Edge::Knows { since: 2023 });
69                (graph, edge_id)
70            },
71            |(mut graph, edge_id)| graph.remove_edge(edge_id),
72            criterion::BatchSize::SmallInput,
73        )
74    });
75}
76
77/// Benchmark accessing edge properties
78fn bench_edge_property_access<G: Graph<Vertex = Vertex, Edge = Edge>>(
79    group: &mut BenchmarkGroup<WallTime>,
80    setup: impl Fn() -> G + Clone,
81) {
82    group.throughput(Throughput::Elements(1));
83    group.bench_function("edge_property_access", |b| {
84        // Setup: Create graph with test data
85        let mut graph = setup();
86        let refs = generate_test_graph(&mut graph);
87
88        b.iter(|| {
89            let edge = graph.edge(refs.bryn_knows_julia).unwrap();
90            let knows = edge.project::<Knows<_>>().expect("knows");
91            knows.since()
92        })
93    });
94}