graph_api_benches/
construction.rs1use crate::generators::{
2 GraphSize, generate_project_graph, generate_random_graph, generate_social_graph,
3};
4
5use criterion::{BenchmarkGroup, Throughput, measurement::WallTime};
6use graph_api_lib::Graph;
7use graph_api_test::{Edge, Vertex};
8
9pub fn run_benchmarks<G: Graph<Vertex = Vertex, Edge = Edge>>(
11 group: &mut BenchmarkGroup<WallTime>,
12 setup: impl Fn() -> G + Clone,
13) {
14 bench_construction_small(group, setup.clone());
15 bench_construction_medium(group, setup.clone());
16 bench_construction_social(group, setup.clone());
17 bench_construction_project(group, setup.clone());
18 bench_construction_batch(group, setup.clone());
19}
20
21fn bench_construction_small<G: Graph<Vertex = Vertex, Edge = Edge>>(
23 group: &mut BenchmarkGroup<WallTime>,
24 setup: impl Fn() -> G + Clone,
25) {
26 let size = GraphSize::Small;
27 let vertex_count = size.vertex_count();
28 let edge_count = vertex_count * size.edge_multiplier();
29
30 group.throughput(Throughput::Elements((vertex_count + edge_count) as u64));
31 group.bench_function("construction_small", |b| {
32 b.iter_batched(
33 &setup,
34 |mut graph| {
35 generate_random_graph(&mut graph, size, 42);
36 },
37 criterion::BatchSize::SmallInput,
38 )
39 });
40}
41
42fn bench_construction_medium<G: Graph<Vertex = Vertex, Edge = Edge>>(
44 group: &mut BenchmarkGroup<WallTime>,
45 setup: impl Fn() -> G + Clone,
46) {
47 let size = GraphSize::Medium;
48 let vertex_count = size.vertex_count();
49 let edge_count = vertex_count * size.edge_multiplier();
50
51 group.throughput(Throughput::Elements((vertex_count + edge_count) as u64));
52 group.bench_function("construction_medium", |b| {
53 b.iter_batched(
54 &setup,
55 |mut graph| {
56 generate_random_graph(&mut graph, size, 42);
57 },
58 criterion::BatchSize::LargeInput,
59 )
60 });
61}
62
63fn bench_construction_social<G: Graph<Vertex = Vertex, Edge = Edge>>(
65 group: &mut BenchmarkGroup<WallTime>,
66 setup: impl Fn() -> G + Clone,
67) {
68 let size = GraphSize::Small;
69 let vertex_count = size.vertex_count();
70 let edge_count = vertex_count * size.edge_multiplier();
71
72 group.throughput(Throughput::Elements((vertex_count + edge_count) as u64));
73 group.bench_function("construction_social", |b| {
74 b.iter_batched(
75 &setup,
76 |mut graph| {
77 generate_social_graph(&mut graph, size, 42);
78 },
79 criterion::BatchSize::SmallInput,
80 )
81 });
82}
83
84fn bench_construction_project<G: Graph<Vertex = Vertex, Edge = Edge>>(
86 group: &mut BenchmarkGroup<WallTime>,
87 setup: impl Fn() -> G + Clone,
88) {
89 let size = GraphSize::Small;
90 let vertex_count = size.vertex_count();
91 let edge_count = vertex_count * size.edge_multiplier();
92
93 group.throughput(Throughput::Elements((vertex_count + edge_count) as u64));
94 group.bench_function("construction_project", |b| {
95 b.iter_batched(
96 &setup,
97 |mut graph| {
98 generate_project_graph(&mut graph, size, 42);
99 },
100 criterion::BatchSize::SmallInput,
101 )
102 });
103}
104
105fn bench_construction_batch<G: Graph<Vertex = Vertex, Edge = Edge>>(
107 group: &mut BenchmarkGroup<WallTime>,
108 setup: impl Fn() -> G + Clone,
109) {
110 const BATCH_SIZE: usize = 100;
111
112 group.throughput(Throughput::Elements(BATCH_SIZE as u64));
113 group.bench_function("construction_batch_vertex", |b| {
114 b.iter_batched(
115 &setup,
116 |mut graph| {
117 for i in 0..BATCH_SIZE {
119 graph.add_vertex(Vertex::Person {
120 name: format!("BatchPerson-{}", i),
121 age: 30 + (i % 50) as u64,
122 unique_id: uuid::Uuid::new_v4(),
123 username: format!("batch_user_{}", i),
124 biography: format!("Batch created person {}", i),
125 });
126 }
127 },
128 criterion::BatchSize::SmallInput,
129 )
130 });
131}