|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | + |
| 4 | +use criterion::{criterion_group, criterion_main, Criterion, Throughput}; |
| 5 | +use std::hash::{BuildHasher, Hasher}; |
| 6 | + |
| 7 | +use stringzilla::sz::{checksum as sz_checksum, hash as sz_hash}; |
| 8 | +use stringzilla::StringZilla; |
| 9 | + |
| 10 | +use ahash::AHasher; |
| 11 | +use xxhash_rust::const_xxh3::xxh3_64 as const_xxh3; |
| 12 | +use xxhash_rust::xxh3::xxh3_64; |
| 13 | + |
| 14 | +// Mode: "lines", "words", "file" |
| 15 | +// STRINGWARS_MODE controls how we interpret the input data. |
| 16 | +fn configure_bench() -> Criterion { |
| 17 | + Criterion::default() |
| 18 | + .sample_size(1000) // Number of iterations per benchmark. |
| 19 | + .warm_up_time(std::time::Duration::from_secs(10)) // Let CPU frequencies settle. |
| 20 | + .measurement_time(std::time::Duration::from_secs(120)) // Actual measurement time. |
| 21 | +} |
| 22 | + |
| 23 | +fn bench_hash(c: &mut Criterion) { |
| 24 | + let dataset_path = |
| 25 | + env::var("STRINGWARS_DATASET").expect("STRINGWARS_DATASET environment variable not set"); |
| 26 | + let mode = env::var("STRINGWARS_MODE").unwrap_or_else(|_| "lines".to_string()); |
| 27 | + |
| 28 | + let content = fs::read_to_string(&dataset_path).expect("Could not read dataset"); |
| 29 | + let units: Vec<&str> = match mode.as_str() { |
| 30 | + "lines" => content.lines().collect(), |
| 31 | + "words" => content.split_whitespace().collect(), |
| 32 | + "file" => { |
| 33 | + // In "file" mode, treat the entire content as a single unit. |
| 34 | + vec![&content] |
| 35 | + } |
| 36 | + other => panic!( |
| 37 | + "Unknown STRINGWARS_MODE: {}. Use 'lines', 'words', or 'file'.", |
| 38 | + other |
| 39 | + ), |
| 40 | + }; |
| 41 | + |
| 42 | + if units.is_empty() { |
| 43 | + panic!("No data found for hashing in the provided dataset."); |
| 44 | + } |
| 45 | + |
| 46 | + // Calculate total bytes processed for throughput reporting |
| 47 | + let total_bytes: usize = units.iter().map(|u| u.len()).sum(); |
| 48 | + |
| 49 | + let mut g = c.benchmark_group("hash"); |
| 50 | + g.throughput(Throughput::Bytes(total_bytes as u64)); |
| 51 | + |
| 52 | + perform_hashing_benchmarks(&mut g, &units); |
| 53 | + |
| 54 | + g.finish(); |
| 55 | +} |
| 56 | + |
| 57 | +fn perform_hashing_benchmarks( |
| 58 | + g: &mut criterion::BenchmarkGroup<'_, criterion::measurement::WallTime>, |
| 59 | + units: &[&str], |
| 60 | +) { |
| 61 | + // Benchmark StringZilla checksums |
| 62 | + let mut index = 0; |
| 63 | + g.bench_function("stringzilla::checksum", |b| { |
| 64 | + b.iter(|| { |
| 65 | + let unit = units[index]; |
| 66 | + let _hash = sz_checksum(unit.as_bytes()); |
| 67 | + index = (index + 1) % units.len(); |
| 68 | + }) |
| 69 | + }); |
| 70 | + |
| 71 | + // Benchmark StringZilla hashing |
| 72 | + let mut index = 0; |
| 73 | + g.bench_function("stringzilla::hash", |b| { |
| 74 | + b.iter(|| { |
| 75 | + let unit = units[index]; |
| 76 | + let _hash = sz_hash(unit.as_bytes()); |
| 77 | + index = (index + 1) % units.len(); |
| 78 | + }) |
| 79 | + }); |
| 80 | + |
| 81 | + // Benchmark aHash |
| 82 | + let mut index = 0; |
| 83 | + let ahash_builder = ahash::RandomState::new(); |
| 84 | + g.bench_function("aHash", |b| { |
| 85 | + b.iter(|| { |
| 86 | + let unit = units[index]; |
| 87 | + let mut hasher = ahash_builder.build_hasher(); |
| 88 | + hasher.write(unit.as_bytes()); |
| 89 | + let _hash = hasher.finish(); |
| 90 | + index = (index + 1) % units.len(); |
| 91 | + }) |
| 92 | + }); |
| 93 | + |
| 94 | + // Benchmark xxHash (xxh3) |
| 95 | + let mut index = 0; |
| 96 | + g.bench_function("xxh3", |b| { |
| 97 | + b.iter(|| { |
| 98 | + let unit = units[index]; |
| 99 | + let _hash = xxh3_64(unit.as_bytes()); |
| 100 | + index = (index + 1) % units.len(); |
| 101 | + }) |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +criterion_group! { |
| 106 | + name = bench_hash_group; |
| 107 | + config = configure_bench(); |
| 108 | + targets = bench_hash |
| 109 | +} |
| 110 | +criterion_main!(bench_hash_group); |
0 commit comments