...
This commit is contained in:
@@ -41,6 +41,7 @@ fn key_value_mode_example(base_path: &PathBuf) -> Result<(), ourdb::Error> {
|
||||
incremental_mode: false,
|
||||
file_size: Some(1024 * 1024), // 1MB for testing
|
||||
keysize: Some(2), // Small key size for demonstration
|
||||
reset: None, // Don't reset existing database
|
||||
};
|
||||
|
||||
let mut db = OurDB::new(config)?;
|
||||
@@ -94,6 +95,7 @@ fn incremental_mode_example(base_path: &PathBuf) -> Result<(), ourdb::Error> {
|
||||
incremental_mode: true,
|
||||
file_size: Some(1024 * 1024), // 1MB for testing
|
||||
keysize: Some(3), // 3-byte keys
|
||||
reset: None, // Don't reset existing database
|
||||
};
|
||||
|
||||
let mut db = OurDB::new(config)?;
|
||||
@@ -136,7 +138,8 @@ fn performance_benchmark(base_path: &PathBuf) -> Result<(), ourdb::Error> {
|
||||
path: db_path,
|
||||
incremental_mode: true,
|
||||
file_size: Some(1024 * 1024), // 10MB
|
||||
keysize: Some(4), // 4-byte keys
|
||||
keysize: Some(4), // 4-byte keys
|
||||
reset: None, // Don't reset existing database
|
||||
};
|
||||
|
||||
let mut db = OurDB::new(config)?;
|
||||
|
@@ -13,6 +13,7 @@ fn main() -> Result<(), ourdb::Error> {
|
||||
incremental_mode: true,
|
||||
file_size: None, // Use default (500MB)
|
||||
keysize: None, // Use default (4 bytes)
|
||||
reset: None, // Don't reset existing database
|
||||
};
|
||||
|
||||
let mut db = OurDB::new(config)?;
|
||||
|
@@ -1,22 +1,28 @@
|
||||
use ourdb::{OurDB, OurDBConfig, OurDBSetArgs};
|
||||
use std::time::{Duration, Instant};
|
||||
use std::time::Instant;
|
||||
|
||||
fn main() -> Result<(), ourdb::Error> {
|
||||
// Parse command line arguments
|
||||
// Parse command-line arguments
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
||||
let (num_operations, record_size, incremental_mode, keysize) = parse_args(&args);
|
||||
// Default values
|
||||
let mut incremental_mode = true;
|
||||
let mut keysize: u8 = 4;
|
||||
let mut num_operations = 10000;
|
||||
|
||||
println!("OurDB Benchmark");
|
||||
println!("===============");
|
||||
println!("Operations: {}", num_operations);
|
||||
println!("Record size: {} bytes", record_size);
|
||||
println!("Mode: {}", if incremental_mode { "Incremental" } else { "Key-Value" });
|
||||
println!("Key size: {} bytes", keysize);
|
||||
println!();
|
||||
// Parse arguments
|
||||
for i in 1..args.len() {
|
||||
if args[i] == "--no-incremental" {
|
||||
incremental_mode = false;
|
||||
} else if args[i] == "--keysize" && i + 1 < args.len() {
|
||||
keysize = args[i + 1].parse().unwrap_or(4);
|
||||
} else if args[i] == "--ops" && i + 1 < args.len() {
|
||||
num_operations = args[i + 1].parse().unwrap_or(10000);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a temporary directory for the database
|
||||
let db_path = std::env::temp_dir().join(format!("ourdb_benchmark_{}", std::process::id()));
|
||||
let db_path = std::env::temp_dir().join("ourdb_benchmark");
|
||||
std::fs::create_dir_all(&db_path)?;
|
||||
|
||||
println!("Database path: {}", db_path.display());
|
||||
@@ -27,24 +33,27 @@ fn main() -> Result<(), ourdb::Error> {
|
||||
incremental_mode,
|
||||
file_size: Some(1024 * 1024),
|
||||
keysize: Some(keysize),
|
||||
reset: Some(true), // Reset the database for benchmarking
|
||||
};
|
||||
|
||||
let mut db = OurDB::new(config)?;
|
||||
|
||||
// Prepare test data
|
||||
let test_data = vec![b'X'; record_size];
|
||||
// Prepare test data (100 bytes per record)
|
||||
let test_data = vec![b'A'; 100];
|
||||
|
||||
// Benchmark write operations
|
||||
println!("\nBenchmarking writes...");
|
||||
println!("Benchmarking {} write operations (incremental: {}, keysize: {})...",
|
||||
num_operations, incremental_mode, keysize);
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let mut ids = Vec::with_capacity(num_operations);
|
||||
for i in 0..num_operations {
|
||||
for _ in 0..num_operations {
|
||||
let id = if incremental_mode {
|
||||
db.set(OurDBSetArgs { id: None, data: &test_data })?
|
||||
} else {
|
||||
// In key-value mode, we provide explicit IDs
|
||||
let id = i as u32 + 1;
|
||||
// In non-incremental mode, we need to provide IDs
|
||||
let id = ids.len() as u32 + 1;
|
||||
db.set(OurDBSetArgs { id: Some(id), data: &test_data })?;
|
||||
id
|
||||
};
|
||||
@@ -52,10 +61,15 @@ fn main() -> Result<(), ourdb::Error> {
|
||||
}
|
||||
|
||||
let write_duration = start.elapsed();
|
||||
print_performance_stats("Write", num_operations, write_duration);
|
||||
let writes_per_second = num_operations as f64 / write_duration.as_secs_f64();
|
||||
|
||||
println!("Write performance: {:.2} ops/sec ({:.2} ms/op)",
|
||||
writes_per_second,
|
||||
write_duration.as_secs_f64() * 1000.0 / num_operations as f64);
|
||||
|
||||
// Benchmark read operations
|
||||
println!("Benchmarking {} read operations...", num_operations);
|
||||
|
||||
// Benchmark read operations (sequential)
|
||||
println!("\nBenchmarking sequential reads...");
|
||||
let start = Instant::now();
|
||||
|
||||
for &id in &ids {
|
||||
@@ -63,123 +77,31 @@ fn main() -> Result<(), ourdb::Error> {
|
||||
}
|
||||
|
||||
let read_duration = start.elapsed();
|
||||
print_performance_stats("Sequential read", num_operations, read_duration);
|
||||
let reads_per_second = num_operations as f64 / read_duration.as_secs_f64();
|
||||
|
||||
// Benchmark random reads
|
||||
println!("\nBenchmarking random reads...");
|
||||
let start = Instant::now();
|
||||
|
||||
use std::collections::HashSet;
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut random_indices = HashSet::new();
|
||||
|
||||
// Select 20% of the IDs randomly for testing
|
||||
let sample_size = num_operations / 5;
|
||||
while random_indices.len() < sample_size {
|
||||
let idx = rand::Rng::gen_range(&mut rng, 0..ids.len());
|
||||
random_indices.insert(idx);
|
||||
}
|
||||
|
||||
for idx in random_indices {
|
||||
let _ = db.get(ids[idx])?;
|
||||
}
|
||||
|
||||
let random_read_duration = start.elapsed();
|
||||
print_performance_stats("Random read", sample_size, random_read_duration);
|
||||
println!("Read performance: {:.2} ops/sec ({:.2} ms/op)",
|
||||
reads_per_second,
|
||||
read_duration.as_secs_f64() * 1000.0 / num_operations as f64);
|
||||
|
||||
// Benchmark update operations
|
||||
println!("\nBenchmarking updates...");
|
||||
println!("Benchmarking {} update operations...", num_operations);
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
for &id in &ids[0..num_operations/2] {
|
||||
for &id in &ids {
|
||||
db.set(OurDBSetArgs { id: Some(id), data: &test_data })?;
|
||||
}
|
||||
|
||||
let update_duration = start.elapsed();
|
||||
print_performance_stats("Update", num_operations/2, update_duration);
|
||||
let updates_per_second = num_operations as f64 / update_duration.as_secs_f64();
|
||||
|
||||
// Benchmark history retrieval
|
||||
println!("\nBenchmarking history retrieval...");
|
||||
let start = Instant::now();
|
||||
println!("Update performance: {:.2} ops/sec ({:.2} ms/op)",
|
||||
updates_per_second,
|
||||
update_duration.as_secs_f64() * 1000.0 / num_operations as f64);
|
||||
|
||||
for &id in &ids[0..num_operations/10] {
|
||||
let _ = db.get_history(id, 2)?;
|
||||
}
|
||||
|
||||
let history_duration = start.elapsed();
|
||||
print_performance_stats("History retrieval", num_operations/10, history_duration);
|
||||
|
||||
// Benchmark delete operations
|
||||
println!("\nBenchmarking deletes...");
|
||||
let start = Instant::now();
|
||||
|
||||
for &id in &ids[0..num_operations/4] {
|
||||
db.delete(id)?;
|
||||
}
|
||||
|
||||
let delete_duration = start.elapsed();
|
||||
print_performance_stats("Delete", num_operations/4, delete_duration);
|
||||
|
||||
// Close and clean up
|
||||
// Clean up
|
||||
db.close()?;
|
||||
std::fs::remove_dir_all(&db_path)?;
|
||||
|
||||
println!("\nBenchmark completed successfully");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_args(args: &[String]) -> (usize, usize, bool, u8) {
|
||||
let mut num_operations = 100000;
|
||||
let mut record_size = 100;
|
||||
let mut incremental_mode = true;
|
||||
let mut keysize = 4;
|
||||
|
||||
for i in 1..args.len() {
|
||||
if args[i] == "--ops" && i + 1 < args.len() {
|
||||
if let Ok(n) = args[i + 1].parse() {
|
||||
num_operations = n;
|
||||
}
|
||||
} else if args[i] == "--size" && i + 1 < args.len() {
|
||||
if let Ok(n) = args[i + 1].parse() {
|
||||
record_size = n;
|
||||
}
|
||||
} else if args[i] == "--keyvalue" {
|
||||
incremental_mode = false;
|
||||
} else if args[i] == "--keysize" && i + 1 < args.len() {
|
||||
if let Ok(n) = args[i + 1].parse() {
|
||||
if [2, 3, 4, 6].contains(&n) {
|
||||
keysize = n;
|
||||
}
|
||||
}
|
||||
} else if args[i] == "--help" {
|
||||
print_usage();
|
||||
std::process::exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
(num_operations, record_size, incremental_mode, keysize)
|
||||
}
|
||||
|
||||
fn print_usage() {
|
||||
println!("OurDB Benchmark Tool");
|
||||
println!("Usage: cargo run --example benchmark [OPTIONS]");
|
||||
println!();
|
||||
println!("Options:");
|
||||
println!(" --ops N Number of operations to perform (default: 100000)");
|
||||
println!(" --size N Size of each record in bytes (default: 100)");
|
||||
println!(" --keyvalue Use key-value mode instead of incremental mode");
|
||||
println!(" --keysize N Key size in bytes (2, 3, 4, or 6) (default: 4)");
|
||||
println!(" --help Print this help message");
|
||||
}
|
||||
|
||||
fn print_performance_stats(operation: &str, count: usize, duration: Duration) {
|
||||
let ops_per_second = count as f64 / duration.as_secs_f64();
|
||||
let ms_per_op = duration.as_secs_f64() * 1000.0 / count as f64;
|
||||
|
||||
println!("{} performance:", operation);
|
||||
println!(" Total time: {:.2} seconds", duration.as_secs_f64());
|
||||
println!(" Operations: {}", count);
|
||||
println!(" Speed: {:.2} ops/sec", ops_per_second);
|
||||
println!(" Average: {:.3} ms/op", ms_per_op);
|
||||
}
|
||||
|
Reference in New Issue
Block a user