1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! Write cost regression benchmark for cluster metadata.
//!
//! Measures write-path cost with cluster metadata to ensure ≤+5% increase vs v1.6 baseline.
//! This validates that the observe_with_cluster() optimization doesn't degrade write performance.
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use sqlitegraph::{EdgeSpec, NodeSpec, open_graph};
mod bench_utils;
use bench_utils::{MEASURE, WARM_UP, create_benchmark_temp_dir};
/// Benchmark write operations with cluster metadata
///
/// Measures time to write N nodes with edges in a chain pattern.
/// Target: ≤+5% increase vs v1.6 baseline (without cluster metadata tracking).
fn bench_write_cost_with_metadata(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("regression_write_cost");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
// Test various graph sizes to detect scaling issues
for &size in &[100, 500, 1000, 5000] {
group.bench_with_input(
BenchmarkId::new("native_with_metadata", size),
&size,
|b, &size| {
b.iter(|| {
let temp_dir = create_benchmark_temp_dir();
let db_path = temp_dir.path().join("benchmark.db");
let graph = open_graph(&db_path, &sqlitegraph::GraphConfig::native())
.expect("Failed to create graph");
// Create nodes
let mut node_ids = Vec::with_capacity(size);
for i in 0..size {
let node_id = graph
.insert_node(NodeSpec {
kind: "Node".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({
"id": i,
"created_at": "regression_test",
}),
})
.expect("Failed to insert node");
node_ids.push(node_id);
}
// Create chain edges (linear pattern)
for i in 0..size.saturating_sub(1) {
graph
.insert_edge(EdgeSpec {
from: node_ids[i],
to: node_ids[i + 1],
edge_type: "chain".to_string(),
data: serde_json::json!({"order": i}),
})
.expect("Failed to insert edge");
}
std::mem::forget(temp_dir);
});
},
);
}
group.finish();
}
/// Benchmark baseline write cost (without cluster metadata)
///
/// This serves as a reference point. Since we can't disable cluster metadata
/// in the current implementation, this measures the same path and provides
/// a baseline for future comparisons.
fn bench_write_cost_baseline(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("regression_write_baseline");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
// Measure the same operations to establish baseline
for &size in &[100, 500, 1000, 5000] {
group.bench_with_input(
BenchmarkId::new("native_baseline", size),
&size,
|b, &size| {
b.iter(|| {
let temp_dir = create_benchmark_temp_dir();
let db_path = temp_dir.path().join("benchmark.db");
let graph = open_graph(&db_path, &sqlitegraph::GraphConfig::native())
.expect("Failed to create graph");
// Create nodes
let mut node_ids = Vec::with_capacity(size);
for i in 0..size {
let node_id = graph
.insert_node(NodeSpec {
kind: "Node".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({
"id": i,
"created_at": "baseline_test",
}),
})
.expect("Failed to insert node");
node_ids.push(node_id);
}
// Create chain edges
for i in 0..size.saturating_sub(1) {
graph
.insert_edge(EdgeSpec {
from: node_ids[i],
to: node_ids[i + 1],
edge_type: "chain".to_string(),
data: serde_json::json!({"order": i}),
})
.expect("Failed to insert edge");
}
std::mem::forget(temp_dir);
});
},
);
}
group.finish();
}
/// Benchmark write operations per 1000 for normalization
///
/// This provides normalized metrics for comparison across different graph sizes.
fn bench_write_cost_per_operation(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("regression_write_per_1k");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
// Fixed size for per-operation measurement
const SIZE: usize = 1000;
group.bench_function("native_1k_ops", |b| {
b.iter(|| {
let temp_dir = create_benchmark_temp_dir();
let db_path = temp_dir.path().join("benchmark.db");
let graph = open_graph(&db_path, &sqlitegraph::GraphConfig::native())
.expect("Failed to create graph");
// Create 1000 nodes
let mut node_ids = Vec::with_capacity(SIZE);
for i in 0..SIZE {
let node_id = graph
.insert_node(NodeSpec {
kind: "Node".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({"id": i}),
})
.expect("Failed to insert node");
node_ids.push(node_id);
}
// Create chain edges
for i in 0..SIZE.saturating_sub(1) {
graph
.insert_edge(EdgeSpec {
from: node_ids[i],
to: node_ids[i + 1],
edge_type: "chain".to_string(),
data: serde_json::json!({"order": i}),
})
.expect("Failed to insert edge");
}
std::mem::forget(temp_dir);
});
});
group.finish();
}
criterion_group!(
benches,
bench_write_cost_with_metadata,
bench_write_cost_baseline,
bench_write_cost_per_operation
);
criterion_main!(benches);