|
| 1 | +//! Writing and reading the telemetry artifacts, on the spans and logs of a telemetry-heavy benchmark: |
| 2 | +//! about 10k spans and 10k logs, from nested `tracing` spans with attributes and an event in each. |
| 3 | +
|
| 4 | +use std::io::Cursor; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +use divan::Bencher; |
| 8 | +use divan::counter::{BytesCount, ItemsCount}; |
| 9 | +use runner_shared::telemetry::{LogRow, SpanRow, TelemetryReader, TelemetryRow, TelemetryWriter}; |
| 10 | + |
| 11 | +#[global_allocator] |
| 12 | +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; |
| 13 | + |
| 14 | +fn main() { |
| 15 | + divan::main(); |
| 16 | +} |
| 17 | + |
| 18 | +const SPANS: &[u8] = include_bytes!("../testdata/telemetry_volume_spans.msgpack.zst"); |
| 19 | +const LOGS: &[u8] = include_bytes!("../testdata/telemetry_volume_logs.msgpack.zst"); |
| 20 | + |
| 21 | +fn read<T: TelemetryRow>(artifact: &[u8]) -> Vec<T> { |
| 22 | + TelemetryReader::<T, _>::new(Cursor::new(artifact)) |
| 23 | + .unwrap() |
| 24 | + .collect::<anyhow::Result<_>>() |
| 25 | + .unwrap() |
| 26 | +} |
| 27 | + |
| 28 | +fn bench_read<T: TelemetryRow>(bencher: Bencher, artifact: &[u8]) { |
| 29 | + let rows = read::<T>(artifact).len(); |
| 30 | + bencher |
| 31 | + .counter(ItemsCount::new(rows)) |
| 32 | + .counter(BytesCount::new(artifact.len())) |
| 33 | + .bench_local(|| read::<T>(artifact).len()); |
| 34 | +} |
| 35 | + |
| 36 | +fn bench_write<T: TelemetryRow + Clone>(bencher: Bencher, artifact: &[u8]) { |
| 37 | + let rows = read::<T>(artifact); |
| 38 | + let dir = tempfile::tempdir_in(env!("CARGO_TARGET_TMPDIR")).unwrap(); |
| 39 | + let path = dir.path().join("telemetry.msgpack.zst"); |
| 40 | + let write = |rows: Vec<T>, path: &Path| { |
| 41 | + let mut writer = TelemetryWriter::<T>::create(path).unwrap(); |
| 42 | + writer.write(rows).unwrap(); |
| 43 | + writer.close().unwrap(); |
| 44 | + }; |
| 45 | + bencher |
| 46 | + .counter(ItemsCount::new(rows.len())) |
| 47 | + .with_inputs(|| rows.clone()) |
| 48 | + .bench_local_values(|rows| write(rows, &path)); |
| 49 | +} |
| 50 | + |
| 51 | +#[divan::bench] |
| 52 | +fn read_spans(bencher: Bencher) { |
| 53 | + bench_read::<SpanRow>(bencher, SPANS); |
| 54 | +} |
| 55 | + |
| 56 | +#[divan::bench] |
| 57 | +fn read_logs(bencher: Bencher) { |
| 58 | + bench_read::<LogRow>(bencher, LOGS); |
| 59 | +} |
| 60 | + |
| 61 | +#[divan::bench] |
| 62 | +fn write_spans(bencher: Bencher) { |
| 63 | + bench_write::<SpanRow>(bencher, SPANS); |
| 64 | +} |
| 65 | + |
| 66 | +#[divan::bench] |
| 67 | +fn write_logs(bencher: Bencher) { |
| 68 | + bench_write::<LogRow>(bencher, LOGS); |
| 69 | +} |
0 commit comments