cesiumdb 0.1.0

Blazing fast, persistent key-value store for Rust
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::{
    ops::Bound,
    sync::Arc,
};

use bytes::Bytes;
use cesiumdb::{
    compact::compact,
    compaction::{
        AdaptiveExecutor,
        CompactionExecutor,
        CompactionJob,
        CompactionQueue,
        ResourceLimits,
    },
    hlc::{
        HLC,
        HybridLogicalClock,
    },
    keypair::{
        DEFAULT_NS,
        KeyBytes,
        ValueBytes,
    },
    memtable::Memtable,
    segment::Segment,
    version::VersionManager,
};
use criterion::{
    BatchSize,
    BenchmarkId,
    Criterion,
    Throughput,
    black_box,
    criterion_group,
    criterion_main,
};
use mimalloc::MiMalloc;
use rand::Rng;
use tempfile::TempDir;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

/// Test data generator for compaction benchmarks
struct CompactionTestData {
    /// Memtables to be flushed/compacted
    memtables: Vec<Arc<Memtable>>,
    /// Output directory for segments
    output_dir: TempDir,
    /// Clock for timestamps
    clock: HybridLogicalClock,
}

impl CompactionTestData {
    /// Creates test data with configurable characteristics
    fn new(
        num_memtables: usize,
        keys_per_table: usize,
        key_overlap_pct: f64,
        tombstone_pct: f64,
    ) -> Self {
        let clock = HybridLogicalClock::new();
        let mut rng = rand::rng();
        let mut memtables = Vec::with_capacity(num_memtables);

        // Create memtables
        for i in 0..num_memtables {
            let table = Memtable::new(i as u64, 1024 * 1024 * 256);
            memtables.push(Arc::new(table));
        }

        // Populate memtables
        for table_idx in 0..num_memtables {
            let start_key = if key_overlap_pct > 0.0 {
                // With overlap, tables share some key ranges
                (table_idx as f64 * keys_per_table as f64 * (1.0 - key_overlap_pct)) as usize
            } else {
                // No overlap - disjoint key ranges
                table_idx * keys_per_table
            };

            for i in 0..keys_per_table {
                let key_idx = start_key + i;
                let key = format!("key_{:010}", key_idx);
                let ts = clock.time();

                let key_bytes = KeyBytes::new(DEFAULT_NS, Bytes::from(key), ts);

                // Generate tombstones based on percentage
                let value_bytes = if rng.random_bool(tombstone_pct) {
                    ValueBytes::new_tombstone(DEFAULT_NS)
                } else {
                    let value = format!("value_{}", key_idx);
                    ValueBytes::new(DEFAULT_NS, Bytes::from(value))
                };

                let _ = memtables[table_idx].put(key_bytes, value_bytes);
            }
        }

        Self {
            memtables,
            output_dir: TempDir::new().unwrap(),
            clock,
        }
    }

    /// Flush all memtables to segments for compaction testing
    fn flush_to_segments(&self) -> Vec<Arc<Segment>> {
        use cesiumdb::compact::flush_memtable;

        let mut segments = Vec::new();
        for (idx, memtable) in self.memtables.iter().enumerate() {
            let path = self
                .output_dir
                .path()
                .join("segments")
                .join(idx.to_string());
            let segment_id = idx as u64 + 1;

            match flush_memtable(memtable.clone(), path, segment_id) {
                | Ok((segment, _, _)) => segments.push(segment),
                | Err(e) => eprintln!("Flush failed: {:?}", e),
            }
        }
        segments
    }
}

/// Benchmark: Full compaction throughput
///
/// Measures how fast we can compact N segments into one
fn bench_full_compaction_throughput(c: &mut Criterion) {
    let mut group = c.benchmark_group("compaction_throughput");

    for num_segments in [2, 4, 8, 16] {
        for keys_per_segment in [1000, 10_000] {
            group.throughput(Throughput::Elements(
                (num_segments * keys_per_segment) as u64,
            ));

            group.bench_with_input(
                BenchmarkId::new(format!("segments/{}/keys", num_segments), keys_per_segment),
                &(num_segments, keys_per_segment),
                |b, &(num_seg, keys)| {
                    b.iter_batched(
                        || {
                            // Setup: create segments
                            let test_data = CompactionTestData::new(num_seg, keys, 0.0, 0.0); // No overlap, no tombstones
                            let segments = test_data.flush_to_segments();
                            (test_data.output_dir.path().to_path_buf(), segments)
                        },
                        |(output_path, segments)| {
                            // Benchmark: compact all segments
                            let readers: Vec<_> = segments
                                .iter()
                                .filter_map(|seg| seg.reader().ok())
                                .collect();

                            let iterators: Vec<_> = readers
                                .into_iter()
                                .map(|reader| {
                                    reader
                                        .scan(Bound::Unbounded, Bound::Unbounded)
                                        .filter_map(|r| r.ok())
                                })
                                .collect();

                            let output_dir = output_path.join("output");
                            let result = compact(iterators, output_dir, 999);
                            black_box(result);
                        },
                        BatchSize::LargeInput,
                    )
                },
            );
        }
    }

    group.finish();
}

