-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathencoder.rs
More file actions
124 lines (110 loc) · 4.02 KB
/
encoder.rs
File metadata and controls
124 lines (110 loc) · 4.02 KB
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
use std::time::Duration;
use bytes::{BufMut, BytesMut};
use criterion::{
BatchSize, BenchmarkGroup, Criterion, SamplingMode, Throughput, criterion_group,
measurement::WallTime,
};
use tokio_util::codec::Encoder;
use vector::event::{Event, LogEvent};
use vector_lib::codecs::{JsonSerializerConfig, NewlineDelimitedEncoder, encoding::Framer};
use vector_lib::{btreemap, byte_size_of::ByteSizeOf};
#[derive(Debug, Clone)]
pub struct JsonLogSerializer;
impl Encoder<Event> for JsonLogSerializer {
type Error = vector_lib::Error;
fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
let writer = buffer.writer();
let log = event.as_log();
serde_json::to_writer(writer, log)?;
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct JsonLogVecSerializer;
impl Encoder<Event> for JsonLogVecSerializer {
type Error = vector_lib::Error;
fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
let log = event.as_log();
let vec = serde_json::to_vec(log)?;
buffer.put_slice(&vec);
Ok(())
}
}
fn encoder(c: &mut Criterion) {
let mut group: BenchmarkGroup<WallTime> = c.benchmark_group("encoder");
group.sampling_mode(SamplingMode::Auto);
let input: Event = Event::Log(LogEvent::from(btreemap! {
"key1" => "value1",
"key2" => "value2",
"key3" => "value3"
}));
group.throughput(Throughput::Bytes(input.size_of() as u64));
group.bench_with_input("JsonLogVecSerializer::encode", &(), |b, ()| {
b.iter_batched(
|| JsonLogVecSerializer,
|mut encoder| {
let mut bytes = BytesMut::new();
encoder.encode(input.clone(), &mut bytes).unwrap();
bytes.put_u8(b'\n');
},
BatchSize::SmallInput,
)
});
group.throughput(Throughput::Bytes(input.size_of() as u64));
group.bench_with_input("JsonLogSerializer::encode", &(), |b, ()| {
b.iter_batched(
|| JsonLogSerializer,
|mut encoder| {
let mut bytes = BytesMut::new();
encoder.encode(input.clone(), &mut bytes).unwrap();
bytes.put_u8(b'\n');
},
BatchSize::SmallInput,
)
});
group.throughput(Throughput::Bytes(input.size_of() as u64));
group.bench_with_input("codecs::JsonSerializer::encode", &(), |b, ()| {
b.iter_batched(
|| JsonSerializerConfig::default().build(),
|mut encoder| {
let mut bytes = BytesMut::new();
encoder.encode(input.clone(), &mut bytes).unwrap();
bytes.put_u8(b'\n');
},
BatchSize::SmallInput,
)
});
group.throughput(Throughput::Bytes(input.size_of() as u64));
group.bench_with_input("vector::codecs::Encoder::encode", &(), |b, ()| {
b.iter_batched(
|| {
vector::codecs::Encoder::<Framer>::new(
NewlineDelimitedEncoder::default().into(),
JsonSerializerConfig::default().build().into(),
)
},
|mut encoder| {
let mut bytes = BytesMut::new();
encoder.encode(input.clone(), &mut bytes).unwrap();
},
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 = encoder
);