summaryrefslogtreecommitdiff
path: root/benches/jpeg_parsing.rs
blob: 6d3e4fc752605ac64a12b9ef263c4fc3e53a4448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Benchmark for JPEG parsing performance

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use geek_szitman_supercamera::protocol::jpeg::JpegParser;

// Sample JPEG data for benchmarking
const SAMPLE_JPEG: &[u8] = &[
    0xFF, 0xD8, // SOI
    0xFF, 0xE0, // APP0
    0x00, 0x10, // Length
    0x4A, 0x46, 0x49, 0x46, 0x00, // "JFIF\0"
    0x01, 0x01, // Version
    0x00, // Units
    0x00, 0x01, // Density
    0x00, 0x01, 0x00, 0x00, // No thumbnail
    0xFF, 0xC0, // SOF0
    0x00, 0x0B, // Length
    0x08, // Precision
    0x00, 0x40, // Height (64)
    0x00, 0x40, // Width (64)
    0x03, // Components
    0x01, 0x11, 0x00, // Y component
    0x02, 0x11, 0x01, // U component
    0x03, 0x11, 0x01, // V component
    0xFF, 0xD9, // EOI
];

fn benchmark_jpeg_parsing(c: &mut Criterion) {
    let parser = JpegParser::new();

    c.bench_function("parse_jpeg_dimensions", |b| {
        b.iter(|| parser.parse_dimensions(black_box(SAMPLE_JPEG)))
    });

    c.bench_function("parse_jpeg_metadata", |b| {
        b.iter(|| parser.parse_metadata(black_box(SAMPLE_JPEG)))
    });

    c.bench_function("validate_jpeg", |b| {
        b.iter(|| parser.validate_jpeg(black_box(SAMPLE_JPEG)))
    });
}

fn benchmark_jpeg_parser_creation(c: &mut Criterion) {
    c.bench_function("create_jpeg_parser", |b| b.iter(|| JpegParser::new()));

    c.bench_function("create_jpeg_parser_with_debug", |b| {
        b.iter(|| JpegParser::new_with_debug(true))
    });
}

criterion_group!(
    benches,
    benchmark_jpeg_parsing,
    benchmark_jpeg_parser_creation
);
criterion_main!(benches);