This commit is contained in:
2025-04-20 08:00:59 +02:00
parent 0051754c65
commit e3ec26a6ef
22 changed files with 971 additions and 691 deletions

View File

@@ -14,61 +14,42 @@ fn criterion_benchmark(c: &mut Criterion) {
incremental_mode: true,
file_size: Some(10 * 1024 * 1024), // 10MB
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
reset: Some(true), // Reset the database for benchmarking
};
let mut db = OurDB::new(config).unwrap();
let test_data = vec![b'X'; 100]; // 100 bytes of data
let mut i = 0;
b.iter(|| {
let args = OurDBSetArgs {
id: None, // Let the DB assign an ID
let _ = db.set(OurDBSetArgs {
id: None,
data: &test_data,
};
black_box(db.set(args).unwrap());
i += 1;
}).unwrap();
});
db.close().unwrap();
});
// Setup database with data for other benchmarks
let setup_config = OurDBConfig {
path: db_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024), // 10MB
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
};
let mut setup_db = OurDB::new(setup_config).unwrap();
let test_data = vec![b'X'; 100]; // 100 bytes of data
let mut ids = Vec::with_capacity(1000);
// Insert 1000 records
for _ in 0..1000 {
let args = OurDBSetArgs {
id: None,
data: &test_data,
};
let id = setup_db.set(args).unwrap();
ids.push(id);
}
// Benchmark get operation
// Benchmark get operation (retrieval)
c.bench_function("get", |b| {
let config = OurDBConfig {
// Setup: Create a database and insert a record
let setup_config = OurDBConfig {
path: db_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
keysize: Some(6),
reset: Some(true), // Reset the database for benchmarking
};
let mut db = OurDB::new(config).unwrap();
let mut i = 0;
let mut db = OurDB::new(setup_config).unwrap();
let test_data = vec![b'X'; 100];
let id = db.set(OurDBSetArgs { id: None, data: &test_data }).unwrap();
b.iter(|| {
let id = ids[i % ids.len()];
black_box(db.get(id).unwrap());
i += 1;
let _ = db.get(id).unwrap();
});
db.close().unwrap();
});
// Benchmark update operation
@@ -77,199 +58,143 @@ fn criterion_benchmark(c: &mut Criterion) {
path: db_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
keysize: Some(6),
reset: Some(true), // Reset the database for benchmarking
};
let mut db = OurDB::new(config).unwrap();
let updated_data = vec![b'Y'; 100]; // Different data for updates
let mut i = 0;
let test_data = vec![b'X'; 100];
let id = db.set(OurDBSetArgs { id: None, data: &test_data }).unwrap();
b.iter(|| {
let id = ids[i % ids.len()];
let args = OurDBSetArgs {
let _ = db.set(OurDBSetArgs {
id: Some(id),
data: &updated_data,
};
black_box(db.set(args).unwrap());
i += 1;
data: &test_data,
}).unwrap();
});
db.close().unwrap();
});
// Benchmark get_history operation
// Benchmark delete operation
c.bench_function("delete", |b| {
let config = OurDBConfig {
path: db_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6),
reset: Some(true), // Reset the database for benchmarking
};
let mut db = OurDB::new(config).unwrap();
// Create a test data vector outside the closure
let test_data = vec![b'X'; 100];
b.iter_with_setup(
// Setup: Insert a record before each iteration
|| {
db.set(OurDBSetArgs { id: None, data: &test_data }).unwrap()
},
// Benchmark: Delete the record
|id| {
db.delete(id).unwrap();
}
);
db.close().unwrap();
});
// Benchmark history tracking
c.bench_function("get_history", |b| {
let config = OurDBConfig {
path: db_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
};
let mut db = OurDB::new(config).unwrap();
let mut i = 0;
b.iter(|| {
let id = ids[i % ids.len()];
black_box(db.get_history(id, 2).unwrap());
i += 1;
});
});
// Benchmark delete operation
c.bench_function("delete", |b| {
// Create a fresh database for deletion benchmarks
let delete_dir = tempdir().expect("Failed to create temp directory");
let delete_path = delete_dir.path().to_path_buf();
let config = OurDBConfig {
path: delete_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
keysize: Some(6),
reset: Some(true), // Reset the database for benchmarking
};
let mut db = OurDB::new(config).unwrap();
let test_data = vec![b'X'; 100];
// Setup keys to delete
let mut delete_ids = Vec::with_capacity(1000);
for _ in 0..1000 {
let args = OurDBSetArgs {
id: None,
data: &test_data,
};
let id = db.set(args).unwrap();
delete_ids.push(id);
// Create a record with history
let id = db.set(OurDBSetArgs { id: None, data: &test_data }).unwrap();
// Update it a few times to create history
for _ in 0..5 {
db.set(OurDBSetArgs { id: Some(id), data: &test_data }).unwrap();
}
let mut i = 0;
b.iter(|| {
let id = delete_ids[i % delete_ids.len()];
// Only try to delete if it exists (not already deleted)
if db.get(id).is_ok() {
black_box(db.delete(id).unwrap());
}
i += 1;
let _ = db.get_history(id, 3).unwrap();
});
db.close().unwrap();
});
// Benchmark key-value mode vs incremental mode
let mut group = c.benchmark_group("mode_comparison");
// Benchmark set in key-value mode
group.bench_function("set_keyvalue_mode", |b| {
let kv_dir = tempdir().expect("Failed to create temp directory");
let kv_path = kv_dir.path().to_path_buf();
// Benchmark large data handling
c.bench_function("large_data", |b| {
let config = OurDBConfig {
path: kv_path.clone(),
incremental_mode: false, // Key-value mode
path: db_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
keysize: Some(6),
reset: Some(true), // Reset the database for benchmarking
};
let mut db = OurDB::new(config).unwrap();
let test_data = vec![b'X'; 100];
let mut i = 0;
let large_data = vec![b'X'; 10 * 1024]; // 10KB
b.iter(|| {
let id = i + 1; // Explicit ID
let args = OurDBSetArgs {
id: Some(id as u32),
data: &test_data,
};
black_box(db.set(args).unwrap());
i += 1;
let id = db.set(OurDBSetArgs { id: None, data: &large_data }).unwrap();
let _ = db.get(id).unwrap();
db.delete(id).unwrap();
});
db.close().unwrap();
});
// Benchmark set in incremental mode
group.bench_function("set_incremental_mode", |b| {
let inc_dir = tempdir().expect("Failed to create temp directory");
let inc_path = inc_dir.path().to_path_buf();
// Benchmark concurrent operations (simulated)
c.bench_function("concurrent_ops", |b| {
let config = OurDBConfig {
path: inc_path.clone(),
incremental_mode: true, // Incremental mode
path: db_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
keysize: Some(6),
reset: Some(true), // Reset the database for benchmarking
};
let mut db = OurDB::new(config).unwrap();
let test_data = vec![b'X'; 100];
// Pre-insert some data
let mut ids = Vec::with_capacity(100);
for _ in 0..100 {
let id = db.set(OurDBSetArgs { id: None, data: &test_data }).unwrap();
ids.push(id);
}
b.iter(|| {
let args = OurDBSetArgs {
id: None, // Auto-generated ID
data: &test_data,
};
black_box(db.set(args).unwrap());
});
});
group.finish();
// Benchmark with different record sizes
let mut size_group = c.benchmark_group("record_size");
for &size in &[10, 100, 1000, 10000] {
size_group.bench_function(format!("set_size_{}", size), |b| {
let size_dir = tempdir().expect("Failed to create temp directory");
let size_path = size_dir.path().to_path_buf();
let config = OurDBConfig {
path: size_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
};
let mut db = OurDB::new(config).unwrap();
let test_data = vec![b'X'; size];
b.iter(|| {
let args = OurDBSetArgs {
id: None,
data: &test_data,
};
black_box(db.set(args).unwrap());
});
// Simulate mixed workload
for i in 0..10 {
if i % 3 == 0 {
// Insert
let _ = db.set(OurDBSetArgs { id: None, data: &test_data }).unwrap();
} else if i % 3 == 1 {
// Read
let idx = i % ids.len();
let _ = db.get(ids[idx]).unwrap();
} else {
// Update
let idx = i % ids.len();
db.set(OurDBSetArgs { id: Some(ids[idx]), data: &test_data }).unwrap();
}
}
});
size_group.bench_function(format!("get_size_{}", size), |b| {
let size_dir = tempdir().expect("Failed to create temp directory");
let size_path = size_dir.path().to_path_buf();
let config = OurDBConfig {
path: size_path.clone(),
incremental_mode: true,
file_size: Some(10 * 1024 * 1024),
keysize: Some(6), // Use keysize=6 to allow non-zero file_nr
};
let mut db = OurDB::new(config).unwrap();
let test_data = vec![b'X'; size];
// Insert some records first
let mut size_ids = Vec::with_capacity(100);
for _ in 0..100 {
let args = OurDBSetArgs {
id: None,
data: &test_data,
};
let id = db.set(args).unwrap();
size_ids.push(id);
}
let mut i = 0;
b.iter(|| {
let id = size_ids[i % size_ids.len()];
black_box(db.get(id).unwrap());
i += 1;
});
});
}
size_group.finish();
db.close().unwrap();
});
}
criterion_group!(benches, criterion_benchmark);