|
1 | 1 | #[macro_use] |
2 | 2 | extern crate criterion; |
3 | | -extern crate rand; |
4 | 3 | extern crate bytecount; |
| 4 | +extern crate rand; |
5 | 5 |
|
| 6 | +use criterion::{Bencher, BenchmarkId, Criterion}; |
| 7 | +use rand::RngCore; |
6 | 8 | use std::env; |
7 | 9 | use std::time::Duration; |
8 | | -use rand::RngCore; |
9 | | -use criterion::{Bencher, Criterion, ParameterizedBenchmark}; |
10 | 10 |
|
11 | | -use bytecount::{ |
12 | | - count, naive_count, naive_count_32, |
13 | | - num_chars, naive_num_chars, |
14 | | -}; |
| 11 | +use bytecount::{count, naive_count, naive_count_32, naive_num_chars, num_chars}; |
15 | 12 |
|
16 | 13 | fn random_bytes(len: usize) -> Vec<u8> { |
17 | 14 | let mut result = vec![0; len]; |
18 | 15 | rand::thread_rng().fill_bytes(&mut result); |
19 | 16 | result |
20 | 17 | } |
21 | 18 |
|
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]; |
| 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, 1000, 1_000, 1_200, 1_400, 1_700, 2_100, 2_500, 3_000, 4_000, 5_000, 6_000, 7_000, |
| 22 | + 8_000, 9_000, 10_000, 12_000, 14_000, 17_000, 21_000, 25_000, 30_000, 100_000, 1_000_000, |
| 23 | +]; |
27 | 24 |
|
28 | 25 | fn get_counts() -> Vec<usize> { |
29 | | - env::var("COUNTS").map( |
30 | | - |s| s.split(',').map( |
31 | | - |n| str::parse::<usize>(n).unwrap()).collect()) |
| 26 | + env::var("COUNTS") |
| 27 | + .map(|s| { |
| 28 | + s.split(',') |
| 29 | + .map(|n| str::parse::<usize>(n).unwrap()) |
| 30 | + .collect() |
| 31 | + }) |
32 | 32 | .unwrap_or(COUNTS.to_owned()) |
33 | 33 | } |
34 | 34 |
|
35 | 35 | fn get_config() -> Criterion { |
36 | 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)) |
| 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)) |
41 | 42 | } else { |
42 | 43 | Criterion::default() |
43 | 44 | } |
44 | 45 | } |
45 | 46 |
|
46 | 47 | fn bench_counts(criterion: &mut Criterion) { |
47 | 48 | fn naive(b: &mut Bencher, s: &usize) { |
48 | | - let haystack = random_bytes(*s); |
| 49 | + let haystack = random_bytes(*s); |
49 | 50 | b.iter(|| naive_count(&haystack, 10)) |
50 | 51 | } |
51 | 52 | fn naive_32(b: &mut Bencher, s: &usize) { |
52 | | - let haystack = random_bytes(*s); |
| 53 | + let haystack = random_bytes(*s); |
53 | 54 | b.iter(|| naive_count_32(&haystack, 10)) |
54 | 55 | } |
55 | 56 | fn hyper(b: &mut Bencher, s: &usize) { |
56 | | - let haystack = random_bytes(*s); |
| 57 | + let haystack = random_bytes(*s); |
57 | 58 | b.iter(|| count(&haystack, 10)) |
58 | 59 | } |
59 | 60 | 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)); |
| 61 | + let mut group = criterion.benchmark_group("counts"); |
| 62 | + for count in counts { |
| 63 | + group.bench_with_input(BenchmarkId::new("naive", count), &count, naive); |
| 64 | + group.bench_with_input(BenchmarkId::new("naive_32", count), &count, naive_32); |
| 65 | + group.bench_with_input(BenchmarkId::new("hyper", count), &count, hyper); |
| 66 | + } |
64 | 67 | } |
65 | 68 |
|
66 | 69 | fn bench_num_chars(criterion: &mut Criterion) { |
67 | 70 | fn naive(b: &mut Bencher, s: &usize) { |
68 | | - let haystack = random_bytes(*s); |
| 71 | + let haystack = random_bytes(*s); |
69 | 72 | b.iter(|| naive_num_chars(&haystack)) |
70 | 73 | } |
71 | 74 | fn hyper(b: &mut Bencher, s: &usize) { |
72 | | - let haystack = random_bytes(*s); |
| 75 | + let haystack = random_bytes(*s); |
73 | 76 | b.iter(|| num_chars(&haystack)) |
74 | 77 | } |
75 | 78 | let counts = get_counts(); |
76 | | - criterion.bench("num_chars", |
77 | | - ParameterizedBenchmark::new("naive", naive, counts) |
78 | | - .with_function("hyper", hyper)); |
| 79 | + let mut group = criterion.benchmark_group("num_chars"); |
| 80 | + for count in counts { |
| 81 | + group.bench_with_input(BenchmarkId::new("naive", count), &count, naive); |
| 82 | + group.bench_with_input(BenchmarkId::new("hyper", count), &count, hyper); |
| 83 | + } |
79 | 84 | } |
80 | 85 |
|
81 | 86 | criterion_group!(name = count_bench; config = get_config(); targets = bench_counts); |
|
0 commit comments