Skip to content

Commit ac03751

Browse files
committed
fix(bench): add some basic criterion benchmarks
criterion dep is disabled by default since there is no way to add separate deps for tests and benchmarks
1 parent 07bb166 commit ac03751

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

miniz_oxide/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core
2626
alloc = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-alloc' }
2727
compiler_builtins = { version = '0.1.2', optional = true }
2828

29+
[dev-dependencies]
30+
## Messes with minimum rust version and drags in deps just for running tests
31+
## so just comment out for now and enable manually when needed for enabling benches
32+
# criterion = "0.5"
33+
34+
[[bench]]
35+
name = "benchmark"
36+
harness = false
37+
2938
[features]
3039
default = ["with-alloc"]
3140
with-alloc = []

miniz_oxide/benches/benchmark.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extern crate criterion;
2+
3+
use std::hint::black_box;
4+
use std::io::Read;
5+
6+
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
7+
use miniz_oxide::deflate::{compress_to_vec, compress_to_vec_zlib};
8+
use miniz_oxide::inflate::{decompress_to_vec, decompress_to_vec_zlib, TINFLStatus};
9+
10+
fn get_test_file_data(name: &str) -> Vec<u8> {
11+
use std::fs::File;
12+
let mut input = Vec::new();
13+
let mut f = File::open(name).unwrap();
14+
15+
f.read_to_end(&mut input).unwrap();
16+
input
17+
}
18+
19+
fn get_test_data() -> Vec<u8> {
20+
use std::env;
21+
let path = env::var("TEST_FILE").unwrap_or_else(|_| "../miniz/miniz.c".to_string());
22+
get_test_file_data(&path)
23+
}
24+
25+
fn bench_inflate(c: &mut Criterion) {
26+
let data = get_test_data();
27+
let compressed= compress_to_vec(&data, 6);
28+
c.bench_function("inflate", |b| b.iter(|| decompress_to_vec(black_box(&compressed))));
29+
let compressed_zlib = compress_to_vec_zlib(&data, 6);
30+
c.bench_function("inflate_zlib", |b| b.iter(|| decompress_to_vec_zlib(black_box(&compressed_zlib))));
31+
}
32+
33+
fn bench_deflate(c: &mut Criterion) {
34+
let data = get_test_data();
35+
c.bench_function("deflate_l6", |b| b.iter(|| compress_to_vec(black_box(&data), 6)));
36+
c.bench_function("deflate_zlib_l6", |b| b.iter(|| compress_to_vec_zlib(black_box(&data), 6)));
37+
}
38+
39+
criterion_group!(benches, bench_inflate, bench_deflate);
40+
criterion_main!(benches);

0 commit comments

Comments
 (0)