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
|
#![allow(unknown_lints)]
use criterion::{criterion_group, criterion_main, Criterion, BatchSize, Throughput, ParameterizedBenchmark};
use rand::prelude::*;
const SIZES: &[usize] = &[3, 16, 64, 256, 1024];
fn rand_enc_input(sz: usize) -> (Vec<u8>, base16::EncConfig) {
let mut rng = thread_rng();
let mut vec = vec![0u8; sz];
let cfg = if rng.gen::<bool>() {
base16::EncodeUpper
} else {
base16::EncodeLower
};
rng.fill_bytes(&mut vec);
(vec, cfg)
}
fn batch_size_for_input(i: usize) -> BatchSize {
if i < 1024 {
BatchSize::SmallInput
} else {
BatchSize::LargeInput
}
}
fn bench_encode(c: &mut Criterion) {
c.bench(
"encode to fresh string",
ParameterizedBenchmark::new(
"encode_config",
|b, items| {
b.iter_batched(
|| rand_enc_input(*items),
|(input, enc)| base16::encode_config(&input, enc),
batch_size_for_input(*items),
)
},
SIZES.iter().cloned(),
).throughput(|bytes| Throughput::Bytes(*bytes as u32)),
);
c.bench(
"encode to preallocated string",
ParameterizedBenchmark::new(
"encode_config_buf",
|b, items| {
b.iter_batched(
|| (rand_enc_input(*items), String::with_capacity(2 * *items)),
|((input, enc), mut buf)| {
buf.truncate(0);
base16::encode_config_buf(&input, enc, &mut buf)
},
batch_size_for_input(*items),
)
},
SIZES.iter().cloned(),
).throughput(|bytes| Throughput::Bytes(*bytes as u32)),
);
c.bench(
"encode to slice",
ParameterizedBenchmark::new(
"encode_config_slice",
|b, items| {
b.iter_batched(
|| (rand_enc_input(*items), vec![0u8; 2 * *items]),
|((input, enc), mut dst)| {
base16::encode_config_slice(&input, enc, &mut dst)
},
batch_size_for_input(*items),
)
},
SIZES.iter().cloned(),
).throughput(|bytes| Throughput::Bytes(*bytes as u32)),
);
}
fn rand_hex_string(size: usize) -> String {
let mut rng = thread_rng();
let mut s = String::with_capacity(size);
let chars: &[u8] = b"0123456789abcdefABCDEF";
while s.len() < size {
s.push(*chars.choose(&mut rng).unwrap() as char);
}
s
}
fn bench_decode(c: &mut Criterion) {
c.bench(
"decode to fresh vec",
ParameterizedBenchmark::new(
"decode",
|b, items| {
b.iter_batched(
|| rand_hex_string(*items),
|input| base16::decode(&input),
batch_size_for_input(*items),
)
},
SIZES.iter().cloned(),
).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)),
);
c.bench(
"decode to preallocated vec",
ParameterizedBenchmark::new(
"decode_buf",
|b, items| {
b.iter_batched(
|| (rand_hex_string(*items), Vec::with_capacity(*items)),
|(input, mut buf)| {
buf.truncate(0);
base16::decode_buf(&input, &mut buf)
},
batch_size_for_input(*items),
)
},
SIZES.iter().cloned(),
).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)),
);
c.bench(
"decode to slice",
ParameterizedBenchmark::new(
"decode_slice",
|b, items| {
b.iter_batched(
|| (rand_hex_string(*items), vec![0u8; *items]),
|(input, mut buf)| {
base16::decode_slice(&input, &mut buf)
},
batch_size_for_input(*items),
)
},
SIZES.iter().cloned(),
).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)),
);
}
criterion_group!(benches, bench_encode, bench_decode);
criterion_main!(benches);
|