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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//! Write cost regression benchmark for pub/sub emission.
//!
//! Measures write-path cost with pub/sub event emission to ensure ≤+10% increase vs baseline.
//! This validates that publisher.emit() on the commit path doesn't degrade write performance.
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use sqlitegraph::backend::SubscriptionFilter;
use sqlitegraph::{EdgeSpec, NodeSpec, open_graph};
mod bench_utils;
use bench_utils::{MEASURE, WARM_UP, create_benchmark_temp_dir};
/// Benchmark write cost with pub/sub (0 subscribers - baseline)
///
/// Creates a Publisher but has no subscribers. This measures the overhead
/// of emit() calls when there are no receivers to deliver to.
fn bench_write_cost_no_subscribers(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("regression_pubsub_write_baseline");
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("no_subscribers", 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 write cost with N subscribers (receivers dropped immediately)
///
/// Subscribes N receivers but drops them immediately. This isolates the emit()
/// cost from receiver processing cost. We measure channel send overhead without
/// waiting for receivers to consume events.
fn bench_write_cost_with_subscribers(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("regression_pubsub_write_with_subs");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
// Test with different subscriber counts
for &subscriber_count in &[1, 5, 10] {
let size = 1000; // Fixed size for subscriber comparison
group.bench_with_input(
BenchmarkId::new("with_subscribers", subscriber_count),
&subscriber_count,
|b, &subscriber_count| {
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");
// Subscribe N receivers and drop them immediately
// This measures emit() overhead without receiver processing
for _ in 0..subscriber_count {
let (_id, _rx) = graph
.subscribe(SubscriptionFilter::all())
.expect("Failed to subscribe");
// Drop rx immediately - we only measure emit() cost
}
// 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}),
})
.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
///
/// Provides normalized metrics for comparison across different subscriber counts.
/// All benchmarks use 1000 operations.
fn bench_write_cost_per_operation(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("regression_pubsub_write_per_1k");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
const SIZE: usize = 1000;
// Benchmark with 0 subscribers (baseline)
group.bench_function("0_subscribers", |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);
});
});
// Benchmark with 1 subscriber
group.bench_function("1_subscriber", |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");
// Subscribe and drop receiver immediately
let (_id, _rx) = graph
.subscribe(SubscriptionFilter::all())
.expect("Failed to subscribe");
// 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);
});
});
// Benchmark with 5 subscribers
group.bench_function("5_subscribers", |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");
// Subscribe 5 receivers and drop them
for _ in 0..5 {
let (_id, _rx) = graph
.subscribe(SubscriptionFilter::all())
.expect("Failed to subscribe");
}
// 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);
});
});
// Benchmark with 10 subscribers
group.bench_function("10_subscribers", |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");
// Subscribe 10 receivers and drop them
for _ in 0..10 {
let (_id, _rx) = graph
.subscribe(SubscriptionFilter::all())
.expect("Failed to subscribe");
}
// 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_no_subscribers,
bench_write_cost_with_subscribers,
bench_write_cost_per_operation
);
criterion_main!(benches);