diff --git a/lib/vector-core/Cargo.toml b/lib/vector-core/Cargo.toml index 1e29318535234..c6fe7080634b7 100644 --- a/lib/vector-core/Cargo.toml +++ b/lib/vector-core/Cargo.toml @@ -73,3 +73,7 @@ vrl = ["vrl-core", "enrichment"] [[bench]] name = "lookup" harness = false + +[[bench]] +name = "event" +harness = false diff --git a/lib/vector-core/benches/event/log_event.rs b/lib/vector-core/benches/event/log_event.rs new file mode 100644 index 0000000000000..b0c92faeb9d35 --- /dev/null +++ b/lib/vector-core/benches/event/log_event.rs @@ -0,0 +1,61 @@ +use criterion::{ + criterion_group, measurement::WallTime, BatchSize, BenchmarkGroup, Criterion, SamplingMode, +}; +use std::time::Duration; +use vector_core::event::LogEvent; + +fn rename_key_flat(c: &mut Criterion) { + let mut group: BenchmarkGroup = + c.benchmark_group("vector_core::event::log_event::LogEvent::rename_key_flat"); + group.sampling_mode(SamplingMode::Auto); + + group.bench_function("rename_flat_key (key is present)", move |b| { + b.iter_batched( + || { + let mut log_event = LogEvent::default(); + log_event.insert("one", 1); + log_event.insert("two", 2); + log_event.insert("three", 3); + log_event + }, + |mut log_event| { + log_event.rename_key_flat("one", "1"); + }, + BatchSize::SmallInput, + ) + }); + + group.bench_function("rename_flat_key (key is NOT present)", move |b| { + b.iter_batched( + || { + let mut log_event = LogEvent::default(); + log_event.insert("one", 1); + log_event.insert("two", 2); + log_event.insert("three", 3); + log_event + }, + |mut log_event| { + log_event.rename_key_flat("four", "4"); + }, + BatchSize::SmallInput, + ) + }); +} + +criterion_group!( + name = benches; + config = Criterion::default() + .warm_up_time(Duration::from_secs(5)) + .measurement_time(Duration::from_secs(120)) + // degree of noise to ignore in measurements, here 1% + .noise_threshold(0.01) + // likelihood of noise registering as difference, here 5% + .significance_level(0.05) + // likelihood of capturing the true runtime, here 95% + .confidence_level(0.95) + // total number of bootstrap resamples, higher is less noisy but slower + .nresamples(100_000) + // total samples to collect within the set measurement time + .sample_size(150); + targets = rename_key_flat +); diff --git a/lib/vector-core/benches/event/main.rs b/lib/vector-core/benches/event/main.rs new file mode 100644 index 0000000000000..373e7a7545832 --- /dev/null +++ b/lib/vector-core/benches/event/main.rs @@ -0,0 +1,5 @@ +use criterion::criterion_main; + +mod log_event; + +criterion_main!(log_event::benches); diff --git a/lib/vector-core/src/event/log_event.rs b/lib/vector-core/src/event/log_event.rs index 309ee9665f32d..b480fd3e74313 100644 --- a/lib/vector-core/src/event/log_event.rs +++ b/lib/vector-core/src/event/log_event.rs @@ -137,12 +137,17 @@ impl LogEvent { K: AsRef + Into + PartialEq + Display, { if from_key != to_key { - if let Some(val) = self.remove(from_key) { + if let Some(val) = self.fields.as_map_mut().remove(from_key.as_ref()) { self.insert_flat(to_key, val); } } } + /// Insert a key in place without reference to pathing + /// + /// This function will insert a key in place without reference to any + /// pathing information in the key. It will insert over the top of any value + /// that exists in the map already. #[instrument(level = "trace", skip(self, key), fields(key = %key))] pub fn insert_flat(&mut self, key: K, value: V) where