graph_api_benches/
query.rs

1use crate::generators::{GraphSize, generate_random_graph, generate_test_graph};
2use crate::{Edge, Vertex};
3use criterion::{BenchmarkGroup, Throughput, measurement::WallTime};
4use graph_api_lib::{EdgeSearch, Graph, VertexSearch};
5
6/// Run all query operation benchmarks
7pub fn run_benchmarks<G: Graph<Vertex = Vertex, Edge = Edge>>(
8    group: &mut BenchmarkGroup<WallTime>,
9    setup: impl Fn() -> G + Clone,
10) {
11    bench_query_count_vertices(group, setup.clone());
12    bench_query_count_edges(group, setup.clone());
13    bench_query_first(group, setup.clone());
14    bench_query_collect(group, setup.clone());
15    bench_count(group, setup.clone());
16}
17
18/// Benchmark counting vertices
19fn bench_query_count_vertices<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("query_count_vertices", |b| {
25        // Setup: Create graph with random data
26        let mut graph = setup();
27        generate_random_graph(&mut graph, GraphSize::Small, 42);
28
29        b.iter(|| graph.walk().vertices(VertexSearch::scan()).count())
30    });
31}
32
33/// Benchmark counting edges
34fn bench_query_count_edges<G: Graph<Vertex = Vertex, Edge = Edge>>(
35    group: &mut BenchmarkGroup<WallTime>,
36    setup: impl Fn() -> G + Clone,
37) {
38    group.throughput(Throughput::Elements(1));
39    group.bench_function("query_count_edges", |b| {
40        // Setup: Create graph with random data
41        let mut graph = setup();
42        generate_random_graph(&mut graph, GraphSize::Small, 42);
43
44        b.iter(|| {
45            graph
46                .walk()
47                .vertices(VertexSearch::scan())
48                .edges(EdgeSearch::scan())
49                .count()
50        })
51    });
52}
53
54/// Benchmark finding first vertex matching criteria
55fn bench_query_first<G: Graph<Vertex = Vertex, Edge = Edge>>(
56    group: &mut BenchmarkGroup<WallTime>,
57    setup: impl Fn() -> G + Clone,
58) {
59    group.throughput(Throughput::Elements(1));
60    group.bench_function("query_first", |b| {
61        // Setup: Create graph with test data
62        let mut graph = setup();
63        generate_test_graph(&mut graph);
64
65        b.iter(|| graph.walk().vertices(VertexSearch::scan()).first())
66    });
67}
68
69/// Benchmark collecting results into a collection
70fn bench_query_collect<G: Graph<Vertex = Vertex, Edge = Edge>>(
71    group: &mut BenchmarkGroup<WallTime>,
72    setup: impl Fn() -> G + Clone,
73) {
74    group.throughput(Throughput::Elements(1));
75    group.bench_function("query_collect", |b| {
76        // Setup: Create graph with random data
77        let mut graph = setup();
78        generate_random_graph(&mut graph, GraphSize::Small, 42);
79
80        b.iter(|| {
81            graph
82                .walk()
83                .vertices(VertexSearch::scan())
84                .take(25)
85                .collect::<Vec<_>>()
86        })
87    });
88}
89
90/// Benchmark checking raw iteration via count
91fn bench_count<G: Graph<Vertex = Vertex, Edge = Edge>>(
92    group: &mut BenchmarkGroup<WallTime>,
93    setup: impl Fn() -> G + Clone,
94) {
95    group.throughput(Throughput::Elements(1));
96    group.bench_function("query_is_empty", |b| {
97        // Setup: Create graph with test data
98        let mut graph = setup();
99        generate_test_graph(&mut graph);
100
101        b.iter(|| graph.walk().vertices(VertexSearch::scan()).take(0).count())
102    });
103}