Skip to content

Commit

Permalink
benches
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Nov 15, 2022
1 parent e69a7e1 commit cd1cfb0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
members = ["crates/*", "benches", "analyze"]
resolver = "2"

[profile.release]
[profile.bench]
lto = "fat"
codegen-units = 1
3 changes: 2 additions & 1 deletion benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ detect = [
"uuid-simd/detect",
"radix64/simd",
]
unstable = ["criterion/real_blackbox"]

[dependencies.base32-simd]
path = "../crates/base32-simd"
Expand Down Expand Up @@ -83,4 +84,4 @@ base32ct = "0.1.0"
data-encoding = "2.3.2"

[dev-dependencies]
criterion = { version = "0.4.0", features = ["real_blackbox"] }
criterion = "0.4.0"
39 changes: 21 additions & 18 deletions benches/quick.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::time::Instant;

#[inline]
fn time(f: impl FnOnce()) -> u128 {
let t0 = Instant::now();
f();
let t1 = Instant::now();
(t1 - t0).as_nanos()
}

#[inline(never)]
fn bench_base64(n: usize, src: &str) -> u128 {
let encoded = base64_simd::STANDARD.encode_type::<Box<[u8]>>(src.as_bytes());

Expand All @@ -19,6 +21,7 @@ fn bench_base64(n: usize, src: &str) -> u128 {
})
}

#[inline(never)]
fn bench_ascii(n: usize, data: &[u8]) -> u128 {
time(|| {
for _ in 0..n {
Expand All @@ -27,6 +30,7 @@ fn bench_ascii(n: usize, data: &[u8]) -> u128 {
})
}

#[inline(never)]
fn bench_utf32(n: usize, data: &[u32]) -> u128 {
time(|| {
for _ in 0..n {
Expand All @@ -35,26 +39,35 @@ fn bench_utf32(n: usize, data: &[u32]) -> u128 {
})
}

/// GiB/s
fn throughput(bytes: u128, ns: u128) -> f64 {
let time_sec = ns as f64 / 1e9;
let gib = u128::pow(1024, 3);
bytes as f64 / gib as f64 / time_sec
}

fn main() {
println!("simd-benches quick mode\n");

{
println!("base64-simd forgiving_decode_inplace");

{
let src = "helloworld".repeat(100_000);
let data = "helloworld".repeat(100_000);
let n = 100;
let time = bench_base64(n, &src);
let time = bench_base64(n, &data);
let time_per_op = (time / n as u128) as f64;
println!("long | n = {n:<8} time = {time_per_op:>8}ns");
let throughput = throughput(n as u128 * data.len() as u128, time);
println!("long | n = {n:<8} time = {time_per_op:>8}ns, throughout = {throughput:.6}GiB/s");
}

{
let src = "123";
let data = "123";
let n = 1_000_000;
let time = bench_base64(n, src);
let time = bench_base64(n, data);
let time_per_op = time / n as u128;
println!("short | n = {n:<8} time = {time_per_op:>8}ns");
let throughput = throughput(n as u128 * data.len() as u128, time);
println!("short | n = {n:<8} time = {time_per_op:>8}ns, throughout = {throughput:.6}GiB/s");
}
println!();
}
Expand All @@ -65,12 +78,7 @@ fn main() {
let n = 10000;
let time = bench_ascii(n, data.as_bytes());
let time_per_op = time / n as u128;
let throughput = {
let total = n as u128 * data.len() as u128;
let time_sec = time as f64 / 1e9;
let gib = u128::pow(1024, 3);
total as f64 / gib as f64 / time_sec
};
let throughput = throughput(n as u128 * data.len() as u128, time);
println!(
"long | n = {n:<8} time = {:>8}ns, throughout = {:.6}GiB/s",
time_per_op, throughput
Expand All @@ -84,12 +92,7 @@ fn main() {
let n = 1000;
let time = bench_utf32(n, &data);
let time_per_op = time / n as u128;
let throughput = {
let total = n as u128 * data.len() as u128 * 4;
let time_sec = time as f64 / 1e9;
let gib = u128::pow(1024, 3);
total as f64 / gib as f64 / time_sec
};
let throughput = throughput(n as u128 * data.len() as u128 * 4, time);
println!(
"long | n = {n:<8} time = {:>8}ns, throughput = {:.6}GiB/s",
time_per_op, throughput
Expand Down
16 changes: 8 additions & 8 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ doc pkg="vsimd":
cargo doc --no-deps --all-features
cargo doc --open -p {{pkg}}

x86-bench dispatch *ARGS:
bench dispatch *ARGS:
#!/bin/bash -ex
cd {{justfile_directory()}}
mkdir -p target/x86-bench
mkdir -p target/simd-benches
COMMIT_HASH=`git rev-parse --short HEAD`

case "{{dispatch}}" in
Expand All @@ -39,21 +39,21 @@ x86-bench dispatch *ARGS:
;;
esac

NAME=target/x86-bench/$COMMIT_HASH-{{dispatch}}
NAME=target/simd-benches/$COMMIT_HASH-{{dispatch}}

time cargo criterion -p simd-benches --history-id $COMMIT_HASH --message-format json --features "$FEATURES" {{ARGS}} > $NAME.jsonl
just analyze $COMMIT_HASH {{dispatch}} > $NAME.md
bat $NAME.md
bat --paging=never $NAME.md

bench-all:
just x86-bench static
just x86-bench dynamic
just x86-bench fallback
just bench static
just bench dynamic
just bench fallback

analyze commit dispatch:
#!/bin/bash -ex
cd {{justfile_directory()}}
cargo run -q -p analyze -- target/x86-bench/{{commit}}-{{dispatch}}.jsonl
cargo run -q -p analyze -- target/simd-benches/{{commit}}-{{dispatch}}.jsonl

js-bench:
#!/bin/bash -e
Expand Down

0 comments on commit cd1cfb0

Please sign in to comment.