|
| 1 | +//! Criterion microbench for table extraction over the fixture corpus. |
| 2 | +//! |
| 3 | +//! Run with: `cargo bench -p paperjam-core --bench table_extraction` |
| 4 | +//! |
| 5 | +//! Each synthetic PDF fixture under `tests/fixtures/tables/` becomes a bench group. |
| 6 | +//! The document and page are parsed outside the measured section so the bench only |
| 7 | +//! measures `table::extract_tables` itself — that's the code subsequent phases will |
| 8 | +//! change. |
| 9 | +
|
| 10 | +use std::path::PathBuf; |
| 11 | + |
| 12 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; |
| 13 | +use paperjam_core::document::Document; |
| 14 | +use paperjam_core::table::{extract_tables, TableExtractionOptions}; |
| 15 | + |
| 16 | +fn fixtures_dir() -> PathBuf { |
| 17 | + let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 18 | + manifest.join("../../tests/fixtures/tables") |
| 19 | +} |
| 20 | + |
| 21 | +fn collect_fixtures() -> Vec<PathBuf> { |
| 22 | + let mut out = Vec::new(); |
| 23 | + let dir = fixtures_dir(); |
| 24 | + let Ok(rd) = std::fs::read_dir(&dir) else { |
| 25 | + eprintln!( |
| 26 | + "skipping bench: fixtures dir not found at {}", |
| 27 | + dir.display() |
| 28 | + ); |
| 29 | + return out; |
| 30 | + }; |
| 31 | + for entry in rd.flatten() { |
| 32 | + let path = entry.path(); |
| 33 | + if path.extension().and_then(|s| s.to_str()) == Some("pdf") { |
| 34 | + out.push(path); |
| 35 | + } |
| 36 | + } |
| 37 | + out.sort(); |
| 38 | + out |
| 39 | +} |
| 40 | + |
| 41 | +fn bench_extract(c: &mut Criterion) { |
| 42 | + let fixtures = collect_fixtures(); |
| 43 | + let opts = TableExtractionOptions::default(); |
| 44 | + |
| 45 | + let mut group = c.benchmark_group("table_extraction"); |
| 46 | + for path in &fixtures { |
| 47 | + let name = path |
| 48 | + .file_stem() |
| 49 | + .and_then(|s| s.to_str()) |
| 50 | + .unwrap_or("?") |
| 51 | + .to_string(); |
| 52 | + // Parse the document once and keep its pages alive for the whole bench run. |
| 53 | + let doc = match Document::open(path) { |
| 54 | + Ok(d) => d, |
| 55 | + Err(e) => { |
| 56 | + eprintln!("skipping {name}: failed to open ({e:?})"); |
| 57 | + continue; |
| 58 | + } |
| 59 | + }; |
| 60 | + let mut pages = Vec::new(); |
| 61 | + for n in 1..=doc.page_count() as u32 { |
| 62 | + if let Ok(p) = doc.page(n) { |
| 63 | + pages.push(p); |
| 64 | + } |
| 65 | + } |
| 66 | + group.throughput(Throughput::Elements(pages.len() as u64)); |
| 67 | + group.bench_with_input(BenchmarkId::from_parameter(&name), &pages, |b, pages| { |
| 68 | + b.iter(|| { |
| 69 | + for page in pages.iter() { |
| 70 | + let _ = extract_tables(page, &opts); |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + } |
| 75 | + group.finish(); |
| 76 | +} |
| 77 | + |
| 78 | +criterion_group!(benches, bench_extract); |
| 79 | +criterion_main!(benches); |
0 commit comments