Skip to content

Commit da17eba

Browse files
hooliohgleocadie
authored andcommitted
Add benchmarking feature in order to re-export pub(crate) function to benches.
1 parent 7499140 commit da17eba

File tree

8 files changed

+47
-128
lines changed

8 files changed

+47
-128
lines changed

benchmark/run_benchmarks_ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pushd "${PROJECT_DIR}" > /dev/null
2222

2323
# Run benchmarks
2424
message "Running benchmarks"
25-
cargo bench --workspace -- --warm-up-time 1 --measurement-time 5 --sample-size=200
25+
cargo bench --workspace --features datadog-crashtracker/benchmarking -- --warm-up-time 1 --measurement-time 5 --sample-size=200
2626
message "Finished running benchmarks"
2727

2828
# Copy the benchmark results to the output directory

datadog-crashtracker/Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ edition.workspace = true
66
version.workspace = true
77
rust-version.workspace = true
88
license.workspace = true
9+
autobenches = false
910

1011
[lib]
1112
crate-type = ["lib"]
12-
bench = true
13+
bench = false
1314

1415
[[bin]]
1516
name = "crashtracker-receiver"
1617
path = "src/bin/crashtracker_receiver.rs"
1718
bench = false
1819

1920
[[bench]]
20-
name = "receiver_bench"
21+
name = "main"
2122
harness = false
23+
path = "benches/main.rs"
2224

