Skip to content

Commit

Permalink
Add a benchmark covering Reader.next_frame_info.
Browse files Browse the repository at this point in the history
  • Loading branch information
anforowicz committed Nov 1, 2024
1 parent 6016c9b commit 56ff289
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ name = "decoder"
harness = false

[[bench]]
path = "benches/unfilter.rs"
name = "unfilter"
path = "benches/expand_paletted.rs"
name = "expand_paletted"
harness = false
required-features = ["benchmarks"]

[[bench]]
path = "benches/expand_paletted.rs"
name = "expand_paletted"
path = "benches/next_frame_info.rs"
name = "next_frame_info"
harness = false

[[bench]]
path = "benches/unfilter.rs"
name = "unfilter"
harness = false
required-features = ["benchmarks"]
43 changes: 43 additions & 0 deletions benches/next_frame_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::fs;
use std::path::Path;

use criterion::{
criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion,
};
use png::Decoder;

criterion_group! {benches, load_all}
criterion_main!(benches);

fn load_all(c: &mut Criterion) {
let mut g = c.benchmark_group("next_frame_info");
bench_file(&mut g, Path::new("tests/animated/basic_f20.png"), 18, 35);
}

fn bench_file(
g: &mut BenchmarkGroup<WallTime>,
png_path: &Path,
number_of_frames_to_skip: usize,
expected_fctl_sequence_number: u32,
) {
let data = fs::read(png_path).unwrap();
let name = format!("{}: {} skips", png_path.display(), number_of_frames_to_skip);
g.bench_with_input(&name, data.as_slice(), |b, data| {
b.iter(|| {
let decoder = Decoder::new(data);
let mut reader = decoder.read_info().unwrap();
for _ in 0..number_of_frames_to_skip {
reader.next_frame_info().unwrap();
}
assert_eq!(
reader
.info()
.frame_control
.as_ref()
.unwrap()
.sequence_number,
expected_fctl_sequence_number,
);
})
});
}

0 comments on commit 56ff289

Please sign in to comment.