photosync/benches/image.rs
Bill Thiede 7f64a4d2f6 Implement less abstracted nearest filtering.
Add some tests that try to compare image's Nearest with my
implementation.  According the PSNR they're very different, but to a
human the look the same.
2020-02-22 09:38:47 -08:00

96 lines
3.1 KiB
Rust

use criterion::BenchmarkId;
use criterion::Throughput;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use image::imageops;
use image::GenericImageView;
use photosync::library::load_image;
use photosync::library::resize;
use photosync::library::save_to_jpeg_bytes;
use photosync::library::FilterType;
pub fn criterion_benchmark(c: &mut Criterion) {
const TEST_IMAGE_PATH: &'static str = "testdata/image.jpg";
let img = load_image(TEST_IMAGE_PATH).expect("failed to load test image");
c.bench_function("Load image", |b| {
b.iter(|| black_box(load_image(TEST_IMAGE_PATH)))
});
let mut group = c.benchmark_group("Resizing");
for size in [
(None, None),
(Some(256), Some(256)),
(Some(512), Some(512)),
(Some(1024), Some(1024)),
(Some(2048), Some(2048)),
]
.iter()
{
let (w, h) = size;
for filter in [
FilterType::Builtin(imageops::Nearest),
FilterType::Builtin(imageops::CatmullRom),
FilterType::Builtin(imageops::Lanczos3),
FilterType::Nearest,
]
.iter()
{
let (img_w, img_h) = img.dimensions();
let pixels = (img_w * img_h) as u64;
group.throughput(Throughput::Elements(pixels));
group.bench_with_input(
BenchmarkId::new(
format!("{:?}", filter),
format!(
"{}x{}",
w.map(|i| i.to_string()).unwrap_or("FULL".to_string()),
h.map(|i| i.to_string()).unwrap_or("FULL".to_string())
),
),
&(size, filter),
|b, (size, &filter)| b.iter(|| black_box(resize(&img, **size, filter))),
);
}
group.bench_with_input(
BenchmarkId::new(
"Save to bytes",
format!(
"{}x{}",
w.map(|i| i.to_string()).unwrap_or("FULL".to_string()),
h.map(|i| i.to_string()).unwrap_or("FULL".to_string())
),
),
size,
|b, size| {
let small_img = resize(&img, *size, FilterType::Builtin(imageops::Lanczos3));
b.iter(|| black_box(save_to_jpeg_bytes(&small_img)))
},
);
group.bench_with_input(
BenchmarkId::new(
"Full pipeline Lanczos3",
format!(
"{}x{}",
w.map(|i| i.to_string()).unwrap_or("FULL".to_string()),
h.map(|i| i.to_string()).unwrap_or("FULL".to_string())
),
),
size,
|b, size| {
b.iter(|| {
let img = load_image(TEST_IMAGE_PATH).expect("failed to load test image");
let small_img = resize(&img, *size, FilterType::Builtin(imageops::Lanczos3));
black_box(save_to_jpeg_bytes(&small_img))
})
},
);
}
group.finish();
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);