/// Benchmark: Compaction with key overlap
///
/// Tests performance impact of overlapping key ranges
fn bench_compaction_with_overlap(c: &mut Criterion) {
    let mut group = c.benchmark_group("compaction_overlap");

    let num_segments = 4;
    let keys_per_segment = 10_000;

    for overlap_pct in [0.0, 0.25, 0.5, 0.75] {
        group.throughput(Throughput::Elements(
            (num_segments * keys_per_segment) as u64,
        ));

        group.bench_with_input(
            BenchmarkId::new("overlap_pct", (overlap_pct * 100.0) as u32),
            &overlap_pct,
            |b, &overlap| {
                b.iter_batched(
                    || {
                        let test_data =
                            CompactionTestData::new(num_segments, keys_per_segment, overlap, 0.0);
                        let segments = test_data.flush_to_segments();
                        (test_data.output_dir.path().to_path_buf(), segments)
                    },
                    |(output_path, segments)| {
                        let readers: Vec<_> = segments
                            .iter()
                            .filter_map(|seg| seg.reader().ok())
                            .collect();

                        let iterators: Vec<_> = readers
                            .into_iter()
                            .map(|reader| {
                                reader
                                    .scan(Bound::Unbounded, Bound::Unbounded)
                                    .filter_map(|r| r.ok())
                            })
                            .collect();

                        let output_dir = output_path.join("output");
                        let result = compact(iterators, output_dir, 999);
                        black_box(result);
                    },
                    BatchSize::LargeInput,
                )
            },
        );
    }

    group.finish();
}

/// Benchmark: Compaction with tombstones
///
/// Tests performance impact of tombstone handling
fn bench_compaction_with_tombstones(c: &mut Criterion) {
    let mut group = c.benchmark_group("compaction_tombstones");

    let num_segments = 4;
    let keys_per_segment = 10_000;

    for tombstone_pct in [0.0, 0.1, 0.25, 0.5] {
        group.throughput(Throughput::Elements(
            (num_segments * keys_per_segment) as u64,
        ));

        group.bench_with_input(
            BenchmarkId::new("tombstone_pct", (tombstone_pct * 100.0) as u32),
            &tombstone_pct,
            |b, &tombstones| {
                b.iter_batched(
                    || {
                        let test_data = CompactionTestData::new(
                            num_segments,
                            keys_per_segment,
                            0.0,
                            tombstones,
                        );
                        let segments = test_data.flush_to_segments();
                        (test_data.output_dir.path().to_path_buf(), segments)
                    },
                    |(output_path, segments)| {
                        let readers: Vec<_> = segments
                            .iter()
                            .filter_map(|seg| seg.reader().ok())
                            .collect();

                        let iterators: Vec<_> = readers
                            .into_iter()
                            .map(|reader| {
                                reader
                                    .scan(Bound::Unbounded, Bound::Unbounded)
                                    .filter_map(|r| r.ok())
                            })
                            .collect();

                        let output_dir = output_path.join("output");
                        let result = compact(iterators, output_dir, 999);
                        black_box(result);
                    },
                    BatchSize::LargeInput,
                )
            },
        );
    }

    group.finish();
}

/// Benchmark: Adaptive executor performance
///
/// Tests the overhead and scaling of the adaptive executor
fn bench_adaptive_executor(c: &mut Criterion) {
    let mut group = c.benchmark_group("adaptive_executor");

    // Test job submission and processing overhead
    group.bench_function("job_submission_overhead", |b| {
        b.iter_batched(
            || {
                let temp_dir = TempDir::new().unwrap();
                let version_manager = Arc::new(VersionManager::new(7));
                let executor_impl = Arc::new(CompactionExecutor::new(
                    Arc::clone(&version_manager),
                    temp_dir.path().to_path_buf(),
                    None,
                ));
                let queue = Arc::new(CompactionQueue::new());
                let limits = ResourceLimits {
                    min_workers: 1,
                    max_workers: 2,
                    ..Default::default()
                };

                let executor =
                    AdaptiveExecutor::new(executor_impl, queue.clone(), version_manager, limits);

                (executor, queue)
            },
            |(executor, _queue)| {
                // Create dummy jobs and submit them
                for i in 0..100 {
                    let job = create_dummy_job(i);
                    executor.submit(job);
                }

                // Wait a bit for processing
                std::thread::sleep(std::time::Duration::from_millis(100));

                let stats = executor.usage();
                black_box(stats);

                executor.shutdown();
            },
            BatchSize::SmallInput,
        )
    });

    group.finish();
}

/// Benchmark: Queue operations
///
/// Tests compaction queue enqueue/dequeue performance
fn bench_queue_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("compaction_queue");

    group.bench_function("enqueue_dequeue_1000_jobs", |b| {
        b.iter(|| {
            let queue = CompactionQueue::new();

            // Enqueue 1000 jobs
            for i in 0..1000 {
                let job = create_dummy_job(i);
                queue.enqueue(job);
            }

            // Dequeue all
            let mut count = 0;
            while let Some(job) = queue.dequeue() {
                black_box(&job);
                queue.mark_completed(job);
                count += 1;
            }

            black_box(count);
        })
    });

    group.finish();
}

/// Helper: Create a dummy compaction job for testing
fn create_dummy_job(id: u64) -> CompactionJob {
    use cesiumdb::{
        compaction::job::{
            CompactionInput,
            CompactionJobType,
            CompactionOutput,
        },
        levels::KeyRange,
    };

    let input = CompactionInput {
        level: 0,
        segments: vec![],
        key_range: KeyRange::new(vec![], vec![], 0),
        total_size: 0,
    };

    let output = CompactionOutput::new(1, 64 * 1024 * 1024);

    CompactionJob::new(id, CompactionJobType::Flush, input, None, output, vec![])
}

criterion_group!(
    benches,
    bench_full_compaction_throughput,
    bench_compaction_with_overlap,
    bench_compaction_with_tombstones,
    bench_adaptive_executor,
    bench_queue_operations
);
criterion_main!(benches);