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
//! Memory profiling benchmarks for V3 backend.
//!
//! Run with: cargo bench --features memory_profiling -- memory_profiling
//!
//! Measures actual memory usage during graph operations:
//! - Memory per 1000 nodes during insertion (1K, 10K, 100K)
//! - Memory growth during BFS traversal (1000, 10000 nodes)
#[cfg(feature = "memory_profiling")]
use criterion::{BenchmarkId, Criterion, Throughput, black_box, criterion_group, criterion_main};
use sqlitegraph::backend::{BackendDirection, EdgeSpec, GraphBackend, NeighborQuery, NodeSpec};
use sqlitegraph::snapshot::SnapshotId;
mod bench_utils;
#[cfg(feature = "memory_profiling")]
use bench_utils::{MEASURE, WARM_UP, create_v3_bench_context};
// ============================================================================
// MEMORY PROFILING UTILITIES
// ============================================================================
/// Read current RSS (Resident Set Size) in bytes from /proc/self/status
///
/// Returns the VmRSS value which represents the actual physical memory used.
/// Returns 0 on unsupported platforms (non-Linux).
#[cfg(feature = "memory_profiling")]
fn get_rss_bytes() -> usize {
use std::fs::File;
use std::io::BufRead;
if let Ok(file) = File::open("/proc/self/status") {
let reader = std::io::BufReader::new(file);
for line in reader.lines().flatten() {
if line.starts_with("VmRSS:") {
// Format: "VmRSS: 12345 kB"
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 {
if let Ok(kb) = parts[1].parse::<usize>() {
return kb * 1024; // Convert KB to bytes
}
}
}
}
}
0 // Unsupported platform or parse failure
}
// ============================================================================
// MEMORY PER 1000 NODES BENCHMARK
// ============================================================================
#[cfg(feature = "memory_profiling")]
fn bench_memory_per_1000_nodes(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("memory_per_1000_nodes");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
group.sample_size(10);
for thousand_nodes in [1, 10, 100] {
let size = thousand_nodes * 1000;
group.throughput(Throughput::Elements(size as u64));
group.bench_with_input(
BenchmarkId::new("insertion", thousand_nodes),
&size,
|b, &size| {
b.iter_batched(
|| create_v3_bench_context("v3.db"),
|ctx| {
let backend = &ctx.backend;
let rss_before = get_rss_bytes();
// Insert nodes
for i in 0..size {
black_box(
backend
.insert_node(NodeSpec {
kind: "Node".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({"id": i}),
})
.unwrap(),
);
}
let rss_after = get_rss_bytes();
// Calculate memory per 1000 nodes
let memory_delta = rss_after.saturating_sub(rss_before);
let per_1000 = memory_delta / (size / 1000).max(1);
// Report as metric (bytes per 1000 nodes)
black_box(per_1000);
},
criterion::BatchSize::LargeInput,
);
},
);
}
group.finish();
}
// ============================================================================
// MEMORY DURING TRAVERSAL BENCHMARK
// ============================================================================
#[cfg(feature = "memory_profiling")]
fn bench_memory_during_traversal(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("memory_during_traversal");
group.measurement_time(MEASURE);
group.warm_up_time(WARM_UP);
group.sample_size(10);
for size in [1000, 10000] {
group.throughput(Throughput::Elements(size as u64));
group.bench_with_input(
BenchmarkId::new("bfs_traversal", size),
&size,
|b, &size| {
b.iter_batched(
|| {
let ctx = create_v3_bench_context("v3.db");
let backend = &ctx.backend;
// Create a chain graph for traversal
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 chain edges
if i > 0 {
backend
.insert_edge(EdgeSpec {
from: node_ids[i - 1],
to: node_id,
edge_type: "chain".to_string(),
data: serde_json::json!({"order": i}),
})
.unwrap();
}
}
(ctx, node_ids)
},
|(ctx, node_ids)| {
let backend = &ctx.backend;
let snapshot = SnapshotId::current();
let rss_before = get_rss_bytes();
// BFS traversal
let start_node = node_ids[0];
let mut visited = std::collections::HashSet::new();
let mut queue = vec![start_node];
visited.insert(start_node);
while let Some(node_id) = queue.pop() {
let neighbors = backend
.neighbors(
snapshot,
node_id,
NeighborQuery {
direction: BackendDirection::Outgoing,
edge_type: None,
},
)
.unwrap();
for &neighbor in &neighbors {
if visited.insert(neighbor) {
queue.push(neighbor);
}
}
}
let rss_after = get_rss_bytes();
let memory_growth = rss_after.saturating_sub(rss_before);
// Report memory growth during traversal
black_box(memory_growth);
},
criterion::BatchSize::LargeInput,
);
},
);
}
group.finish();
}
// ============================================================================
// CRITERION MAIN
// ============================================================================
#[cfg(feature = "memory_profiling")]
criterion_group!(
benches,
bench_memory_per_1000_nodes,
bench_memory_during_traversal
);
#[cfg(feature = "memory_profiling")]
criterion_main!(benches);
// ============================================================================
// STUB MAIN WHEN FEATURE NOT ENABLED
// ============================================================================
#[cfg(not(feature = "memory_profiling"))]
fn main() {
eprintln!("ERROR: Memory profiling benchmarks require the 'memory_profiling' feature.");
eprintln!("Run with: cargo bench --features memory_profiling -- memory_profiling");
std::process::exit(1);
}