forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.rs
176 lines (153 loc) · 5.46 KB
/
batch.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
use std::{convert::Infallible, time::Duration};
use bytes::{BufMut, Bytes, BytesMut};
use criterion::{criterion_group, Criterion, SamplingMode, Throughput};
use futures::{future, stream, SinkExt, StreamExt};
use vector::{
sinks::util::{
batch::{Batch, BatchConfig, BatchError, BatchSettings, BatchSize, PushResult},
BatchSink, Buffer, Compression, EncodedEvent, Merged, Partition, PartitionBatchSink,
SinkBatchSettings,
},
test_util::{random_lines, runtime},
};
fn benchmark_batch(c: &mut Criterion) {
let event_len: usize = 100;
let num_events: usize = 100_000;
let mut group = c.benchmark_group("batch");
group.throughput(Throughput::Bytes((event_len * num_events) as u64));
group.sampling_mode(SamplingMode::Flat);
let cases = [
(Compression::None, 2_000_000),
(Compression::None, 500_000),
(Compression::gzip_default(), 2_000_000),
(Compression::gzip_default(), 500_000),
];
let input: Vec<_> = random_lines(event_len)
.take(num_events)
.map(|s| {
let mut bytes = BytesMut::new();
bytes.put_slice(s.as_bytes());
bytes
})
.collect();
for (compression, batch_size) in cases.iter() {
group.bench_function(format!("partitioned/{}_{}", compression, batch_size), |b| {
b.iter_batched(
|| {
let rt = runtime();
let mut batch = BatchSettings::default();
batch.size.bytes = *batch_size;
batch.size.events = num_events;
let batch_sink = PartitionBatchSink::new(
tower::service_fn(|_| future::ok::<_, Infallible>(())),
PartitionedBuffer::new(batch.size, *compression),
Duration::from_secs(1),
)
.sink_map_err(|error| panic!("{}", error));
(
rt,
stream::iter(input.clone().into_iter().map(|b| InnerBuffer {
inner: b,
key: Bytes::from("key"),
}))
.map(|item| Ok(EncodedEvent::new(item, 0))),
batch_sink,
)
},
|(rt, input, batch_sink)| rt.block_on(input.forward(batch_sink)).unwrap(),
criterion::BatchSize::LargeInput,
)
});
group.bench_function(
format!("unpartitioned/{}_{}", compression, batch_size),
|b| {
b.iter_batched(
|| {
let rt = runtime();
let mut batch = BatchSettings::default();
batch.size.bytes = *batch_size;
batch.size.events = num_events;
let batch_sink = BatchSink::new(
tower::service_fn(|_| future::ok::<_, Infallible>(())),
Buffer::new(batch.size, *compression),
Duration::from_secs(1),
)
.sink_map_err(|error| panic!("{}", error));
(
rt,
stream::iter(input.clone()).map(|item| Ok(EncodedEvent::new(item, 0))),
batch_sink,
)
},
|(rt, input, batch_sink)| rt.block_on(input.forward(batch_sink)).unwrap(),
criterion::BatchSize::LargeInput,
)
},
);
}
}
criterion_group!(
name = benches;
// noisy benchmarks; 10% encapsulates what we saw in
// https://github.com/vectordotdev/vector/issues/5394
config = Criterion::default().noise_threshold(0.10);
targets = benchmark_batch
);
pub struct PartitionedBuffer {
inner: Buffer,
key: Option<Bytes>,
}
#[derive(Clone)]
pub struct InnerBuffer {
pub(self) inner: BytesMut,
key: Bytes,
}
impl Partition<Bytes> for InnerBuffer {
fn partition(&self) -> Bytes {
self.key.clone()
}
}
impl PartitionedBuffer {
pub fn new(batch: BatchSize<Buffer>, compression: Compression) -> Self {
Self {
inner: Buffer::new(batch, compression),
key: None,
}
}
}
impl Batch for PartitionedBuffer {
type Input = InnerBuffer;
type Output = InnerBuffer;
fn get_settings_defaults<D: SinkBatchSettings + Clone>(
config: BatchConfig<D, Merged>,
) -> Result<BatchConfig<D, Merged>, BatchError> {
Buffer::get_settings_defaults(config)
}
fn push(&mut self, item: Self::Input) -> PushResult<Self::Input> {
let key = item.key;
match Batch::push(&mut self.inner, item.inner) {
PushResult::Ok(full) => {
self.key = Some(key);
PushResult::Ok(full)
}
PushResult::Overflow(inner) => PushResult::Overflow(InnerBuffer { inner, key }),
}
}
fn is_empty(&self) -> bool {
self.inner.is_empty()
}
fn fresh(&self) -> Self {
Self {
inner: self.inner.fresh(),
key: None,
}
}
fn finish(mut self) -> Self::Output {
let key = self.key.take().unwrap();
let inner = self.inner.finish();
InnerBuffer { inner, key }
}
fn num_items(&self) -> usize {
self.inner.num_items()
}
}