/* * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: Apache-2.0 OR MIT */ use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use mail_parser::html_to_text; use std::{hint::black_box, time::Duration}; const SIZES: [(&str, usize); 3] = [("4k", 4 << 10), ("64k", 64 << 10), ("1m", 1 << 20)]; const HEAD: &str = "Weekly\ \ "; const TAIL: &str = ""; const NEWSLETTER: &[&str] = &[ "\
New arrivals this week for you & your team
\n", "

Hello Jane, here is what changed since \ your last visit.
See the full list online.

\n", "\"Logo brand\" width=\"120\" height=\"40\">\n", "
Prices start at 10€ and ship in 2–3 days.
\n", "

Questions? Reply to this message.

\n", ]; const PLAIN: &[&str] = &[ "

The quick brown fox jumps over the lazy dog, and then keeps running down the road.

\n", "

Plain paragraphs with few tags and no attributes at all, mostly text.
Second line.

\n", ]; fn document(blocks: &[&str], size: usize) -> String { let mut html = String::with_capacity(size + HEAD.len() + TAIL.len() + 512); html.push_str(HEAD); for block in blocks.iter().cycle() { if html.len() >= size { break; } html.push_str(block); } html.push_str(TAIL); html } fn html(c: &mut Criterion) { let mut group = c.benchmark_group("html_to_text"); for (kind, blocks) in [("newsletter", NEWSLETTER), ("plain", PLAIN)] { for (size_name, size) in SIZES { let input = document(blocks, size); group.throughput(Throughput::Bytes(input.len() as u64)); group.bench_function(BenchmarkId::new(kind, size_name), |b| { b.iter(|| html_to_text(black_box(&input))) }); } } group.finish(); } fn config() -> Criterion { Criterion::default() .warm_up_time(Duration::from_secs(1)) .measurement_time(Duration::from_secs(3)) .sample_size(50) .noise_threshold(0.02) } criterion_group! { name = benches; config = config(); targets = html } criterion_main!(benches);