| 1 |
use criterion::{measurement::Measurement, BenchmarkGroup, BenchmarkId, Criterion, Throughput}; |
| 2 |
use gif::Decoder; |
| 3 |
use std::hint::black_box; |
| 4 |
|
| 5 |
fn read_image(image: &[u8]) -> Option<Vec<u8>> { |
| 6 |
let decoder = Decoder::new(black_box(image)); |
| 7 |
//decoder.set_param(gif::ColorOutput::RGBA); |
| 8 |
let mut reader = decoder.unwrap(); |
| 9 |
|
| 10 |
if reader.next_frame_info().unwrap().is_some() { |
| 11 |
let mut v = vec![0; reader.buffer_size()]; |
| 12 |
reader.fill_buffer(&mut v).unwrap(); |
| 13 |
Some(v) |
| 14 |
} else { |
| 15 |
None |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
fn read_metadata(image: &[u8]) { |
| 20 |
let decoder = Decoder::new(black_box(image)); |
| 21 |
decoder.unwrap(); |
| 22 |
} |
| 23 |
|
| 24 |
fn main() { |
| 25 |
struct BenchDef { |
| 26 |
data: &'static [u8], |
| 27 |
id: &'static str, |
| 28 |
sample_size: usize, |
| 29 |
} |
| 30 |
|
| 31 |
fn run_bench_def<M: Measurement>(group: &mut BenchmarkGroup<'_, M>, def: BenchDef) { |
| 32 |
group |
| 33 |
.sample_size(def.sample_size) |
| 34 |
.throughput(Throughput::Bytes(def.data.len() as u64)) |
| 35 |
.bench_with_input( |
| 36 |
BenchmarkId::new(def.id, def.data.len()), |
| 37 |
def.data, |
| 38 |
|b, input| { |
| 39 |
b.iter(|| read_image(input)); |
| 40 |
}, |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
let mut c = Criterion::default().configure_from_args(); |
| 45 |
let mut group = c.benchmark_group("gif"); |
| 46 |
|
| 47 |
run_bench_def( |
| 48 |
&mut group, |
| 49 |
BenchDef { |
| 50 |
data: include_bytes!("note.gif"), |
| 51 |
id: "note.gif", |
| 52 |
sample_size: 100, |
| 53 |
}, |
| 54 |
); |
| 55 |
|
| 56 |
run_bench_def( |
| 57 |
&mut group, |
| 58 |
BenchDef { |
| 59 |
data: include_bytes!("photo.gif"), |
| 60 |
id: "photo.gif", |
| 61 |
sample_size: 20, |
| 62 |
}, |
| 63 |
); |
| 64 |
|
| 65 |
run_bench_def( |
| 66 |
&mut group, |
| 67 |
BenchDef { |
| 68 |
data: include_bytes!("../tests/samples/sample_1.gif"), |
| 69 |
id: "sample_1.gif", |
| 70 |
sample_size: 100, |
| 71 |
}, |
| 72 |
); |
| 73 |
|
| 74 |
run_bench_def( |
| 75 |
&mut group, |
| 76 |
BenchDef { |
| 77 |
data: include_bytes!("../tests/samples/sample_big.gif"), |
| 78 |
id: "sample_big.gif", |
| 79 |
sample_size: 20, |
| 80 |
}, |
| 81 |
); |
| 82 |
|
| 83 |
group.bench_with_input( |
| 84 |
"extract-metadata-note", |
| 85 |
include_bytes!("note.gif"), |
| 86 |
|b, input| { |
| 87 |
b.iter(|| read_metadata(input)); |
| 88 |
}, |
| 89 |
); |
| 90 |
|
| 91 |
group.finish(); |
| 92 |
|
| 93 |
c.final_summary(); |
| 94 |
} |