#![feature(test)]
extern crate test;
use std::{fs, path::PathBuf};
use test::Bencher;
fn bench_all_samples(b: &mut Bencher, name: &str, fnc: fn(&[u8], &str)) {
const SEPARATOR: &[u8] = "\n---- EXPECTED STRUCTURE ----\n".as_bytes();
println!("Benchmarking {}...\n", name);
let mut test_data = Vec::new();
for test_suite in ["rfc", "legacy", "thirdparty", "malformed"] {
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("tests");
test_dir.push(test_suite);
let mut bench_count = 0;
for file_name in fs::read_dir(&test_dir).unwrap() {
let file_name = file_name.as_ref().unwrap().path();
if file_name.extension().map_or(false, |e| e == "txt") {
let input = fs::read(&file_name).unwrap();
let mut pos = 0;
for sep_pos in 0..input.len() {
if input[sep_pos..sep_pos + SEPARATOR.len()].eq(SEPARATOR) {
pos = sep_pos;
break;
}
}
assert!(
pos > 0,
"Failed to find separator in test file '{}'.",
file_name.display()
);
test_data.push(Vec::from(&input[0..pos]));
bench_count += 1;
}
}
assert!(
bench_count > 0,
"Did not find any benchmarks to run in folder {}.",
test_dir.display()
);
}
b.iter(|| {
for test_msg in &test_data {
fnc(test_msg, String::from_utf8_lossy(&test_msg).as_ref());
}
});
}
#[bench]
fn bench_stalwart(b: &mut Bencher) {
bench_all_samples(b, "stalwart_mail_parser", |bytes, _str| {
mail_parser::Message::parse(bytes);
});
}