Debian Codegraph @

benches/bench.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 #![allow(unknown_lints)]
3
4 use criterion::{criterion_group, criterion_main, Criterion, BatchSize, Throughput, ParameterizedBenchmark};
5 use rand::prelude::*;
6
7
8 const SIZES: &[usize] = &[3, 16, 64, 256, 1024];
9
10 fn rand_enc_input(sz: usize) -> (Vec<u8>, base16::EncConfig) {
11 let mut rng = thread_rng();
12 let mut vec = vec![0u8; sz];
13 let cfg = if rng.gen::<bool>() {
14 base16::EncodeUpper
15 } else {
16 base16::EncodeLower
17 };
18 rng.fill_bytes(&mut vec);
19 (vec, cfg)
20 }
21
22 fn batch_size_for_input(i: usize) -> BatchSize {
23 if i < 1024 {
24 BatchSize::SmallInput
25 } else {
26 BatchSize::LargeInput
27 }
28 }
29
30 fn bench_encode(c: &mut Criterion) {
31 c.bench(
32 "encode to fresh string",
33 ParameterizedBenchmark::new(
34 "encode_config",
35 |b, items| {
36 b.iter_batched(
37 || rand_enc_input(*items),
38 |(input, enc)| base16::encode_config(&input, enc),
39 batch_size_for_input(*items),
40 )
41 },
42 SIZES.iter().cloned(),
43 ).throughput(|bytes| Throughput::Bytes(*bytes as u32)),
44 );
45
46 c.bench(
47 "encode to preallocated string",
48 ParameterizedBenchmark::new(
49 "encode_config_buf",
50 |b, items| {
51 b.iter_batched(
52 || (rand_enc_input(*items), String::with_capacity(2 * *items)),
53 |((input, enc), mut buf)| {
54 buf.truncate(0);
55 base16::encode_config_buf(&input, enc, &mut buf)
56 },
57 batch_size_for_input(*items),
58 )
59 },
60 SIZES.iter().cloned(),
61 ).throughput(|bytes| Throughput::Bytes(*bytes as u32)),
62 );
63
64 c.bench(
65 "encode to slice",
66 ParameterizedBenchmark::new(
67 "encode_config_slice",
68 |b, items| {
69 b.iter_batched(
70 || (rand_enc_input(*items), vec![0u8; 2 * *items]),
71 |((input, enc), mut dst)| {
72 base16::encode_config_slice(&input, enc, &mut dst)
73 },
74 batch_size_for_input(*items),
75 )
76 },
77 SIZES.iter().cloned(),
78 ).throughput(|bytes| Throughput::Bytes(*bytes as u32)),
79 );
80 }
81
82 fn rand_hex_string(size: usize) -> String {
83 let mut rng = thread_rng();
84 let mut s = String::with_capacity(size);
85 let chars: &[u8] = b"0123456789abcdefABCDEF";
86 while s.len() < size {
87 s.push(*chars.choose(&mut rng).unwrap() as char);
88 }
89 s
90 }
91
92 fn bench_decode(c: &mut Criterion) {
93 c.bench(
94 "decode to fresh vec",
95 ParameterizedBenchmark::new(
96 "decode",
97 |b, items| {
98 b.iter_batched(
99 || rand_hex_string(*items),
100 |input| base16::decode(&input),
101 batch_size_for_input(*items),
102 )
103 },
104 SIZES.iter().cloned(),
105 ).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)),
106 );
107
108 c.bench(
109 "decode to preallocated vec",
110 ParameterizedBenchmark::new(
111 "decode_buf",
112 |b, items| {
113 b.iter_batched(
114 || (rand_hex_string(*items), Vec::with_capacity(*items)),
115 |(input, mut buf)| {
116 buf.truncate(0);
117 base16::decode_buf(&input, &mut buf)
118 },
119 batch_size_for_input(*items),
120 )
121 },
122 SIZES.iter().cloned(),
123 ).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)),
124 );
125
126 c.bench(
127 "decode to slice",
128 ParameterizedBenchmark::new(
129 "decode_slice",
130 |b, items| {
131 b.iter_batched(
132 || (rand_hex_string(*items), vec![0u8; *items]),
133 |(input, mut buf)| {
134 base16::decode_slice(&input, &mut buf)
135 },
136 batch_size_for_input(*items),
137 )
138 },
139 SIZES.iter().cloned(),
140 ).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)),
141 );
142 }
143
144
145 criterion_group!(benches, bench_encode, bench_decode);
146 criterion_main!(benches);
•147 */