Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark for common::Frame::from_rgba_speed #119

Merged
merged 3 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ color_quant = "1.0"
[dev-dependencies]
glob = "0.3"
criterion = "0.3.1"
png = "0.17.2"

[features]
default = ["raii_no_panic", "std"]
Expand Down Expand Up @@ -51,3 +52,8 @@ required-features = ["std"]
name = "decode"
harness = false
required-features = ["std"]

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

use criterion::{Criterion, Throughput};
use gif::{Encoder, Frame, Repeat};
use png;

const DIR: &str = "benches/samples";

fn main()
{
let mut c = Criterion::default().configure_from_args();
let mut group = c.benchmark_group("rgb_frame");

let dir = fs::read_dir(DIR).expect("Cant'r read dir:\n{}");

for path in dir {
let path = path.expect("Can't read path:\n{}").path();
if path.extension().unwrap() != "png" {
continue;
}

let mut reader = {
let input = fs::File::open(&path).unwrap();
let decoder = png::Decoder::new(input);
decoder.read_info().unwrap()
};

let mut buf = vec![0; reader.output_buffer_size()];
let info = reader.next_frame(&mut buf).unwrap();

let (w, h, size) = {
// could use try_into().unwrap() but probably no need
(info.width as u16, info.height as u16, info.buffer_size())
};

//size might have to be adjusted for large images
group
.sample_size(50)
.throughput(Throughput::Bytes(size as u64))
.bench_function(path.file_name().unwrap().to_str().unwrap(),
|b| {
match info.color_type {
png::ColorType::Rgb => b.iter(|| {
Frame::from_rgb_speed(w, h, &mut buf[..size], 30)
}),
png::ColorType::Rgba => b.iter(|| {
Frame::from_rgba_speed(w, h, &mut buf[..size], 30)
}),
c => {
println!("Image has wrong color type: {:?}", c);
}
}
});

// actually write the image as a singe frame gif... while MSE can be used
// for quality check, it might not be as good as visual inspection
let mut encoder = {
let output = fs::File::create(path.with_extension("gif")).unwrap();
Encoder::new(output, w, h, &[]).unwrap()
};
encoder.set_repeat(Repeat::Finite(0)).unwrap();

let frame = match info.color_type {
png::ColorType::Rgb => Frame::from_rgb(w, h, &mut buf[..size]),
png::ColorType::Rgba => Frame::from_rgba(w, h, &mut buf[..size]),
_ => continue,
};

encoder.write_frame(&frame).unwrap();
}
group.finish();
c.final_summary();
}
Binary file added benches/samples/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added benches/samples/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.