-
Notifications
You must be signed in to change notification settings - Fork 467
/
Copy pathlog.rs
333 lines (294 loc) · 12.7 KB
/
log.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//! run with `$ cargo bench --bench log -- --exact <test_name>` to run specific test for logs
//! So to run test named "full-log-with-attributes/with-context" you would run `$ cargo bench --bench log -- --exact full-log-with-attributes/with-context`
//! To run all tests for logs you would run `$ cargo bench --bench log`
//!
/*
The benchmark results:
criterion = "0.5.1"
OS: Ubuntu 22.04.3 LTS (5.15.146.1-microsoft-standard-WSL2)
Hardware: AMD EPYC 7763 64-Core Processor - 2.44 GHz, 16vCPUs,
RAM: 64.0 GB
| Test | Average time|
|--------------------------------|-------------|
| Logger_Creation | 30 ns |
| LoggerProvider_Creation | 909 ns |
| Logging_Comparable_To_Appender | 87 ns |
*/
use std::collections::HashMap;
use std::time::SystemTime;
use criterion::{criterion_group, criterion_main, Criterion};
use opentelemetry::logs::{AnyValue, LogRecord as _, Logger as _, LoggerProvider as _, Severity};
use opentelemetry::trace::Tracer;
use opentelemetry::trace::TracerProvider as _;
use opentelemetry::{InstrumentationScope, Key};
use opentelemetry_sdk::logs::{LogProcessor, LogRecord, LogResult, Logger, LoggerProvider};
use opentelemetry_sdk::trace::{Sampler, TracerProvider};
#[derive(Debug)]
struct NoopProcessor;
impl LogProcessor for NoopProcessor {
fn emit(&self, _data: &mut LogRecord, _scope: &InstrumentationScope) {}
fn force_flush(&self) -> LogResult<()> {
Ok(())
}
fn shutdown(&self) -> LogResult<()> {
Ok(())
}
}
fn log_benchmark_group<F: Fn(&Logger)>(c: &mut Criterion, name: &str, f: F) {
let mut group = c.benchmark_group(name);
group.bench_function("no-context", |b| {
let provider = LoggerProvider::builder()
.with_log_processor(NoopProcessor {})
.build();
let logger = provider.logger("no-context");
b.iter(|| f(&logger));
});
group.bench_function("with-context", |b| {
let provider = LoggerProvider::builder()
.with_log_processor(NoopProcessor {})
.build();
let logger = provider.logger("with-context");
// setup tracing as well.
let tracer_provider = TracerProvider::builder()
.with_sampler(Sampler::AlwaysOn)
.build();
let tracer = tracer_provider.tracer("bench-tracer");
// Act
tracer.in_span("bench-span", |_cx| {
b.iter(|| f(&logger));
});
});
group.finish();
}
fn log_provider_creation(c: &mut Criterion) {
c.bench_function("LoggerProvider_Creation", |b| {
b.iter(|| {
let _provider = LoggerProvider::builder()
.with_log_processor(NoopProcessor {})
.build();
});
});
}
fn logger_creation(c: &mut Criterion) {
// Provider is created once, outside of the benchmark
let provider = LoggerProvider::builder()
.with_log_processor(NoopProcessor {})
.build();
c.bench_function("Logger_Creation", |b| {
b.iter(|| {
let _logger = provider.logger("benchmark");
});
});
}
fn logging_comparable_to_appender(c: &mut Criterion) {
let provider = LoggerProvider::builder()
.with_log_processor(NoopProcessor {})
.build();
let logger = provider.logger("benchmark");
// This mimics the logic from opentelemetry-tracing-appender closely, but
// without the overhead of the tracing layer itself.
c.bench_function("Logging_Comparable_To_Appender", |b| {
b.iter(|| {
let mut log_record = logger.create_log_record();
let now = SystemTime::now();
log_record.set_observed_timestamp(now);
log_record.set_target("my-target".to_string());
log_record.set_event_name("CheckoutFailed");
log_record.set_severity_number(Severity::Warn);
log_record.set_severity_text("WARN");
log_record.add_attribute("book_id", "12345");
log_record.add_attribute("book_title", "Rust Programming Adventures");
log_record.add_attribute("message", "Unable to process checkout.");
logger.emit(log_record);
});
});
}
fn criterion_benchmark(c: &mut Criterion) {
logger_creation(c);
log_provider_creation(c);
logging_comparable_to_appender(c);
log_benchmark_group(c, "simple-log", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
logger.emit(log_record);
});
log_benchmark_group(c, "simple-log-with-int", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testint", 2);
logger.emit(log_record);
});
log_benchmark_group(c, "simple-log-with-double", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testdouble", 2.2);
logger.emit(log_record);
});
log_benchmark_group(c, "simple-log-with-string", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("teststring", "test");
logger.emit(log_record);
});
log_benchmark_group(c, "simple-log-with-bool", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testbool", AnyValue::Boolean(true));
logger.emit(log_record);
});
let bytes = AnyValue::Bytes(Box::new(vec![25u8, 30u8, 40u8]));
log_benchmark_group(c, "simple-log-with-bytes", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testbytes", bytes.clone());
logger.emit(log_record);
});
let bytes = AnyValue::Bytes(Box::new(vec![
25u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8,
30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8,
40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8,
30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8,
40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8,
30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8, 30u8, 40u8,
]));
log_benchmark_group(c, "simple-log-with-a-lot-of-bytes", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testbytes", bytes.clone());
logger.emit(log_record);
});
let vec_any_values = AnyValue::ListAny(Box::new(vec![
AnyValue::Int(25),
"test".into(),
true.into(),
]));
log_benchmark_group(c, "simple-log-with-vec-any-value", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testvec", vec_any_values.clone());
logger.emit(log_record);
});
let vec_any_values = AnyValue::ListAny(Box::new(vec![
AnyValue::Int(25),
"test".into(),
true.into(),
]));
let vec_any_values = AnyValue::ListAny(Box::new(vec![
AnyValue::Int(25),
"test".into(),
true.into(),
vec_any_values,
]));
log_benchmark_group(c, "simple-log-with-inner-vec-any-value", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testvec", vec_any_values.clone());
logger.emit(log_record);
});
let map_any_values = AnyValue::Map(Box::new(HashMap::from([
("testint".into(), 2.into()),
("testdouble".into(), 2.2.into()),
("teststring".into(), "test".into()),
])));
log_benchmark_group(c, "simple-log-with-map-any-value", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testmap", map_any_values.clone());
logger.emit(log_record);
});
let map_any_values = AnyValue::Map(Box::new(HashMap::from([
("testint".into(), 2.into()),
("testdouble".into(), 2.2.into()),
("teststring".into(), "test".into()),
])));
let map_any_values = AnyValue::Map(Box::new(HashMap::from([
("testint".into(), 2.into()),
("testdouble".into(), 2.2.into()),
("teststring".into(), "test".into()),
("testmap".into(), map_any_values),
])));
log_benchmark_group(c, "simple-log-with-inner-map-any-value", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("simple log".into());
log_record.add_attribute("testmap", map_any_values.clone());
logger.emit(log_record);
});
log_benchmark_group(c, "long-log", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Gravida in fermentum et sollicitudin ac orci phasellus. Ullamcorper dignissim cras tincidunt lobortis feugiat vivamus at augue. Magna etiam tempor orci eu. Sed tempus urna et pharetra pharetra massa.".into());
logger.emit(log_record);
});
let now = SystemTime::now();
log_benchmark_group(c, "full-log", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("full log".into());
log_record.set_timestamp(now);
log_record.set_observed_timestamp(now);
log_record.set_severity_number(Severity::Warn);
log_record.set_severity_text(Severity::Warn.name());
logger.emit(log_record);
});
log_benchmark_group(c, "full-log-with-4-attributes", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("full log".into());
log_record.set_timestamp(now);
log_record.set_observed_timestamp(now);
log_record.set_severity_number(Severity::Warn);
log_record.set_severity_text(Severity::Warn.name());
log_record.add_attribute("name", "my-event-name");
log_record.add_attribute("event.id", 20);
log_record.add_attribute("user.name", "otel");
log_record.add_attribute("user.email", "otel@opentelemetry.io");
logger.emit(log_record);
});
log_benchmark_group(c, "full-log-with-9-attributes", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("full log".into());
log_record.set_timestamp(now);
log_record.set_observed_timestamp(now);
log_record.set_severity_number(Severity::Warn);
log_record.set_severity_text(Severity::Warn.name());
log_record.add_attribute("name", "my-event-name");
log_record.add_attribute("event.id", 20);
log_record.add_attribute("user.name", "otel");
log_record.add_attribute("user.email", "otel@opentelemetry.io");
log_record.add_attribute("code.filename", "log.rs");
log_record.add_attribute("code.filepath", "opentelemetry_sdk/benches/log.rs");
log_record.add_attribute("code.lineno", 96);
log_record.add_attribute("code.namespace", "opentelemetry_sdk::benches::log");
log_record.add_attribute("log.target", "opentelemetry_sdk::benches::log");
logger.emit(log_record);
});
let attributes: Vec<(Key, AnyValue)> = vec![
("name".into(), "my-event-name".into()),
("event-id".into(), 20.into()),
("user.name".into(), "otel".into()),
("user.email".into(), "otel@opentelemetry.io".into()),
("code.filename".into(), "log.rs".into()),
(
"code.filepath".into(),
"opentelemetry_sdk/benches/log.rs".into(),
),
("code.lineno".into(), 96.into()),
(
"code.namespace".into(),
"opentelemetry_sdk::benches::log".into(),
),
(
"log.target".into(),
"opentelemetry_sdk::benches::log".into(),
),
];
log_benchmark_group(c, "full-log-with-attributes", |logger| {
let mut log_record = logger.create_log_record();
log_record.set_body("full log".into());
log_record.set_timestamp(now);
log_record.set_observed_timestamp(now);
log_record.set_severity_number(Severity::Warn);
log_record.set_severity_text(Severity::Warn.name());
log_record.add_attributes(attributes.clone());
logger.emit(log_record);
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);