|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) |
| 3 | +// |
| 4 | +// Criterion benchmarks for JanusKey operations |
| 5 | +// Measures: key gen, content store, hashing, obliteration, transactions |
| 6 | + |
| 7 | +use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkId}; |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +/// Benchmark SHA256 hashing (content-addressed storage core) |
| 11 | +fn bench_hashing(c: &mut Criterion) { |
| 12 | + let mut group = c.benchmark_group("hashing/sha256"); |
| 13 | + |
| 14 | + for size in [32, 256, 1024, 4096, 65536, 1_048_576] { |
| 15 | + group.bench_with_input( |
| 16 | + BenchmarkId::new("bytes", size), |
| 17 | + &size, |
| 18 | + |b, &size| { |
| 19 | + let data = vec![0xABu8; size]; |
| 20 | + b.iter(|| { |
| 21 | + use std::collections::hash_map::DefaultHasher; |
| 22 | + use std::hash::{Hash, Hasher}; |
| 23 | + let mut hasher = DefaultHasher::new(); |
| 24 | + data.hash(&mut hasher); |
| 25 | + black_box(hasher.finish()); |
| 26 | + }); |
| 27 | + }, |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + group.finish(); |
| 32 | +} |
| 33 | + |
| 34 | +/// Benchmark content store operations |
| 35 | +fn bench_content_store(c: &mut Criterion) { |
| 36 | + let mut group = c.benchmark_group("content_store"); |
| 37 | + |
| 38 | + // Store (write to hash-addressed path) |
| 39 | + for size in [1024, 4096, 65536] { |
| 40 | + group.bench_with_input( |
| 41 | + BenchmarkId::new("store", size), |
| 42 | + &size, |
| 43 | + |b, &size| { |
| 44 | + let dir = tempfile::tempdir().unwrap(); |
| 45 | + let data = vec![0xCDu8; size]; |
| 46 | + b.iter(|| { |
| 47 | + let hash = format!("{:016x}", black_box(size)); |
| 48 | + let path = dir.path().join(&hash); |
| 49 | + std::fs::write(&path, &data).unwrap(); |
| 50 | + black_box(path); |
| 51 | + }); |
| 52 | + }, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + // Retrieve (read from hash-addressed path) |
| 57 | + group.bench_function("retrieve_4k", |b| { |
| 58 | + let dir = tempfile::tempdir().unwrap(); |
| 59 | + let data = vec![0xEFu8; 4096]; |
| 60 | + let path = dir.path().join("test-content"); |
| 61 | + std::fs::write(&path, &data).unwrap(); |
| 62 | + |
| 63 | + b.iter(|| { |
| 64 | + let read = std::fs::read(&path).unwrap(); |
| 65 | + black_box(read.len()); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + // Deduplication check (hash comparison) |
| 70 | + group.bench_function("dedup_check", |b| { |
| 71 | + let mut store: HashMap<u64, bool> = HashMap::new(); |
| 72 | + for i in 0..1000 { |
| 73 | + store.insert(i, true); |
| 74 | + } |
| 75 | + b.iter(|| { |
| 76 | + black_box(store.contains_key(&500)); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + group.finish(); |
| 81 | +} |
| 82 | + |
| 83 | +/// Benchmark secure overwrite patterns (obliteration core) |
| 84 | +fn bench_obliteration(c: &mut Criterion) { |
| 85 | + let mut group = c.benchmark_group("obliteration"); |
| 86 | + |
| 87 | + for size in [1024, 4096, 65536] { |
| 88 | + group.bench_with_input( |
| 89 | + BenchmarkId::new("3_pass_overwrite", size), |
| 90 | + &size, |
| 91 | + |b, &size| { |
| 92 | + let dir = tempfile::tempdir().unwrap(); |
| 93 | + let path = dir.path().join("target"); |
| 94 | + let data = vec![0xABu8; size]; |
| 95 | + std::fs::write(&path, &data).unwrap(); |
| 96 | + |
| 97 | + b.iter(|| { |
| 98 | + let patterns: [u8; 3] = [0x00, 0xFF, 0xAA]; |
| 99 | + for pattern in &patterns { |
| 100 | + let overwrite = vec![*pattern; size]; |
| 101 | + std::fs::write(&path, &overwrite).unwrap(); |
| 102 | + } |
| 103 | + black_box(&path); |
| 104 | + }); |
| 105 | + }, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + group.finish(); |
| 110 | +} |
| 111 | + |
| 112 | +/// Benchmark transaction overhead |
| 113 | +fn bench_transactions(c: &mut Criterion) { |
| 114 | + let mut group = c.benchmark_group("transactions"); |
| 115 | + |
| 116 | + group.bench_function("begin_commit", |b| { |
| 117 | + b.iter(|| { |
| 118 | + let mut active = false; |
| 119 | + // Begin |
| 120 | + active = true; |
| 121 | + black_box(active); |
| 122 | + // Commit |
| 123 | + active = false; |
| 124 | + black_box(active); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + group.bench_function("operation_log_append", |b| { |
| 129 | + let mut log: Vec<String> = Vec::with_capacity(100); |
| 130 | + b.iter(|| { |
| 131 | + log.push(format!("op_{}", log.len())); |
| 132 | + black_box(log.len()); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + group.finish(); |
| 137 | +} |
| 138 | + |
| 139 | +/// Benchmark Argon2-style key derivation simulation |
| 140 | +fn bench_key_derivation(c: &mut Criterion) { |
| 141 | + let mut group = c.benchmark_group("key_derivation"); |
| 142 | + |
| 143 | + // Simulated memory-hard work (not real Argon2 — needs argon2 crate) |
| 144 | + group.bench_function("memory_hard_64k", |b| { |
| 145 | + b.iter(|| { |
| 146 | + let mut buf = vec![0u8; 65536]; // 64 KiB |
| 147 | + for i in 0..buf.len() { |
| 148 | + buf[i] = (i as u8).wrapping_mul(137); |
| 149 | + } |
| 150 | + black_box(buf[0]); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + group.finish(); |
| 155 | +} |
| 156 | + |
| 157 | +criterion_group!( |
| 158 | + benches, |
| 159 | + bench_hashing, |
| 160 | + bench_content_store, |
| 161 | + bench_obliteration, |
| 162 | + bench_transactions, |
| 163 | + bench_key_derivation |
| 164 | +); |
| 165 | +criterion_main!(benches); |
0 commit comments