use criterion::*;
use dircpy::CopyBuilder;
use env_logger;
use log::*;
use std::fs::File;
use unzip::Unzipper;
const SAMPLE_DATA: &str = "https://github.com/rust-lang/cargo/archive/master.zip";
const SOURCE: &str = "bench_data/source";
const DEST: &str = "bench_data/dest";
fn random_string() -> String {
format!("{:?}", std::time::Instant::now())
}
fn download_and_unpack(url: &str, name: &str) {
let archive = format!("{}.zip", name);
if !std::path::Path::new(&archive).is_file() {
info!("Downloading {:?}", url);
let mut resp = reqwest::blocking::get(url).unwrap();
let mut out = File::create(&archive).expect("failed to create file");
std::io::copy(&mut resp, &mut out).expect("failed to copy content");
} else {
info!("Did not download, archive already present");
}
info!("Unzipping...");
Unzipper::new(File::open(&archive).unwrap(), name)
.unzip()
.unwrap();
info!("Done. Ready.");
}
fn setup(_: &mut Criterion) {
std::env::set_var("RUST_LOG", "INFO");
let _ = env_logger::builder().try_init();
std::fs::create_dir_all(SOURCE).unwrap();
download_and_unpack(SAMPLE_DATA, SOURCE);
}
fn teardown(_: &mut Criterion) {
std::env::set_var("RUST_LOG", "INFO");
let _ = env_logger::builder().try_init();
info!("CLEANUP");
let _ = std::fs::remove_dir_all(DEST);
info!("DONE");
}
fn test_cp(c: &mut Criterion) {
std::env::set_var("RUST_LOG", "INFO");
let _ = env_logger::builder().try_init();
c.bench_function("cp -r", |b| {
b.iter(|| {
std::process::Command::new("cp")
.arg("-r")
.arg(SOURCE)
.arg(&format!("{}{}", DEST, random_string()))
.output()
.unwrap();
});
});
}
fn test_dircpy_single(c: &mut Criterion) {
c.bench_function("cpy single threaded", |b| {
b.iter(|| {
CopyBuilder::new(&SOURCE, &format!("{}{}", DEST, random_string()))
.overwrite(true)
.run()
.unwrap();
});
});
}
fn test_dircpy_parallel(c: &mut Criterion) {
#[cfg(feature = "jwalk")]
c.bench_function("cpy multi-threaded", |b| {
b.iter(|| {
CopyBuilder::new(&SOURCE, &format!("{}{}", DEST, random_string()))
.overwrite(true)
.run_par()
.unwrap();
});
});
}
fn test_lms(c: &mut Criterion) {
std::env::set_var("RUST_LOG", "INFO");
let _ = env_logger::builder().try_init();
c.bench_function("lms", |b| {
b.iter(|| {
std::process::Command::new("lms")
.arg("cp")
.arg(SOURCE)
.arg(&format!("{}{}", DEST, random_string()))
.output()
.unwrap();
});
});
}
criterion_group! {
name = benches;
config = Criterion::default()
.sample_size(10)
.warm_up_time(std::time::Duration::from_secs(4))
.measurement_time(std::time::Duration::from_secs(6))
;
targets = setup, test_dircpy_single, test_dircpy_parallel, test_cp, test_lms, teardown
}
criterion_main!(benches);