|
| 1 | +// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use criterion::{black_box, criterion_group, BatchSize, BenchmarkId, Criterion, Throughput}; |
| 5 | +use libdd_profiling::profiles::datatypes::ProfilesDictionary; |
| 6 | +use std::sync::Barrier; |
| 7 | +use std::thread; |
| 8 | +use std::time::Duration; |
| 9 | + |
| 10 | +const THREAD_COUNTS: [usize; 4] = [1, 2, 4, 16]; |
| 11 | +const STRINGS_PER_THREAD: usize = 1024; |
| 12 | + |
| 13 | +fn make_strings(thread_count: usize) -> Vec<Vec<String>> { |
| 14 | + (0..thread_count) |
| 15 | + .map(|thread_id| { |
| 16 | + (0..STRINGS_PER_THREAD) |
| 17 | + .map(|string_id| { |
| 18 | + format!( |
| 19 | + "/opt/datadog/profiler/thread-{thread_id}/module-{string_id:04}/function-{}::{}", |
| 20 | + string_id % 97, |
| 21 | + string_id.wrapping_mul(2_654_435_761usize) |
| 22 | + ) |
| 23 | + }) |
| 24 | + .collect() |
| 25 | + }) |
| 26 | + .collect() |
| 27 | +} |
| 28 | + |
| 29 | +fn insert_profile_strings(dict: &ProfilesDictionary, strings: &[String]) { |
| 30 | + for string in strings { |
| 31 | + black_box(dict.try_insert_str2(black_box(string.as_str())).unwrap()); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +fn insert_dictionary_strings_concurrently(strings: &[Vec<String>]) -> ProfilesDictionary { |
| 36 | + let dict = ProfilesDictionary::try_new().unwrap(); |
| 37 | + |
| 38 | + if let [strings] = strings { |
| 39 | + insert_profile_strings(&dict, strings); |
| 40 | + return dict; |
| 41 | + } |
| 42 | + |
| 43 | + let barrier = Barrier::new(strings.len()); |
| 44 | + thread::scope(|scope| { |
| 45 | + for thread_strings in strings { |
| 46 | + let dict = &dict; |
| 47 | + let barrier = &barrier; |
| 48 | + scope.spawn(move || { |
| 49 | + barrier.wait(); |
| 50 | + insert_profile_strings(dict, thread_strings); |
| 51 | + }); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + dict |
| 56 | +} |
| 57 | + |
| 58 | +pub fn bench_profiles_dictionary(c: &mut Criterion) { |
| 59 | + let mut group = c.benchmark_group("profiles_dictionary/unique_string_inserts"); |
| 60 | + group.warm_up_time(Duration::from_secs(1)); |
| 61 | + group.measurement_time(Duration::from_secs(5)); |
| 62 | + group.sample_size(10); |
| 63 | + |
| 64 | + for thread_count in THREAD_COUNTS { |
| 65 | + let strings = make_strings(thread_count); |
| 66 | + let total_strings = thread_count * STRINGS_PER_THREAD; |
| 67 | + group.throughput(Throughput::Elements(total_strings as u64)); |
| 68 | + group.bench_with_input( |
| 69 | + BenchmarkId::new("threads", thread_count), |
| 70 | + &strings, |
| 71 | + |b, strings| { |
| 72 | + b.iter_batched( |
| 73 | + || strings, |
| 74 | + |strings| black_box(insert_dictionary_strings_concurrently(strings)), |
| 75 | + BatchSize::LargeInput, |
| 76 | + ); |
| 77 | + }, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + group.finish(); |
| 82 | +} |
| 83 | + |
| 84 | +criterion_group!(benches, bench_profiles_dictionary); |
0 commit comments