forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.rs
173 lines (148 loc) · 5.47 KB
/
lua.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
use std::pin::Pin;
use criterion::{criterion_group, BatchSize, Criterion, Throughput};
use futures::{stream, SinkExt, Stream, StreamExt};
use indoc::indoc;
use transforms::lua::v2::LuaConfig;
use vector::{
event::{Event, LogEvent},
test_util::collect_ready,
transforms::{self, OutputBuffer, Transform},
};
use vrl::event_path;
fn bench_add_fields(c: &mut Criterion) {
let event = Event::from(LogEvent::default());
let key = "the_key";
let value = "this is the value";
let mut group = c.benchmark_group("lua/add_fields");
group.throughput(Throughput::Elements(1));
let benchmarks: Vec<(&str, Transform)> = vec![
("v1", {
let source = format!("event['{}'] = '{}'", key, value);
Transform::event_task(transforms::lua::v1::Lua::new(source, vec![]).unwrap())
}),
("v2", {
let config = format!(
indoc! {r#"
hooks.process = """
function (event, emit)
event.log['{}'] = '{}'
emit(event)
end
"""
"#},
key, value
);
Transform::event_task(
transforms::lua::v2::Lua::new(&toml::from_str::<LuaConfig>(&config).unwrap())
.unwrap(),
)
}),
];
for (name, transform) in benchmarks {
let (tx, rx) = futures::channel::mpsc::channel::<Event>(1);
let mut rx: Pin<Box<dyn Stream<Item = Event> + Send>> = match transform {
Transform::Function(t) => {
let mut t = t.clone();
Box::pin(rx.flat_map(move |v| {
let mut buf = OutputBuffer::with_capacity(1);
t.transform(&mut buf, v);
stream::iter(buf.into_events())
}))
}
Transform::Synchronous(_t) => {
unreachable!("no sync transform used in these benches");
}
Transform::Task(t) => t.transform_events(Box::pin(rx)),
};
group.bench_function(name.to_owned(), |b| {
b.iter_batched(
|| (tx.clone(), event.clone()),
|(mut tx, event)| {
futures::executor::block_on(tx.send(event)).unwrap();
let transformed = futures::executor::block_on(rx.next()).unwrap();
debug_assert_eq!(transformed.as_log()[key], value.to_owned().into());
transformed
},
BatchSize::SmallInput,
)
});
}
group.finish();
}
fn bench_field_filter(c: &mut Criterion) {
let num_events = 10;
let events = (0..num_events)
.map(|i| {
let mut event = LogEvent::default();
event.insert(event_path!("the_field"), (i % 10).to_string());
Event::from(event)
})
.collect::<Vec<_>>();
let mut group = c.benchmark_group("lua/field_filter");
group.throughput(Throughput::Elements(num_events));
let benchmarks: Vec<(&str, Transform)> = vec![
("v1", {
let source = String::from(indoc! {r#"
if event["the_field"] ~= "0" then
event = nil
end
"#});
Transform::event_task(transforms::lua::v1::Lua::new(source, vec![]).unwrap())
}),
("v2", {
let config = indoc! {r#"
hooks.process = """
function (event, emit)
if event.log["the_field"] ~= "0" then
event = nil
end
emit(event)
end
"""
"#};
Transform::event_task(
transforms::lua::v2::Lua::new(&toml::from_str(config).unwrap()).unwrap(),
)
}),
];
for (name, transform) in benchmarks {
let (tx, rx) = futures::channel::mpsc::channel::<Event>(num_events as usize);
let mut rx: Pin<Box<dyn Stream<Item = Event> + Send>> = match transform {
Transform::Function(t) => {
let mut t = t.clone();
Box::pin(rx.flat_map(move |v| {
let mut buf = OutputBuffer::with_capacity(1);
t.transform(&mut buf, v);
stream::iter(buf.into_events())
}))
}
Transform::Synchronous(_t) => {
unreachable!("no sync transform used in these benches");
}
Transform::Task(t) => t.transform_events(Box::pin(rx)),
};
group.bench_function(name.to_owned(), |b| {
b.iter_batched(
|| (tx.clone(), events.clone()),
|(mut tx, events)| {
let _ =
futures::executor::block_on(tx.send_all(&mut stream::iter(events).map(Ok)))
.unwrap();
let output = futures::executor::block_on(collect_ready(&mut rx));
let num = output.len();
debug_assert_eq!(num as u64, num_events / 10);
num
},
BatchSize::SmallInput,
)
});
}
group.finish();
}
criterion_group!(
name = benches;
// encapsulates CI noise we saw in
// https://github.com/vectordotdev/vector/issues/5394
config = Criterion::default().noise_threshold(0.05);
targets = bench_add_fields, bench_field_filter
);