From 9e248e0596f35ec6667e9d9a8b63bd4d28226ee2 Mon Sep 17 00:00:00 2001 From: vianney Date: Wed, 28 Aug 2024 10:58:25 +0200 Subject: [PATCH 1/3] Add benchmark for concentrator --- Cargo.lock | 1 + data-pipeline/Cargo.toml | 6 ++ data-pipeline/benches/main.rs | 7 ++ data-pipeline/benches/span_concentrator.rs | 71 +++++++++++++++++++ data-pipeline/src/lib.rs | 2 +- .../src/span_concentrator/aggregation.rs | 4 +- data-pipeline/src/span_concentrator/mod.rs | 2 +- 7 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 data-pipeline/benches/main.rs create mode 100644 data-pipeline/benches/span_concentrator.rs diff --git a/Cargo.lock b/Cargo.lock index e6979d4d4c..83cdcc4247 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1323,6 +1323,7 @@ version = "12.0.0" dependencies = [ "anyhow", "bytes", + "criterion", "datadog-ddsketch", "datadog-trace-normalization", "datadog-trace-protobuf", diff --git a/data-pipeline/Cargo.toml b/data-pipeline/Cargo.toml index 65cdc9a1f3..ddb8e039dd 100644 --- a/data-pipeline/Cargo.toml +++ b/data-pipeline/Cargo.toml @@ -26,5 +26,11 @@ datadog-ddsketch = { path = "../ddsketch"} [lib] bench = false +[[bench]] +name = "main" +harness = false +path = "benches/main.rs" + [dev-dependencies] +criterion = "0.5.1" rand = "0.8.5" diff --git a/data-pipeline/benches/main.rs b/data-pipeline/benches/main.rs new file mode 100644 index 0000000000..687cdc4104 --- /dev/null +++ b/data-pipeline/benches/main.rs @@ -0,0 +1,7 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 +use criterion::criterion_main; + +mod span_concentrator; + +criterion_main!(span_concentrator::benches); diff --git a/data-pipeline/benches/span_concentrator.rs b/data-pipeline/benches/span_concentrator.rs new file mode 100644 index 0000000000..bde55f46fa --- /dev/null +++ b/data-pipeline/benches/span_concentrator.rs @@ -0,0 +1,71 @@ +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 +use std::{ + collections::HashMap, + time::{self, Duration, SystemTime}, +}; + +use criterion::{criterion_group, Criterion}; +use data_pipeline::span_concentrator::SpanConcentrator; +use datadog_trace_protobuf::pb; + +fn get_bucket_start(now: SystemTime, n: u64) -> i64 { + let start = now.duration_since(time::UNIX_EPOCH).unwrap() + Duration::from_secs(10 * n); + start.as_nanos() as i64 +} + +fn get_span(now: SystemTime, trace_id: u64, span_id: u64) -> pb::Span { + let mut metrics = HashMap::from([("_dd.measured".to_string(), 1.0)]); + if span_id == 1 { + metrics.insert("_dd.top_level".to_string(), 1.0); + } + let mut meta = HashMap::from([("db_name".to_string(), "postgres".to_string())]); + if span_id % 3 == 0 { + meta.insert("bucket_s3".to_string(), "aws_bucket".to_string()); + } + pb::Span { + trace_id, + span_id, + service: "test-service".to_string(), + name: "test-name".to_string(), + resource: format!("test-{trace_id}"), + error: (span_id % 2) as i32, + metrics, + meta, + parent_id: span_id - 1, + start: get_bucket_start(now, trace_id), + duration: span_id as i64 % Duration::from_secs(10).as_nanos() as i64, + ..Default::default() + } +} + +pub fn add_span_to_concentrator(c: &mut Criterion) { + let now = SystemTime::now() - Duration::from_secs(10 * 100); + let concentrator = SpanConcentrator::new( + Duration::from_secs(10), + now, + true, + true, + vec!["db_name".to_string(), "bucket_s3".to_string()], + ); + let mut spans = vec![]; + for trace_id in 1..100 { + for span_id in 1..100 { + spans.push(get_span(now, trace_id, span_id)); + } + } + c.bench_function("benching adding span to the SpanConcentrator", |b| { + b.iter_batched_ref( + || (concentrator.clone(), spans.clone()), + |data| { + let concentrator = &mut data.0; + let spans = &data.1; + for span in spans { + concentrator.add_span(span); + } + }, + criterion::BatchSize::LargeInput, + ); + }); +} +criterion_group!(benches, add_span_to_concentrator); diff --git a/data-pipeline/src/lib.rs b/data-pipeline/src/lib.rs index 8640f68887..50daf7f418 100644 --- a/data-pipeline/src/lib.rs +++ b/data-pipeline/src/lib.rs @@ -1,5 +1,5 @@ // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ // SPDX-License-Identifier: Apache-2.0 -mod span_concentrator; +pub mod span_concentrator; pub mod trace_exporter; diff --git a/data-pipeline/src/span_concentrator/aggregation.rs b/data-pipeline/src/span_concentrator/aggregation.rs index d1f0d70473..b117e8066d 100644 --- a/data-pipeline/src/span_concentrator/aggregation.rs +++ b/data-pipeline/src/span_concentrator/aggregation.rs @@ -110,7 +110,7 @@ fn get_peer_tags(span: &pb::Span, peer_tag_keys: &[String]) -> Vec { } /// The stats computed from a group of span with the same AggregationKey -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)] pub(super) struct GroupedStats { hits: u64, errors: u64, @@ -140,7 +140,7 @@ impl GroupedStats { /// A time bucket used for stats aggregation. It stores a map of GroupedStats storing the stats of /// spans aggregated on their AggregationKey. -#[derive(Debug)] +#[derive(Debug, Clone)] pub(super) struct StatsBucket { data: HashMap, start: u64, diff --git a/data-pipeline/src/span_concentrator/mod.rs b/data-pipeline/src/span_concentrator/mod.rs index 0b63f24ee2..e42de140c5 100644 --- a/data-pipeline/src/span_concentrator/mod.rs +++ b/data-pipeline/src/span_concentrator/mod.rs @@ -60,7 +60,7 @@ fn should_ignore_span(span: &pb::Span, compute_stats_by_span_kind: bool) -> bool /// When the SpanConcentrator is flushed it keeps the `buffer_len` most recent buckets and remove /// all older buckets returning their content. When using force flush all buckets are flushed /// regardless of their age. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SpanConcentrator { /// Size of the time buckets used for aggregation in nanos bucket_size: u64, From 2548ff5312f405a3881bd927610fef5902674315 Mon Sep 17 00:00:00 2001 From: vianney Date: Thu, 29 Aug 2024 11:18:08 +0200 Subject: [PATCH 2/3] Disable autobenches --- data-pipeline/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/data-pipeline/Cargo.toml b/data-pipeline/Cargo.toml index ddb8e039dd..556f38618d 100644 --- a/data-pipeline/Cargo.toml +++ b/data-pipeline/Cargo.toml @@ -5,6 +5,7 @@ rust-version.workspace = true edition.workspace = true version.workspace = true license.workspace = true +autobenches = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From e04263ddf9e72d05042ca79a70b7e88339cdc692 Mon Sep 17 00:00:00 2001 From: vianney Date: Wed, 4 Sep 2024 12:35:28 +0200 Subject: [PATCH 3/3] Change bench name --- data-pipeline/benches/main.rs | 4 ++-- .../{span_concentrator.rs => span_concentrator_bench.rs} | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) rename data-pipeline/benches/{span_concentrator.rs => span_concentrator_bench.rs} (91%) diff --git a/data-pipeline/benches/main.rs b/data-pipeline/benches/main.rs index 687cdc4104..f91acce720 100644 --- a/data-pipeline/benches/main.rs +++ b/data-pipeline/benches/main.rs @@ -2,6 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 use criterion::criterion_main; -mod span_concentrator; +mod span_concentrator_bench; -criterion_main!(span_concentrator::benches); +criterion_main!(span_concentrator_bench::benches); diff --git a/data-pipeline/benches/span_concentrator.rs b/data-pipeline/benches/span_concentrator_bench.rs similarity index 91% rename from data-pipeline/benches/span_concentrator.rs rename to data-pipeline/benches/span_concentrator_bench.rs index bde55f46fa..f956bc9ea6 100644 --- a/data-pipeline/benches/span_concentrator.rs +++ b/data-pipeline/benches/span_concentrator_bench.rs @@ -39,7 +39,8 @@ fn get_span(now: SystemTime, trace_id: u64, span_id: u64) -> pb::Span { } } -pub fn add_span_to_concentrator(c: &mut Criterion) { +pub fn criterion_benchmark(c: &mut Criterion) { + let mut group = c.benchmark_group("concentrator"); let now = SystemTime::now() - Duration::from_secs(10 * 100); let concentrator = SpanConcentrator::new( Duration::from_secs(10), @@ -54,7 +55,7 @@ pub fn add_span_to_concentrator(c: &mut Criterion) { spans.push(get_span(now, trace_id, span_id)); } } - c.bench_function("benching adding span to the SpanConcentrator", |b| { + group.bench_function("add_spans_to_concentrator", |b| { b.iter_batched_ref( || (concentrator.clone(), spans.clone()), |data| { @@ -68,4 +69,4 @@ pub fn add_span_to_concentrator(c: &mut Criterion) { ); }); } -criterion_group!(benches, add_span_to_concentrator); +criterion_group!(benches, criterion_benchmark);