|
| 1 | +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; |
| 5 | + |
| 6 | +fn normalize_service_bench(c: &mut Criterion) { |
| 7 | + let mut group = c.benchmark_group("normalization"); |
| 8 | + let cases = &[ |
| 9 | + ("#test_starting_hash", "test_starting_hash"), |
| 10 | + ("TestCAPSandSuch", "testcapsandsuch"), |
| 11 | + ( |
| 12 | + "Test Conversion Of Weird !@#$%^&**() Characters", |
| 13 | + "test_conversion_of_weird_characters", |
| 14 | + ), |
| 15 | + ("$#weird_starting", "weird_starting"), |
| 16 | + ("allowed:c0l0ns", "allowed:c0l0ns"), |
| 17 | + ("1love", "love"), |
| 18 | + ("ünicöde", "ünicöde"), |
| 19 | + ("ünicöde:metäl", "ünicöde:metäl"), |
| 20 | + ("Data🐨dog🐶 繋がっ⛰てて", "data_dog_繋がっ_てて"), |
| 21 | + (" spaces ", "spaces"), |
| 22 | + (" #hashtag!@#spaces #__<># ", "hashtag_spaces"), |
| 23 | + (":testing", ":testing"), |
| 24 | + ("_foo", "foo"), |
| 25 | + (":::test", ":::test"), |
| 26 | + ("contiguous_____underscores", "contiguous_underscores"), |
| 27 | + ("foo_", "foo"), |
| 28 | + ( |
| 29 | + "\u{017F}odd_\u{017F}case\u{017F}", |
| 30 | + "\u{017F}odd_\u{017F}case\u{017F}", |
| 31 | + ), // edge-case |
| 32 | + ("", ""), |
| 33 | + (" ", ""), |
| 34 | + ("ok", "ok"), |
| 35 | + ("™Ö™Ö™™Ö™", "ö_ö_ö"), |
| 36 | + ("AlsO:ök", "also:ök"), |
| 37 | + (":still_ok", ":still_ok"), |
| 38 | + ("___trim", "trim"), |
| 39 | + ("12.:trim@", ":trim"), |
| 40 | + ("12.:trim@@", ":trim"), |
| 41 | + ("fun:ky__tag/1", "fun:ky_tag/1"), |
| 42 | + ("fun:ky@tag/2", "fun:ky_tag/2"), |
| 43 | + ("fun:ky@@@tag/3", "fun:ky_tag/3"), |
| 44 | + ("tag:1/2.3", "tag:1/2.3"), |
| 45 | + ("---fun:k####y_ta@#g/1_@@#", "fun:k_y_ta_g/1"), |
| 46 | + ("AlsO:œ#@ö))œk", "also:œ_ö_œk"), |
| 47 | + ( |
| 48 | + "A00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000", |
| 49 | + "a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_0" |
| 50 | + ), |
| 51 | + ]; |
| 52 | + |
| 53 | + group.bench_function("normalize_service", |b| { |
| 54 | + b.iter_batched_ref( |
| 55 | + || cases.iter().map(|(c, _)| c.to_string()).collect::<Vec<_>>(), |
| 56 | + |cases| { |
| 57 | + for c in cases { |
| 58 | + datadog_trace_normalization::normalize_utils::normalize_service(c); |
| 59 | + } |
| 60 | + }, |
| 61 | + BatchSize::NumIterations(100000), |
| 62 | + ) |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +fn normalize_name_bench(c: &mut Criterion) { |
| 67 | + let mut group = c.benchmark_group("normalization"); |
| 68 | + let cases = &[ |
| 69 | + "good", |
| 70 | + "bad-name", |
| 71 | + "Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.", |
| 72 | + ]; |
| 73 | + for case in cases { |
| 74 | + group.bench_with_input( |
| 75 | + BenchmarkId::new("normalize_name", case), |
| 76 | + *case, |
| 77 | + |b, case| { |
| 78 | + b.iter_batched_ref( |
| 79 | + || case.to_owned(), |
| 80 | + datadog_trace_normalization::normalize_utils::normalize_name, |
| 81 | + BatchSize::NumIterations(100000), |
| 82 | + ) |
| 83 | + }, |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +criterion_group!(benches, normalize_service_bench, normalize_name_bench); |
| 89 | +criterion_main!(benches); |
0 commit comments