| 1 |
extern crate criterion; |
| 2 |
|
| 3 |
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 4 |
use image::ExtendedColorType; |
| 5 |
use image::{codecs::bmp::BmpEncoder, codecs::jpeg::JpegEncoder, ColorType}; |
| 6 |
|
| 7 |
use std::fs::File; |
| 8 |
use std::io::{BufWriter, Seek, SeekFrom, Write}; |
| 9 |
|
| 10 |
trait Encoder { |
| 11 |
fn encode_raw(&self, into: &mut Vec<u8>, im: &[u8], dims: u32, color: ExtendedColorType); |
| 12 |
fn encode_bufvec(&self, into: &mut Vec<u8>, im: &[u8], dims: u32, color: ExtendedColorType); |
| 13 |
fn encode_file(&self, file: &File, im: &[u8], dims: u32, color: ExtendedColorType); |
| 14 |
} |
| 15 |
|
| 16 |
#[derive(Clone, Copy)] |
| 17 |
struct BenchDef { |
| 18 |
with: &'static dyn Encoder, |
| 19 |
name: &'static str, |
| 20 |
sizes: &'static [u32], |
| 21 |
colors: &'static [ColorType], |
| 22 |
} |
| 23 |
|
| 24 |
fn encode_all(c: &mut Criterion) { |
| 25 |
const BENCH_DEFS: &[BenchDef] = &[ |
| 26 |
BenchDef { |
| 27 |
with: &Bmp, |
| 28 |
name: "bmp", |
| 29 |
sizes: &[100u32, 200, 400], |
| 30 |
colors: &[ColorType::L8, ColorType::Rgb8, ColorType::Rgba8], |
| 31 |
}, |
| 32 |
BenchDef { |
| 33 |
with: &Jpeg, |
| 34 |
name: "jpeg", |
| 35 |
sizes: &[64u32, 128, 256], |
| 36 |
colors: &[ColorType::L8, ColorType::Rgb8, ColorType::Rgba8], |
| 37 |
}, |
| 38 |
]; |
| 39 |
|
| 40 |
for definition in BENCH_DEFS { |
| 41 |
encode_definition(c, definition); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
criterion_group!(benches, encode_all); |
| 46 |
criterion_main!(benches); |
| 47 |
|
| 48 |
type BenchGroup<'a> = criterion::BenchmarkGroup<'a, criterion::measurement::WallTime>; |
| 49 |
|
| 50 |
/// Benchmarks encoding a zeroed image. |
| 51 |
/// |
| 52 |
/// For compressed formats this is surely not representative of encoding a normal image but it's a |
| 53 |
/// start for benchmarking. |
| 54 |
fn encode_zeroed(group: &mut BenchGroup, with: &dyn Encoder, size: u32, color: ExtendedColorType) { |
| 55 |
let im = vec![0; (color.bits_per_pixel() as usize * size as usize).div_ceil(8) * size as usize]; |
| 56 |
|
| 57 |
group.bench_with_input( |
| 58 |
BenchmarkId::new(format!("zero-{color:?}-rawvec"), size), |
| 59 |
&im, |
| 60 |
|b, image| { |
| 61 |
let mut v = vec![]; |
| 62 |
with.encode_raw(&mut v, &im, size, color); |
| 63 |
b.iter(|| with.encode_raw(&mut v, image, size, color)); |
| 64 |
}, |
| 65 |
); |
| 66 |
group.bench_with_input( |
| 67 |
BenchmarkId::new(format!("zero-{color:?}-bufvec"), size), |
| 68 |
&im, |
| 69 |
|b, image| { |
| 70 |
let mut v = vec![]; |
| 71 |
with.encode_raw(&mut v, &im, size, color); |
| 72 |
b.iter(|| with.encode_bufvec(&mut v, image, size, color)); |
| 73 |
}, |
| 74 |
); |
| 75 |
group.bench_with_input( |
| 76 |
BenchmarkId::new(format!("zero-{color:?}-file"), size), |
| 77 |
&im, |
| 78 |
|b, image| { |
| 79 |
let file = File::create("temp.bmp").unwrap(); |
| 80 |
b.iter(|| with.encode_file(&file, image, size, color)); |
| 81 |
}, |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
fn encode_definition(criterion: &mut Criterion, def: &BenchDef) { |
| 86 |
let mut group = criterion.benchmark_group(format!("encode-{}", def.name)); |
| 87 |
|
| 88 |
for &color in def.colors { |
| 89 |
for &size in def.sizes { |
| 90 |
encode_zeroed(&mut group, def.with, size, color.into()); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
struct Bmp; |
| 96 |
|
| 97 |
struct Jpeg; |
| 98 |
|
| 99 |
trait EncoderBase { |
| 100 |
fn encode(&self, into: impl Write, im: &[u8], dims: u32, color: ExtendedColorType); |
| 101 |
} |
| 102 |
|
| 103 |
impl<T: EncoderBase> Encoder for T { |
| 104 |
fn encode_raw(&self, into: &mut Vec<u8>, im: &[u8], dims: u32, color: ExtendedColorType) { |
| 105 |
into.clear(); |
| 106 |
self.encode(into, im, dims, color); |
| 107 |
} |
| 108 |
|
| 109 |
fn encode_bufvec(&self, into: &mut Vec<u8>, im: &[u8], dims: u32, color: ExtendedColorType) { |
| 110 |
into.clear(); |
| 111 |
let buf = BufWriter::new(into); |
| 112 |
self.encode(buf, im, dims, color); |
| 113 |
} |
| 114 |
|
| 115 |
fn encode_file(&self, mut file: &File, im: &[u8], dims: u32, color: ExtendedColorType) { |
| 116 |
file.seek(SeekFrom::Start(0)).unwrap(); |
| 117 |
let buf = BufWriter::new(file); |
| 118 |
self.encode(buf, im, dims, color); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl EncoderBase for Bmp { |
| 123 |
fn encode(&self, mut into: impl Write, im: &[u8], size: u32, color: ExtendedColorType) { |
| 124 |
let mut x = BmpEncoder::new(&mut into); |
| 125 |
x.encode(im, size, size, color).unwrap(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
impl EncoderBase for Jpeg { |
| 130 |
fn encode(&self, mut into: impl Write, im: &[u8], size: u32, color: ExtendedColorType) { |
| 131 |
let mut x = JpegEncoder::new(&mut into); |
| 132 |
x.encode(im, size, size, color).unwrap(); |
| 133 |
} |
| 134 |
} |