mod config;
mod error;
mod filesystem;
mod report;
mod runner;
mod workload;
use crate::{
config::{Backend, Config, OutputFormat},
error::Result,
filesystem::{cleanup_root, prepare_root},
report::Report,
workload::run_benchmark,
};
use commonware_allocator::mimalloc;
#[cfg(target_os = "linux")]
use commonware_runtime::iouring;
use commonware_runtime::{Runner as _, tokio};
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc::new(mimalloc::Config::DEFAULT);
fn main() -> Result<()> {
let mut cfg = Config::parse();
cfg.root = prepare_root(&cfg.root)?;
let report = match cfg.backend {
Backend::Tokio => run_tokio(&cfg),
#[cfg(target_os = "linux")]
Backend::Iouring => run_iouring(&cfg),
#[cfg(not(target_os = "linux"))]
Backend::Iouring => unreachable!("validation rejects io_uring off Linux"),
};
cleanup_root(&cfg.root)?;
match cfg.output {
OutputFormat::Human => report?.print_human(&cfg),
OutputFormat::Json => report?.print_json(&cfg),
}
Ok(())
}
fn run_tokio(cfg: &Config) -> Result<Report> {
let mut runtime_cfg = tokio::Config::default()
.with_worker_threads(cfg.worker_threads())
.with_storage_directory(cfg.root.clone());
if let Some(global_queue_interval) = cfg.global_queue_interval {
runtime_cfg = runtime_cfg.with_global_queue_interval(global_queue_interval);
}
tokio::Runner::new(runtime_cfg).start(|context| run_benchmark(cfg, context))
}
#[cfg(target_os = "linux")]
fn run_iouring(cfg: &Config) -> Result<Report> {
let mut runtime_cfg = iouring::Config::default()
.with_worker_threads(cfg.worker_threads())
.with_storage_directory(cfg.root.clone())
.with_ring_config(iouring::RingConfig {
size: cfg.effective_ring_size().unwrap(),
..Default::default()
});
if let Some(global_queue_interval) = cfg.global_queue_interval {
runtime_cfg = runtime_cfg.with_global_queue_interval(global_queue_interval);
}
iouring::Runner::new(runtime_cfg).start(|context| run_benchmark(cfg, context))
}