2325
[features]
2426
default = ["collector", "receiver", "collector_windows"]
@@ -64,7 +66,7 @@ thiserror = "1.0"
6466
windows = { version = "0.59.0", features = ["Win32_System_Diagnostics_Debug", "Win32_System_Diagnostics_ToolHelp", "Win32_System_ErrorReporting", "Win32_System_Kernel", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_SystemServices", "Win32_System_Threading", "Win32_Security"] }
6567

6668
[dev-dependencies]
67-
criterion = { version = "0.5.1", features = ["html_reports"] }
69+
criterion = { version = "0.5.1" }
6870
goblin = "0.9.3"
6971
tempfile = { version = "3.3" }
7072

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#[cfg(feature = "benchmarking")]
5-
use criterion::criterion_main;
4+
use criterion::{criterion_main, criterion_group, Criterion};
65

76
#[cfg(feature = "benchmarking")]
87
mod receiver_bench;
98

109
#[cfg(feature = "benchmarking")]
11-
use datadog_crashtracker::receiver_entry_point_bench as receiver_entry_point;
10+
fn active_benches(c: &mut Criterion) {
11+
receiver_bench::benches();
12+
}
1213

13-
#[cfg(feature = "benchmarking")]
14-
criterion_main!(receiver_bench::benches);
14+
#[cfg(not(feature = "benchmarking"))]
15+
fn active_benches(_: &mut Criterion) {
16+
println!(
17+
"Benchmarks are disabled. Enable with `--features datadog-crashtracker/benchmarking`."
18+
);
19+
}
20+
criterion_group!(benches, active_benches);
21+
criterion_main!(benches);

datadog-crashtracker/benches/receiver_bench.rs

Lines changed: 11 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,138 +2,44 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use criterion::{black_box, criterion_group, BenchmarkId, Criterion, Throughput};
5-
#[cfg(feature = "benchmarking")]
6-
use datadog_crashtracker::receiver_entry_point_bench;
7-
use datadog_crashtracker::{CrashtrackerConfiguration, CrashtrackerReceiverConfig};
5+
use datadog_crashtracker::benchmark::receiver_entry_point;
86
use std::time::Duration;
97
use tokio::io::BufReader;
108

11-
fn create_mock_crash_report() -> String {
12-
r#"DD_CRASHTRACK_BEGIN_CONFIG
13-
{"resolve_frames": "Disabled","demangle_names": true,"path_replacements": [], "additional_files": []}
14-
DD_CRASHTRACK_END_CONFIG
15-
DD_CRASHTRACK_BEGIN_PROC_INFO
16-
{"pid": 12345, "signal": 6}
17-
DD_CRASHTRACK_END_PROC_INFO
18-
DD_CRASHTRACK_BEGIN_SIG_INFO
19-
{"sig": 6, "errno": 0, "addr": "0x0"}
20-
DD_CRASHTRACK_END_SIG_INFO
21-
DD_CRASHTRACK_BEGIN_METADATA
22-
["log1", "log2", "log3"]
23-
DD_CRASHTRACK_END_METADATA
24-
DD_CRASHTRACK_BEGIN_STACKTRACE
25-
[{"ip": "0x7f8b8c0d1234", "sp": "0x7f8b8c0d5678", "symbol_address": "0x7f8b8c0d0000", "module_base_address": "0x7f8b8c0d0000", "module": "test_module"}]
26-
DD_CRASHTRACK_END_STACKTRACE
27-
DD_CRASHTRACK_DONE"#.to_string()
28-
}
299

30-
fn create_mock_simple_crash_report() -> String {
10+
fn create_dummy_crash_report() -> String {
3111
r#"DD_CRASHTRACK_BEGIN_STACKTRACE
3212
{ "ip": "0x42", "module_address": "0x21", "sp": "0x11", "symbol_address": "0x73" }
3313
DD_CRASHTRACK_END_STACKTRACE
3414
DD_CRASHTRACK_DONE"#.to_string()
3515
}
3616

37-
fn create_empty_report() -> String {
38-
"DD_CRASHTRACK_BEGIN_CONFIG\nDD_CRASHTRACK_FINISHED\n".to_string()
39-
}
40-
41-
async fn bench_receiver_entry_point_valid(data: &str) {
17+
async fn bench_receiver_entry_point_from_str(data: &str) {
4218
let cursor = std::io::Cursor::new(data.as_bytes());
4319
let reader = BufReader::new(cursor);
4420
let timeout = Duration::from_millis(5000);
4521

46-
let _ = receiver_entry_point_bench(timeout, reader).await;
22+
let _ = receiver_entry_point(timeout, reader).await;
4723
}
4824

49-
async fn bench_receiver_entry_point_invalid(data: &str) {
50-
let cursor = std::io::Cursor::new(data.as_bytes());
51-
let reader = BufReader::new(cursor);
52-
let timeout = Duration::from_millis(5000);
53-
54-
let _ = receiver_entry_point_bench(timeout, reader).await;
55-
}
56-
57-
fn receiver_entry_point_benchmarks(c: &mut Criterion) {
25+
pub fn receiver_entry_point_benchmarks(c: &mut Criterion) {
5826
let mut group = c.benchmark_group("receiver_entry_point");
5927

60-
let valid_report = create_mock_crash_report();
61-
group.throughput(Throughput::Bytes(valid_report.len() as u64));
28+
let report = create_dummy_crash_report();
29+
group.throughput(Throughput::Bytes(report.len() as u64));
6230
group.bench_with_input(
63-
BenchmarkId::new("valid_report", valid_report.len()),
64-
&valid_report,
31+
BenchmarkId::new("report", report.len()),
32+
&report,
6533
|b, data| {
6634
b.iter(|| {
6735
let rt = tokio::runtime::Builder::new_current_thread()
6836
.enable_all()
6937
.build()
7038
.unwrap();
71-
rt.block_on(bench_receiver_entry_point_valid(black_box(data)))
39+
rt.block_on(bench_receiver_entry_point_from_str(black_box(data)))
7240
});
7341
},
7442
);
75-
76-
group.finish();
77-
}
78-
79-
fn receiver_entry_point_memory_benchmarks(c: &mut Criterion) {
80-
let mut group = c.benchmark_group("receiver_entry_point_memory");
81-
82-
// Benchmark with varying sizes of crash reports
83-
for size_multiplier in [1, 10, 100].iter() {
84-
let mut large_report = create_mock_crash_report();
85-
86-
// Create a larger stacktrace section
87-
let mut large_stacktrace = String::new();
88-
large_stacktrace.push_str("DD_CRASHTRACK_BEGIN_STACKTRACE\n");
89-
large_stacktrace.push('[');
90-
91-
for i in 0..*size_multiplier {
92-
if i > 0 {
93-
large_stacktrace.push(',');
94-
}
95-
large_stacktrace.push_str(&format!(
96-
r#"{{"ip": "0x7f8b8c0d{:04x}", "sp": "0x7f8b8c0d{:04x}", "symbol_address": "0x7f8b8c0d0000", "module_base_address": "0x7f8b8c0d0000", "module": "test_module_{}"}}"#,
97-
i, i + 1000, i
98-
));
99-
}
100-
101-
large_stacktrace.push_str("]\nDD_CRASHTRACK_END_STACKTRACE\nDD_CRASHTRACK_FINISHED\n");
102-
103-
// Replace the stacktrace section in the report
104-
let begin_idx = large_report.find("DD_CRASHTRACK_BEGIN_STACKTRACE").unwrap();
105-
let end_idx = large_report.find("DD_CRASHTRACK_FINISHED").unwrap() + "DD_CRASHTRACK_FINISHED".len();
106-
large_report.replace_range(begin_idx..end_idx, &large_stacktrace);
107-
108-
group.throughput(Throughput::Bytes(large_report.len() as u64));
109-
group.bench_with_input(
110-
BenchmarkId::new("large_report", size_multiplier),
111-
&large_report,
112-
|b, data| {
113-
b.iter(|| {
114-
let rt = tokio::runtime::Builder::new_current_thread()
115-
.enable_all()
116-
.build()
117-
.unwrap();
118-
rt.block_on(async {
119-
let cursor = std::io::Cursor::new(data.as_bytes());
120-
let reader = BufReader::new(cursor);
121-
let timeout = Duration::from_millis(5000);
122-
123-
let _ = receiver_entry_point_bench(black_box(timeout), black_box(reader)).await;
124-
})
125-
});
126-
},
127-
);
128-
}
129-
130-
group.finish();
13143
}
13244

133-
criterion_group!(
134-
benches,
135-
receiver_entry_point_benchmarks,
136-
// receiver_entry_point_memory_benchmarks
137-
);
138-
139-
criterion::criterion_main!(benches);
45+
criterion_group!(benches, receiver_entry_point_benchmarks);

datadog-crashtracker/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ pub use shared::configuration::{
8585
};
8686

8787
#[cfg(all(unix, feature = "benchmarking"))]
88-
pub use receiver::benchmark::receiver_entry_point_bench;
88+
pub use receiver::benchmark;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
3+
4+
use std::time::Duration;
5+
use tokio::{
6+
io::{AsyncBufReadExt},
7+
};
8+
9+
pub async fn receiver_entry_point(
10+
timeout: Duration,
11+
stream: impl AsyncBufReadExt + std::marker::Unpin,
12+
) -> anyhow::Result<()> {
13+
crate::receiver::entry_points::receiver_entry_point(timeout, stream).await
14+
}

datadog-crashtracker/src/receiver/entry_points.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
21
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
33

44
use super::receive_report::receive_report_from_stream;
55
use crate::{crash_info::CrashInfo, CrashtrackerConfiguration, StacktraceCollection};
@@ -85,14 +85,6 @@ pub fn get_receiver_unix_socket(socket_path: impl AsRef<str>) -> anyhow::Result<
8585
unix_listener.context("Could not create the unix socket")
8686
}
8787

88-
#[cfg(feature = "benchmarking")]
89-
pub async fn receiver_entry_point_bench(
90-
timeout: Duration,
91-
stream: impl AsyncBufReadExt + std::marker::Unpin,
92-
) -> anyhow::Result<()> {
93-
receiver_entry_point(timeout, stream).await
94-
}
95-
9688
/// Receives data from a crash collector via a stream, formats it into
9789
/// `CrashInfo` json, and emits it to the endpoint/file defined in `config`.
9890
///
@@ -102,7 +94,7 @@ pub async fn receiver_entry_point_bench(
10294
///
10395
/// See comments in [datadog-crashtracker/lib.rs] for a full architecture
10496
/// description.
105-
async fn receiver_entry_point(
97+
pub(crate) async fn receiver_entry_point(
10698
timeout: Duration,
10799
stream: impl AsyncBufReadExt + std::marker::Unpin,
108100
) -> anyhow::Result<()> {

datadog-crashtracker/src/receiver/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ pub use entry_points::{
1010
mod receive_report;
1111

1212
#[cfg(feature = "benchmarking")]
13-
pub mod benchmark {
14-
pub use super::entry_points::receiver_entry_point_bench;
15-
}
13+
pub mod benchmark;
1614

1715
#[cfg(test)]
1816
mod tests {

0 commit comments

Comments
 (0)