Debian Codegraph @

debian/missing-sources/bytecount/benches/bench.rs

raw
1 #[macro_use]
2 extern crate criterion;
3 extern crate bytecount;
4 extern crate rand;
5
6 use criterion::{Bencher, BenchmarkId, Criterion};
7 use rand::RngCore;
8 use std::env;
9 use std::time::Duration;
10
11 use bytecount::{count, naive_count, naive_count_32, naive_num_chars, num_chars};
12
13 fn random_bytes(len: usize) -> Vec<u8> {
14 let mut result = vec![0; len];
15 rand::thread_rng().fill_bytes(&mut result);
16 result
17 }
18
19 static COUNTS: &[usize] = &[
20 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 120, 140, 170, 210, 250, 300, 400, 500, 600, 700,
21 800, 900, 1_000, 1_200, 1_400, 1_700, 2_100, 2_500, 3_000, 4_000, 5_000, 6_000, 7_000, 8_000,
22 9_000, 10_000, 12_000, 14_000, 17_000, 21_000, 25_000, 30_000, 100_000, 1_000_000,
23 ];
24
25 fn get_counts() -> Vec<usize> {
26 env::var("COUNTS")
27 .map(|s| {
28 s.split(',')
29 .map(|n| str::parse::<usize>(n).unwrap())
30 .collect()
31 })
32 .unwrap_or(COUNTS.to_owned())
33 }
34
35 fn get_config() -> Criterion {
36 if env::var("CI").is_ok() {
37 Criterion::default()
38 .nresamples(5_000)
39 .without_plots()
40 .measurement_time(Duration::new(2, 0))
41 .warm_up_time(Duration::new(1, 0))
42 } else {
43 Criterion::default()
44 }
45 }
46
47 fn bench_counts(criterion: &mut Criterion) {
48 fn naive(b: &mut Bencher, s: &usize) {
49 let haystack = random_bytes(*s);
50 b.iter(|| naive_count(&haystack, 10))
51 }
52 fn naive_32(b: &mut Bencher, s: &usize) {
53 let haystack = random_bytes(*s);
54 b.iter(|| naive_count_32(&haystack, 10))
55 }
56 fn hyper(b: &mut Bencher, s: &usize) {
57 let haystack = random_bytes(*s);
58 b.iter(|| count(&haystack, 10))
59 }
60 let counts = get_counts();
61 let mut group = criterion.benchmark_group("counts");
62 for count in counts {
63 group.throughput(criterion::Throughput::Bytes(count as u64));
64 group.bench_with_input(BenchmarkId::new("naive", count), &count, naive);
65 group.bench_with_input(BenchmarkId::new("naive_32", count), &count, naive_32);
66 group.bench_with_input(BenchmarkId::new("hyper", count), &count, hyper);
67 }
68 }
69
70 fn bench_num_chars(criterion: &mut Criterion) {
71 fn naive(b: &mut Bencher, s: &usize) {
72 let haystack = random_bytes(*s);
73 b.iter(|| naive_num_chars(&haystack))
74 }
75 fn hyper(b: &mut Bencher, s: &usize) {
76 let haystack = random_bytes(*s);
77 b.iter(|| num_chars(&haystack))
78 }
79 let counts = get_counts();
80 let mut group = criterion.benchmark_group("num_chars");
81 for count in counts {
82 group.throughput(criterion::Throughput::Bytes(count as u64));
83 group.bench_with_input(BenchmarkId::new("naive", count), &count, naive);
84 group.bench_with_input(BenchmarkId::new("hyper", count), &count, hyper);
85 }
86 }
87
88 criterion_group!(name = count_bench; config = get_config(); targets = bench_counts);
89 criterion_group!(name = num_chars_bench; config = get_config(); targets = bench_num_chars);
90 criterion_main!(count_bench, num_chars_bench);