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