Skip to content

Commit

Permalink
Add Criterion benchmark
Browse files Browse the repository at this point in the history
Add `just bench` recipe
  • Loading branch information
alpha-tango-kilo committed Nov 12, 2023
1 parent 67fbfd1 commit 1d537eb
Show file tree
Hide file tree
Showing 2,334 changed files with 114,155 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ tokio = { version = "1.23.1", features = ["rt", "fs"], optional = true }
threadpool = { version = "1.7", optional = true }

[dev-dependencies]
criterion = "0.5"
tempfile = "3"
# Have to include this so we can get Async{Read,Write}Ext through feature unification
tokio = { version = "1.23.1", features = ["io-util"] }

[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"

[[bench]]
name = "criterion"
harness = false
14 changes: 14 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ alias check := clippy
alias t := test
alias fmt := format
alias d := doc
alias b := bench

@_default:
echo "Using this Justfile for clippy/test requires cargo-hack & the"
Expand All @@ -17,6 +18,7 @@ alias d := doc
clippy:
cargo hack \
--each-feature \
--skip default \
--exclude-no-default-features \
--exclude-all-features \
clippy \
Expand All @@ -27,11 +29,23 @@ clippy:
test:
cargo hack \
--each-feature \
--skip default \
--exclude-no-default-features \
--exclude-all-features \
test \
--target x86_64-pc-windows-msvc

bench:
# Skip std perf & async runtimes
cargo hack \
--each-feature \
--skip default,backend-async-std,backend-smol,backend-tokio \
--exclude-no-default-features \
--exclude-all-features \
bench \
-- \
close_already

# Run nightly rustfmt
format:
cargo +nightly fmt
Expand Down
108 changes: 108 additions & 0 deletions benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use std::{fs, time::Duration};

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use tempfile::tempdir;

#[cfg(feature = "backend-threadpool")]
const BACKEND: &str = "threadpool";
#[cfg(feature = "backend-blocking")]
const BACKEND: &str = "blocking";
#[cfg(feature = "backend-rayon")]
const BACKEND: &str = "rayon";
#[cfg(feature = "backend-async-std")]
const BACKEND: &str = "async-std";
#[cfg(feature = "backend-smol")]
const BACKEND: &str = "smol";
#[cfg(feature = "backend-tokio")]
const BACKEND: &str = "tokio";

fn reading_ufos(c: &mut Criterion) {
let files = fs::read_dir("benches/data/Roboto-Regular.ufo/glyphs")
.unwrap()
.filter_map(|de| {
let de = de.unwrap();
let file_name = de.file_name();
let file_name = file_name.to_str().unwrap();
file_name.ends_with(".glif").then(|| de.path())
})
.collect::<Vec<_>>();

let mut group = c.benchmark_group("Reading");
group
.sample_size(100)
.measurement_time(Duration::from_secs(10))
.bench_with_input(
BenchmarkId::new(
format!("close_already {BACKEND}"),
"Roboto-Regular.ufo",
),
&files,
|b, files| {
b.iter(|| {
for file in files {
close_already::fs::read(file).unwrap();
}
});
},
);
group.bench_with_input(
BenchmarkId::new("std::fs", "Roboto-Regular.ufo"),
&files,
|b, files| {
b.iter(|| {
for file in files {
fs::read(file).unwrap();
}
});
},
);
}

fn writing_ufos(c: &mut Criterion) {
let temp_dir = tempdir().unwrap();
let files = fs::read_dir("benches/data/Roboto-Regular.ufo/glyphs")
.unwrap()
.filter_map(|de| {
let de = de.unwrap();
let file_name = de.file_name();
let file_name = file_name.to_str().unwrap();
file_name.ends_with(".glif").then(|| {
let bytes = fs::read(de.path()).unwrap();
(temp_dir.path().join(file_name), bytes)
})
})
.collect::<Vec<_>>();

let mut group = c.benchmark_group("Writing");
group
.sample_size(20)
.measurement_time(Duration::from_secs(50))
.bench_with_input(
BenchmarkId::new(
format!("close_already {BACKEND}"),
"Roboto-Regular.ufo",
),
&files,
|b, files| {
b.iter(|| {
for (path, bytes) in files {
close_already::fs::write(path, bytes).unwrap();
}
});
},
);
group.bench_with_input(
BenchmarkId::new("std::fs", "Roboto-Regular.ufo"),
&files,
|b, files| {
b.iter(|| {
for (path, bytes) in files {
fs::write(path, bytes).unwrap();
}
});
},
);
}

criterion_group!(criterion, reading_ufos, writing_ufos);
criterion_main!(criterion);
Loading

0 comments on commit 1d537eb

Please sign in to comment.