Skip to content

Commit f563588

Browse files
Copilotcijothomas
andcommitted
Refactor benchmarks to use Criterion groups and eliminate code duplication
Co-authored-by: cijothomas <[email protected]>
1 parent 06fb3cb commit f563588

File tree

1 file changed

+18
-30
lines changed
  • opentelemetry-user-events-logs/benches

1 file changed

+18
-30
lines changed

opentelemetry-user-events-logs/benches/logs.rs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
// When no listener (automatically set by benchmark)
1010
| Test | Average time |
1111
|-----------------------------------|--------------|
12-
| User_Event_4_Attributes_Disabled | X ns |
13-
| User_Event_6_Attributes_Disabled | X ns |
12+
| User_Events/4_Attributes_Disabled | X ns |
13+
| User_Events/6_Attributes_Disabled | X ns |
1414
1515
// When listener is enabled (automatically set by benchmark)
1616
| Test | Average time |
1717
|-----------------------------------|--------------|
18-
| User_Event_4_Attributes_Enabled | X ns |
19-
| User_Event_6_Attributes_Enabled | X ns |
18+
| User_Events/4_Attributes_Enabled | X ns |
19+
| User_Events/6_Attributes_Enabled | X ns |
2020
2121
Note: The benchmark automatically enables and disables the user-events listener
2222
to compare performance between the two states.
@@ -82,15 +82,17 @@ fn setup_provider() -> SdkLoggerProvider {
8282
.build()
8383
}
8484

85-
fn benchmark_4_attributes(c: &mut Criterion) {
85+
fn benchmark_user_events(c: &mut Criterion) {
86+
let mut group = c.benchmark_group("User_Events");
87+
8688
let provider = setup_provider();
8789
let ot_layer = tracing_layer::OpenTelemetryTracingBridge::new(&provider);
8890
let subscriber = Registry::default().with(ot_layer);
8991

9092
tracing::subscriber::with_default(subscriber, || {
91-
// Test with listener disabled
93+
// Test 4 attributes with listener disabled
9294
set_user_events_listener(false);
93-
c.bench_function("User_Event_4_Attributes_Disabled", |b| {
95+
group.bench_function("4_Attributes_Disabled", |b| {
9496
b.iter(|| {
9597
error!(
9698
name : "CheckoutFailed",
@@ -103,9 +105,9 @@ fn benchmark_4_attributes(c: &mut Criterion) {
103105
});
104106
});
105107

106-
// Test with listener enabled
108+
// Test 4 attributes with listener enabled
107109
set_user_events_listener(true);
108-
c.bench_function("User_Event_4_Attributes_Enabled", |b| {
110+
group.bench_function("4_Attributes_Enabled", |b| {
109111
b.iter(|| {
110112
error!(
111113
name : "CheckoutFailed",
@@ -118,20 +120,9 @@ fn benchmark_4_attributes(c: &mut Criterion) {
118120
});
119121
});
120122

121-
// Cleanup: disable listener
122-
set_user_events_listener(false);
123-
});
124-
}
125-
126-
fn benchmark_6_attributes(c: &mut Criterion) {
127-
let provider = setup_provider();
128-
let ot_layer = tracing_layer::OpenTelemetryTracingBridge::new(&provider);
129-
let subscriber = Registry::default().with(ot_layer);
130-
131-
tracing::subscriber::with_default(subscriber, || {
132-
// Test with listener disabled
123+
// Test 6 attributes with listener disabled
133124
set_user_events_listener(false);
134-
c.bench_function("User_Event_6_Attributes_Disabled", |b| {
125+
group.bench_function("6_Attributes_Disabled", |b| {
135126
b.iter(|| {
136127
error!(
137128
name : "CheckoutFailed",
@@ -146,9 +137,9 @@ fn benchmark_6_attributes(c: &mut Criterion) {
146137
});
147138
});
148139

149-
// Test with listener enabled
140+
// Test 6 attributes with listener enabled
150141
set_user_events_listener(true);
151-
c.bench_function("User_Event_6_Attributes_Enabled", |b| {
142+
group.bench_function("6_Attributes_Enabled", |b| {
152143
b.iter(|| {
153144
error!(
154145
name : "CheckoutFailed",
@@ -166,16 +157,13 @@ fn benchmark_6_attributes(c: &mut Criterion) {
166157
// Cleanup: disable listener
167158
set_user_events_listener(false);
168159
});
169-
}
170-
171-
fn criterion_benchmark(c: &mut Criterion) {
172-
benchmark_4_attributes(c);
173-
benchmark_6_attributes(c);
160+
161+
group.finish();
174162
}
175163

176164
criterion_group! {
177165
name = benches;
178166
config = Criterion::default();
179-
targets = criterion_benchmark
167+
targets = benchmark_user_events
180168
}
181169
criterion_main!(benches);

0 commit comments

Comments
 (0)