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
|
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use uri_pack::{pack_uri, unpack_uri};
fn rand_compat(size: usize) -> String {
String::from_utf8(
std::iter::repeat_with(|| fastrand::u8(b' '..=b'~'))
.take(size)
.collect(),
)
.unwrap()
}
const COMMON: [u8; 31] = [
b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p',
b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', b'.', b'/', b'-', b'_', b'%',
];
fn rand_simple(size: usize) -> String {
String::from_utf8(
std::iter::repeat_with(|| COMMON[fastrand::usize(..COMMON.len())])
.take(size)
.collect(),
)
.unwrap()
}
fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Uri");
for size in [50, 500, 4048].iter() {
group.throughput(Throughput::Bytes(*size as u64));
group.bench_with_input(BenchmarkId::new("pack rand", size), size, |b, &size| {
b.iter_batched(
|| rand_compat(size),
|uri| pack_uri(&uri).unwrap(),
criterion::BatchSize::SmallInput,
)
});
group.bench_with_input(BenchmarkId::new("unpack rand", size), size, |b, &size| {
b.iter_batched(
|| pack_uri(&rand_compat(size)).unwrap(),
|(packed, len)| unpack_uri(&packed, len),
criterion::BatchSize::SmallInput,
)
});
group.bench_with_input(BenchmarkId::new("pack simple", size), size, |b, &size| {
b.iter_batched(
|| rand_simple(size),
|uri| pack_uri(&uri).unwrap(),
criterion::BatchSize::SmallInput,
)
});
group.bench_with_input(BenchmarkId::new("unpack simple", size), size, |b, &size| {
b.iter_batched(
|| pack_uri(&rand_simple(size)).unwrap(),
|(packed, len)| unpack_uri(&packed, len),
criterion::BatchSize::SmallInput,
)
});
}
group.finish();
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
|