pdfrum-parser 0.1.0

PDF file parser: lexer, xref, object store, damage recovery
//! Opening a document: header, xref, trailer, catalog. Ids `<class>/<stem>`.
//!
//! Pages are not touched. `pdfrum::Document::from_bytes` from memory, so
//! the number is parse and recovery, not the filesystem. Widest band in the
//! suite: most files open in tens of microseconds.

use std::hint::black_box;
use std::sync::Arc;
use std::time::Duration;

use criterion::{Criterion, criterion_main};
use pdfrum::Document;
use pdfrum_corpus::{CORPUS, bytes};

/// Opening a document.
fn open(c: &mut Criterion) {
    let mut group = c.benchmark_group("open");
    for doc in CORPUS {
        let raw = bytes(doc.stem);
        group.bench_function(format!("{}/{}", doc.class.name(), doc.stem), |b| {
            b.iter(|| {
                let opened = Document::from_bytes(Arc::clone(&raw));
                black_box(opened.map(|d| d.page_count()).ok())
            });
        });
    }
    group.finish();
}

// `criterion_group!` generates a `pub fn` the `missing_docs` lint cannot see a
// doc comment for — the attribute would land inside the macro's expansion.
#[allow(missing_docs, reason = "the item is generated by criterion_group!")]
mod group {
    use super::{Criterion, Duration, open};
    use criterion::criterion_group;

    criterion_group! {
        name = benches;
        // measurement behind them.
        config = Criterion::default()
            .measurement_time(Duration::from_secs(5))
            .warm_up_time(Duration::from_secs(2))
            .sample_size(50);
        targets = open
    }
}

criterion_main!(group::benches);