Skip to main content

parse_bench/
parse_bench.rs

1//! 只测 HTML 解析:页面全部预载入内存,单线程解析 `轮数` 遍,报告每遍耗时(越低越好)和最好一遍。
2//! `cargo run --release -p pagesift-html --example parse_bench -- <manifest.jsonl> [轮数]`
3//! manifest 每行是带 `path` 字段的 JSON。
4use std::time::Instant;
5
6fn main() {
7    let mut args = std::env::args().skip(1);
8    let manifest = args
9        .next()
10        .expect("usage: parse_bench <manifest.jsonl> [rounds]");
11    let rounds: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(5);
12    let pages: Vec<String> = std::fs::read_to_string(&manifest)
13        .unwrap()
14        .lines()
15        .filter_map(|l| {
16            let i = l.find("\"path\"")?;
17            let rest = &l[i + 6..];
18            let s = rest.find('"')? + 1;
19            let e = s + rest[s..].find('"')?;
20            std::fs::read(&rest[s..e]).ok()
21        })
22        .map(|b| String::from_utf8_lossy(&b).into_owned())
23        .collect();
24    let mut best = f64::MAX;
25    let mut nodes = 0;
26    for _ in 0..rounds {
27        let t = Instant::now();
28        nodes = 0;
29        for p in &pages {
30            nodes += std::hint::black_box(pagesift_html::Html::parse(p)).len();
31        }
32        let ms = t.elapsed().as_secs_f64() * 1e3;
33        best = best.min(ms);
34        print!("{ms:.1} ");
35    }
36    println!("| pages={} nodes={nodes} best_ms={best:.1}", pages.len());
37}