Skip to content

Commit

Permalink
chore: avoid writing to tmpfs
Browse files Browse the repository at this point in the history
  • Loading branch information
arriqaaq committed Nov 12, 2024
1 parent 1241a8f commit f4673db
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions benches/load_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use bytes::Bytes;
use rand::{thread_rng, Rng};
use std::time::Instant;
use surrealkv::{Options, Store};
use tempdir::TempDir;
use std::fs;
use std::path::PathBuf;

#[cfg_attr(any(target_os = "linux", target_os = "macos"), global_allocator)]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;

fn create_temp_directory() -> TempDir {
TempDir::new("test").unwrap()
fn create_local_directory(test_name: &str) -> PathBuf {
let local_dir = PathBuf::from(format!("./test_data/{}", test_name));
fs::create_dir_all(&local_dir).unwrap();
local_dir
}

async fn benchmark_load_times_kv_size() {
Expand All @@ -24,9 +27,11 @@ async fn benchmark_load_times_kv_size() {
for value_size in &value_sizes {
// Test Sequential Keys
{
let test_name = format!("sequential_keys_{}_{}", key_size, value_size);
let local_dir = create_local_directory(&test_name);

let mut opts = Options::new();
let temp_dir = create_temp_directory();
opts.dir = temp_dir.path().to_path_buf();
opts.dir = local_dir.clone();
let store = Store::new(opts.clone()).expect("should create store");

let default_value = Bytes::from(vec![0x42; *value_size]);
Expand All @@ -43,7 +48,7 @@ async fn benchmark_load_times_kv_size() {
store.close().await.unwrap();

// Calculate store size
let store_size = calculate_store_size(&temp_dir);
let store_size = calculate_store_size(&local_dir);

// Measure load time
let start = Instant::now();
Expand All @@ -58,13 +63,18 @@ async fn benchmark_load_times_kv_size() {
duration.as_secs_f64(),
store_size as f64 / (1024.0 * 1024.0 * 1024.0) // Convert to GB
);

// Remove the local directory
fs::remove_dir_all(local_dir).expect("should remove local directory");
}

// Test Random Keys
{
let test_name = format!("random_keys_{}_{}", key_size, value_size);
let local_dir = create_local_directory(&test_name);

let mut opts = Options::new();
let temp_dir = create_temp_directory();
opts.dir = temp_dir.path().to_path_buf();
opts.dir = local_dir.clone();
let store = Store::new(opts.clone()).expect("should create store");

let default_value = Bytes::from(vec![0x42; *value_size]);
Expand All @@ -85,7 +95,7 @@ async fn benchmark_load_times_kv_size() {
store.close().await.unwrap();

// Calculate store size
let store_size = calculate_store_size(&temp_dir);
let store_size = calculate_store_size(&local_dir);

// Measure load time
let start = Instant::now();
Expand All @@ -100,6 +110,9 @@ async fn benchmark_load_times_kv_size() {
duration.as_secs_f64(),
store_size as f64 / (1024.0 * 1024.0 * 1024.0) // Convert to GB
);

// Remove the local directory
fs::remove_dir_all(local_dir).expect("should remove local directory");
}
}
}
Expand All @@ -118,9 +131,11 @@ async fn benchmark_load_times_versions() {
for version_count in versions {
let num_keys = total_records / version_count;

let test_name = format!("versions_{}", version_count);
let local_dir = create_local_directory(&test_name);

let mut opts = Options::new();
let temp_dir = create_temp_directory();
opts.dir = temp_dir.path().to_path_buf();
opts.dir = local_dir.clone();
let store = Store::new(opts.clone()).expect("should create store");

let default_value = Bytes::from(vec![0x42; value_size]);
Expand All @@ -143,7 +158,7 @@ async fn benchmark_load_times_versions() {
store.close().await.unwrap();

// Calculate store size
let store_size = calculate_store_size(&temp_dir);
let store_size = calculate_store_size(&local_dir);

let start = Instant::now();
let _ = Store::new(opts).expect("should create store");
Expand All @@ -156,13 +171,16 @@ async fn benchmark_load_times_versions() {
duration.as_secs_f64(),
store_size as f64 / (1024.0 * 1024.0 * 1024.0) // Convert to GB
);

// Remove the local directory
fs::remove_dir_all(local_dir).expect("should remove local directory");
}
}

// Helper function to calculate the size of the store directory
fn calculate_store_size(temp_dir: &TempDir) -> u64 {
fn calculate_store_size(local_dir: &PathBuf) -> u64 {
let mut total_size = 0;
for entry in walkdir::WalkDir::new(temp_dir.path()).into_iter().flatten() {
for entry in walkdir::WalkDir::new(local_dir).into_iter().flatten() {
if let Ok(metadata) = entry.metadata() {
if metadata.is_file() {
total_size += metadata.len();
Expand All @@ -180,4 +198,4 @@ async fn main() {
benchmark_load_times_versions().await;

println!("\nBenchmarks complete!");
}
}

0 comments on commit f4673db

Please sign in to comment.