Skip to content

Commit 91f8fd3

Browse files
committed
test(stdlib): Benchmark for del function
1 parent f67b91c commit 91f8fd3

3 files changed

Lines changed: 79 additions & 7 deletions

File tree

benches/stdlib.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use criterion::{Criterion, criterion_group, criterion_main};
44
use regex::Regex;
55

66
use std::{env, path::PathBuf};
7-
use vrl::{bench_function, btreemap, compiler::prelude::*, func_args, value};
7+
use vrl::{
8+
bench_function, bench_query_function, btreemap, compiler::prelude::*, func_args, query, value,
9+
};
810

911
use crate::value::Value;
1012

@@ -31,8 +33,7 @@ criterion_group!(
3133
decode_punycode,
3234
decrypt,
3335
dns_lookup,
34-
// TODO: Cannot pass a Path to bench_function
35-
//del,
36+
del,
3637
decrypt_ip,
3738
downcase,
3839
encode_base16,
@@ -3173,3 +3174,27 @@ bench_function! {
31733174
want: Ok(value!("2001:db8::1")),
31743175
}
31753176
}
3177+
3178+
bench_query_function! {
3179+
del => vrl::stdlib::Del;
3180+
3181+
default {
3182+
args: {
3183+
let mut hashmap = func_args![];
3184+
hashmap.insert("target", query!(".test"));
3185+
hashmap
3186+
},
3187+
event: btreemap! { "test" => true },
3188+
want: Ok(value!(true)),
3189+
}
3190+
3191+
compact {
3192+
args: {
3193+
let mut hashmap = func_args![compact: true];
3194+
hashmap.insert("target", query!(".test"));
3195+
hashmap
3196+
},
3197+
event: btreemap! { "test" => true },
3198+
want: Ok(value!(true)),
3199+
}
3200+
}

src/compiler/function.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ fn required<T>(argument: Option<T>) -> T {
564564
#[cfg(any(test, feature = "test"))]
565565
mod test_impls {
566566
use super::{ArgumentList, HashMap, Span, Value};
567-
use crate::compiler::expression::FunctionArgument;
567+
use crate::compiler::expression::{Expr, FunctionArgument};
568568
use crate::compiler::parser::Node;
569569

570570
impl From<HashMap<&'static str, Value>> for ArgumentList {
@@ -579,6 +579,15 @@ mod test_impls {
579579
}
580580
}
581581

582+
impl From<HashMap<&'static str, Expr>> for ArgumentList {
583+
fn from(map: HashMap<&'static str, Expr>) -> Self {
584+
Self {
585+
arguments: map,
586+
closure: None,
587+
}
588+
}
589+
}
590+
582591
impl From<ArgumentList> for Vec<(&'static str, Option<FunctionArgument>)> {
583592
fn from(args: ArgumentList) -> Self {
584593
args.arguments

src/compiler/test_util.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,25 @@ macro_rules! test_type_def {
1818
};
1919
}
2020

21+
#[macro_export]
22+
macro_rules! query {
23+
($query:expr_2021) => {
24+
$crate::compiler::expression::Expr::Query($crate::compiler::expression::Query::new(
25+
$crate::compiler::expression::Target::External($crate::path::PathPrefix::Event),
26+
$crate::path::parse_value_path($query).expect("invalid path"),
27+
))
28+
};
29+
}
30+
2131
#[macro_export]
2232
macro_rules! func_args {
2333
() => (
24-
::std::collections::HashMap::<&'static str, $crate::value::Value>::default()
34+
::std::collections::HashMap::<&'static str, $crate::compiler::expression::Expr>::default()
2535
);
2636
($($k:tt: $v:expr_2021),+ $(,)?) => {
27-
vec![$((stringify!($k), $v.into())),+]
37+
[$((stringify!($k), $crate::value::Value::from($v).into())),+]
2838
.into_iter()
29-
.collect::<::std::collections::HashMap<&'static str, $crate::value::Value>>()
39+
.collect::<::std::collections::HashMap<&'static str, $crate::compiler::expression::Expr>>()
3040
};
3141
}
3242

@@ -58,6 +68,34 @@ macro_rules! bench_function {
5868
};
5969
}
6070

71+
#[macro_export]
72+
macro_rules! bench_query_function {
73+
($name:tt => $func:path; $($case:ident { args: $args:expr_2021, event: $event:expr_2021, want: $(Ok($ok:expr_2021))? $(Err($err:expr_2021))? $(,)* })+) => {
74+
fn $name(c: &mut criterion::Criterion) {
75+
let mut group = c.benchmark_group(&format!("vrl_stdlib/functions/{}", stringify!($name)));
76+
group.throughput(criterion::Throughput::Elements(1));
77+
$(
78+
group.bench_function(&stringify!($case).to_string(), |b| {
79+
let mut state = $crate::compiler::state::TypeState::default();
80+
81+
let (expression, want) = $crate::__prep_bench_or_test!($func, &state, $args, $(Ok($crate::value::Value::from($ok)))? $(Err($err.to_owned()))?);
82+
let expression = expression.unwrap();
83+
let mut runtime_state = $crate::compiler::state::RuntimeState::default();
84+
let mut target: $crate::value::Value = $event.into();
85+
let tz = $crate::compiler::TimeZone::Named(chrono_tz::Tz::UTC);
86+
let mut ctx = $crate::compiler::Context::new(&mut target, &mut runtime_state, &tz);
87+
88+
b.iter(|| {
89+
let got = expression.resolve(&mut ctx).map_err(|e| e.to_string());
90+
debug_assert_eq!(got, want);
91+
got
92+
})
93+
});
94+
)+
95+
}
96+
};
97+
}
98+
6199
#[macro_export]
62100
macro_rules! test_function {
63101

0 commit comments

Comments
 (0)