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
//! Cold Cache Benchmarks
//!
//! Run with: cargo bench --features v3-bench -- cold_cache
//!
//! Benchmarks V3 backend performance with cold OS page cache.
//! Measures BFS traversal and point lookups after dropping Linux page caches.
//!
//! NOTE: Requires sudo/root privileges to drop caches.
//! Falls back gracefully to warm cache measurements if drop_caches() fails.
use criterion::{BenchmarkId, Criterion, Throughput, black_box, criterion_group, criterion_main};
use std::fs::OpenOptions;
use std::io::Write;
use std::time::Duration;
mod bench_utils;
use bench_utils::BenchmarkState;
use sqlitegraph::backend::native::v3::V3Backend;
use sqlitegraph::backend::{BackendDirection, EdgeSpec, GraphBackend, NeighborQuery, NodeSpec};
use sqlitegraph::snapshot::SnapshotId;
// Benchmark timing constants
const MEASURE: std::time::Duration = std::time::Duration::from_millis(500);
const WARM_UP: std::time::Duration = std::time::Duration::from_millis(300);
// ============================================================================
// DROP CACHES UTILITY
// ============================================================================
/// Attempt to drop Linux page caches to ensure cold cache measurements.
///
/// Requires sudo/root privileges. Falls back gracefully if not available.
fn drop_caches() {
// Try to write to /proc/sys/vm/drop_caches
// This requires root privileges
match OpenOptions::new()
.write(true)
.open("/proc/sys/vm/drop_caches")
{
Ok(mut file) => {
if let Err(e) = file.write_all(b"3\n") {
eprintln!("Warning: Failed to drop caches (write failed): {}", e);
eprintln!("Benchmark will run with warm cache");
} else {
// Sync to ensure caches are dropped
let _ = file.sync_all();
}
}
Err(e) => {
eprintln!("Warning: Failed to open /proc/sys/vm/drop_caches: {}", e);
eprintln!("This is expected without root privileges");
eprintln!("Benchmark will run with warm cache");
}
}
// Give the system a moment to process
std::thread::sleep(Duration::from_millis(100));
}
// ============================================================================
// COLD CACHE BFS BENCHMARK
// ============================================================================
fn bench_cold_cache_bfs(c: &mut Criterion) {
let mut group = c.benchmark_group("cold_cache/bfs");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
group.sample_size(10);
let test_sizes = [1000, 10000, 100000];
for size in test_sizes {
group.throughput(Throughput::Elements(size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
// Setup: Create a fresh database for each iteration
b.iter_batched(
|| {
let temp_dir = bench_utils::create_benchmark_temp_dir();
let db_path = temp_dir.path().join("cold_cache_bfs.db");
let backend = V3Backend::create(&db_path).unwrap();
// Create chain graph: 0 -> 1 -> 2 -> ... -> (size-1)
let mut node_ids = Vec::with_capacity(size);
for i in 0..size {
let node_id = backend
.insert_node(NodeSpec {
kind: "Node".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({ "id": i }),
})
.unwrap();
node_ids.push(node_id);
}
// Create edges (chain)
for i in 0..size.saturating_sub(1) {
backend
.insert_edge(EdgeSpec {
from: node_ids[i],
to: node_ids[i + 1],
edge_type: "NEXT".to_string(),
data: serde_json::json!({}),
})
.unwrap();
}
// Flush to disk
backend.flush().unwrap();
// Drop the backend to close file handles
drop(backend);
// Drop OS page caches
drop_caches();
// Reopen backend (this will be cold cache)
let backend = V3Backend::open(&db_path).unwrap();
(BenchmarkState { backend, temp_dir }, node_ids)
},
|(ctx, node_ids)| {
// BFS traversal from first node
let backend = &ctx.backend;
let snapshot = SnapshotId::current();
let mut visited = std::collections::HashSet::new();
let mut queue = vec![node_ids[0]];
while let Some(node_id) = queue.pop() {
if visited.contains(&node_id) {
continue;
}
visited.insert(node_id);
// Get neighbors using the correct API
let neighbors = backend
.neighbors(
snapshot,
node_id,
NeighborQuery {
direction: BackendDirection::Outgoing,
edge_type: None,
},
)
.unwrap();
for neighbor_id in neighbors {
if !visited.contains(&neighbor_id) {
queue.push(neighbor_id);
}
}
}
black_box(visited.len());
},
criterion::BatchSize::LargeInput,
);
});
}
group.finish();
}
// ============================================================================
// COLD CACHE POINT LOOKUP BENCHMARK
// ============================================================================
fn bench_cold_cache_point_lookup(c: &mut Criterion) {
let mut group = c.benchmark_group("cold_cache/point_lookup");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
group.sample_size(10);
let test_sizes = [1000, 10000, 100000];
for size in test_sizes {
group.throughput(Throughput::Elements(size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
// Setup: Create a fresh database for each iteration
b.iter_batched(
|| {
let temp_dir = bench_utils::create_benchmark_temp_dir();
let db_path = temp_dir.path().join("cold_cache_lookup.db");
let backend = V3Backend::create(&db_path).unwrap();
// Create nodes
let mut node_ids = Vec::with_capacity(size);
for i in 0..size {
let node_id = backend
.insert_node(NodeSpec {
kind: "Node".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({ "id": i }),
})
.unwrap();
node_ids.push(node_id);
}
// Flush to disk
backend.flush().unwrap();
// Drop the backend to close file handles
drop(backend);
// Drop OS page caches
drop_caches();
// Reopen backend (this will be cold cache)
let backend = V3Backend::open(&db_path).unwrap();
(BenchmarkState { backend, temp_dir }, node_ids)
},
|(ctx, node_ids)| {
// Point lookups: fetch every 10th node
let backend = &ctx.backend;
let snapshot = SnapshotId::current();
let mut count = 0;
for i in (0..size).step_by(10) {
let node = backend.get_node(snapshot, node_ids[i]).unwrap();
black_box(node);
count += 1;
}
black_box(count);
},
criterion::BatchSize::LargeInput,
);
});
}
group.finish();
}
// ============================================================================
// COLD CACHE SEQUENTIAL SCAN BENCHMARK
// ============================================================================
fn bench_cold_cache_sequential_scan(c: &mut Criterion) {
let mut group = c.benchmark_group("cold_cache/sequential_scan");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
group.sample_size(10);
let test_sizes = [1000, 10000, 100000];
for size in test_sizes {
group.throughput(Throughput::Elements(size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
// Setup: Create a fresh database for each iteration
b.iter_batched(
|| {
let temp_dir = bench_utils::create_benchmark_temp_dir();
let db_path = temp_dir.path().join("cold_cache_scan.db");
let backend = V3Backend::create(&db_path).unwrap();
// Create nodes
let mut node_ids = Vec::with_capacity(size);
for i in 0..size {
let node_id = backend
.insert_node(NodeSpec {
kind: "Node".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({ "id": i }),
})
.unwrap();
node_ids.push(node_id);
}
// Flush to disk
backend.flush().unwrap();
// Drop the backend to close file handles
drop(backend);
// Drop OS page caches
drop_caches();
// Reopen backend (this will be cold cache)
let backend = V3Backend::open(&db_path).unwrap();
(BenchmarkState { backend, temp_dir }, node_ids)
},
|(ctx, node_ids)| {
// Sequential scan: fetch all nodes in order
let backend = &ctx.backend;
let snapshot = SnapshotId::current();
let mut count = 0;
for node_id in &node_ids {
let node = backend.get_node(snapshot, *node_id).unwrap();
black_box(node);
count += 1;
}
black_box(count);
},
criterion::BatchSize::LargeInput,
);
});
}
group.finish();
}
criterion_group!(
benches,
bench_cold_cache_bfs,
bench_cold_cache_point_lookup,
bench_cold_cache_sequential_scan,
);
criterion_main!(benches);