125 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			125 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
use ourdb::{OurDB, OurDBConfig, OurDBSetArgs};
 | 
						|
use std::time::Instant;
 | 
						|
 | 
						|
fn main() -> Result<(), ourdb::Error> {
 | 
						|
    // Parse command-line arguments
 | 
						|
    let args: Vec<String> = std::env::args().collect();
 | 
						|
 | 
						|
    // Default values
 | 
						|
    let mut incremental_mode = true;
 | 
						|
    let mut keysize: u8 = 4;
 | 
						|
    let mut num_operations = 10000;
 | 
						|
 | 
						|
    // 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("ourdb_benchmark");
 | 
						|
    std::fs::create_dir_all(&db_path)?;
 | 
						|
 | 
						|
    println!("Database path: {}", db_path.display());
 | 
						|
 | 
						|
    // Create a new database
 | 
						|
    let config = OurDBConfig {
 | 
						|
        path: db_path.clone(),
 | 
						|
        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 (100 bytes per record)
 | 
						|
    let test_data = vec![b'A'; 100];
 | 
						|
 | 
						|
    // Benchmark write operations
 | 
						|
    println!(
 | 
						|
        "Benchmarking {} write operations (incremental: {}, keysize: {})...",
 | 
						|
        num_operations, incremental_mode, keysize
 | 
						|
    );
 | 
						|
 | 
						|
    let start = Instant::now();
 | 
						|
 | 
						|
    let mut ids = Vec::with_capacity(num_operations);
 | 
						|
    for _ in 0..num_operations {
 | 
						|
        let id = if incremental_mode {
 | 
						|
            db.set(OurDBSetArgs {
 | 
						|
                id: None,
 | 
						|
                data: &test_data,
 | 
						|
            })?
 | 
						|
        } else {
 | 
						|
            // 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
 | 
						|
        };
 | 
						|
        ids.push(id);
 | 
						|
    }
 | 
						|
 | 
						|
    let write_duration = start.elapsed();
 | 
						|
    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);
 | 
						|
 | 
						|
    let start = Instant::now();
 | 
						|
 | 
						|
    for &id in &ids {
 | 
						|
        let _ = db.get(id)?;
 | 
						|
    }
 | 
						|
 | 
						|
    let read_duration = start.elapsed();
 | 
						|
    let reads_per_second = num_operations as f64 / read_duration.as_secs_f64();
 | 
						|
 | 
						|
    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!("Benchmarking {} update operations...", num_operations);
 | 
						|
 | 
						|
    let start = Instant::now();
 | 
						|
 | 
						|
    for &id in &ids {
 | 
						|
        db.set(OurDBSetArgs {
 | 
						|
            id: Some(id),
 | 
						|
            data: &test_data,
 | 
						|
        })?;
 | 
						|
    }
 | 
						|
 | 
						|
    let update_duration = start.elapsed();
 | 
						|
    let updates_per_second = num_operations as f64 / update_duration.as_secs_f64();
 | 
						|
 | 
						|
    println!(
 | 
						|
        "Update performance: {:.2} ops/sec ({:.2} ms/op)",
 | 
						|
        updates_per_second,
 | 
						|
        update_duration.as_secs_f64() * 1000.0 / num_operations as f64
 | 
						|
    );
 | 
						|
 | 
						|
    // Clean up
 | 
						|
    db.close()?;
 | 
						|
    std::fs::remove_dir_all(&db_path)?;
 | 
						|
 | 
						|
    Ok(())
 | 
						|
}
 |