|
1 | 1 | use criterion::{Criterion, criterion_group, criterion_main};
|
| 2 | +use once_cell::sync::Lazy; |
2 | 3 | use std::hint::black_box;
|
| 4 | +use std::time::Duration; |
3 | 5 | use string_pipeline::Template;
|
4 | 6 |
|
5 |
| -fn criterion_benchmark(c: &mut Criterion) { |
6 |
| - c.bench_function("process_simple", |b| { |
7 |
| - b.iter(|| { |
8 |
| - Template::parse(black_box("{split:/:-1}")) |
9 |
| - .unwrap() |
10 |
| - .format(black_box("/home/user/.cargo/bin")) |
11 |
| - .unwrap() |
12 |
| - }) |
| 7 | +// ----------------------------------------------------------------------------- |
| 8 | +// Test data |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +const SMALL_INPUT: &str = "apple,banana,cherry,date,elderberry,fig,grape,honeydew,kiwi,lemon"; |
| 12 | +static LARGE_INPUT: Lazy<String> = Lazy::new(|| SMALL_INPUT.repeat(1_000)); // ~600 KB |
| 13 | + |
| 14 | +// ----------------------------------------------------------------------------- |
| 15 | +// 1. Parsing Benchmarks – How fast can we compile templates? |
| 16 | +// ----------------------------------------------------------------------------- |
| 17 | + |
| 18 | +fn bench_parsing(c: &mut Criterion) { |
| 19 | + let cases = [ |
| 20 | + ("simple", "{upper}"), |
| 21 | + ("medium", "{split:,:..|join: }"), |
| 22 | + ( |
| 23 | + "complex", |
| 24 | + "{split:,:..|filter:^[a-m]|map:{trim|upper|substring:0..3}|sort|join:,}", |
| 25 | + ), |
| 26 | + ("nested_map", "{split:,:..|map:{split:_:..|reverse}|join: }"), |
| 27 | + ]; |
| 28 | + |
| 29 | + let mut group = c.benchmark_group("template_parsing"); |
| 30 | + for (name, tpl) in cases { |
| 31 | + group.bench_function(name, |b| { |
| 32 | + b.iter(|| Template::parse(black_box(tpl)).unwrap()) |
| 33 | + }); |
| 34 | + } |
| 35 | + group.finish(); |
| 36 | +} |
| 37 | + |
| 38 | +// ----------------------------------------------------------------------------- |
| 39 | +// 2. Execution Benchmarks – Runtime performance of compiled templates |
| 40 | +// ----------------------------------------------------------------------------- |
| 41 | + |
| 42 | +fn bench_execution(c: &mut Criterion) { |
| 43 | + // (id, template, input) |
| 44 | + let cases = [ |
| 45 | + ("split_join_small", "{split:,:..|join: }", SMALL_INPUT), |
| 46 | + ("split_join_large", "{split:,:..|join: }", &LARGE_INPUT), |
| 47 | + ( |
| 48 | + "filter_sort", |
| 49 | + "{split:,:..|filter:^[a-m]|sort|join:,}", |
| 50 | + SMALL_INPUT, |
| 51 | + ), |
| 52 | + ("map_upper", "{split:,:..|map:{upper}|join:,}", SMALL_INPUT), |
| 53 | + ( |
| 54 | + "complex_nested", |
| 55 | + "{split:,:..|filter:^[a-m]|map:{reverse|upper}|sort|join:,}", |
| 56 | + SMALL_INPUT, |
| 57 | + ), |
| 58 | + ]; |
| 59 | + |
| 60 | + let mut group = c.benchmark_group("template_execution"); |
| 61 | + for (name, tpl_str, input) in cases { |
| 62 | + // Compile once outside the measurement loop |
| 63 | + let tpl = Template::parse(tpl_str).unwrap(); |
| 64 | + group.bench_function(name, |b| b.iter(|| tpl.format(black_box(input)).unwrap())); |
| 65 | + } |
| 66 | + group.finish(); |
| 67 | +} |
| 68 | + |
| 69 | +// ----------------------------------------------------------------------------- |
| 70 | +// 3. Cache Effectiveness – First run vs subsequent runs |
| 71 | +// ----------------------------------------------------------------------------- |
| 72 | + |
| 73 | +fn bench_caching(c: &mut Criterion) { |
| 74 | + let tpl_str = "{split:,:..|filter:a|join:,}"; |
| 75 | + let tpl = Template::parse(tpl_str).unwrap(); |
| 76 | + |
| 77 | + let mut group = c.benchmark_group("cache_effect"); |
| 78 | + |
| 79 | + // Measure first-call cost (cold caches) |
| 80 | + group.bench_function("first_call", |b| { |
| 81 | + b.iter(|| tpl.format(black_box(SMALL_INPUT)).unwrap()) |
13 | 82 | });
|
14 | 83 |
|
15 |
| - c.bench_function("process_complex", |b| { |
16 |
| - b.iter(|| { |
17 |
| - Template::parse(black_box( |
18 |
| - "{split:,:0..2|map:{trim|prepend:num\\: }|join: - |upper}", |
19 |
| - )) |
20 |
| - .unwrap() |
21 |
| - .format(black_box("18, 4.92, Unknown")) |
22 |
| - .unwrap() |
23 |
| - }) |
| 84 | + // Warm the caches once |
| 85 | + let _ = tpl.format(SMALL_INPUT).unwrap(); |
| 86 | + |
| 87 | + // Measure subsequent calls (hot caches) |
| 88 | + group.bench_function("subsequent_calls", |b| { |
| 89 | + b.iter(|| tpl.format(black_box(SMALL_INPUT)).unwrap()) |
24 | 90 | });
|
| 91 | + |
| 92 | + group.finish(); |
25 | 93 | }
|
26 | 94 |
|
27 |
| -criterion_group!(benches, criterion_benchmark); |
| 95 | +// ----------------------------------------------------------------------------- |
| 96 | +// Criterion configuration & entry point |
| 97 | +// ----------------------------------------------------------------------------- |
| 98 | + |
| 99 | +criterion_group! { |
| 100 | + name = benches; |
| 101 | + config = Criterion::default() |
| 102 | + .configure_from_args() |
| 103 | + .sample_size(200) |
| 104 | + .measurement_time(Duration::from_secs(5)); |
| 105 | + targets = bench_parsing, bench_execution, bench_caching |
| 106 | +} |
28 | 107 | criterion_main!(benches);
|
0 commit comments