-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriterion.rs
75 lines (60 loc) · 2.2 KB
/
criterion.rs
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
65
66
67
68
69
70
71
72
73
74
75
use std::io::Seek;
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use ripmors::*;
fn ascii_benchmark(c: &mut Criterion) {
let data = std::fs::read_to_string("1-original.txt").unwrap();
let data = data.as_bytes();
let mut f = std::fs::File::open("1-original.txt").unwrap();
let mut devnull = std::fs::File::create("/dev/null").unwrap();
let mut group = c.benchmark_group("Encode ASCII");
group.throughput(Throughput::Bytes(data.len() as u64));
group.bench_function("string", |b| {
b.iter(|| encode_string_ascii(black_box(&data)))
});
group.bench_function("stream", |b| {
b.iter(|| {
f.rewind().unwrap();
encode_stream_ascii(&mut f, &mut devnull).unwrap();
})
});
group.finish();
}
fn unicode_benchmark(c: &mut Criterion) {
let data = std::fs::read_to_string("4-unicode.txt").unwrap();
let mut f = std::fs::File::open("4-unicode.txt").unwrap();
let mut devnull = std::fs::File::create("/dev/null").unwrap();
let mut group = c.benchmark_group("Encode Unicode");
group.throughput(Throughput::Bytes(data.len() as u64));
group.bench_function("string", |b| b.iter(|| encode_string(black_box(&data))));
group.bench_function("stream", |b| {
b.iter(|| {
f.rewind().unwrap();
encode_stream(&mut f, &mut devnull).unwrap();
})
});
group.finish();
}
fn decode_benchmark(c: &mut Criterion) {
let data = std::fs::read_to_string("2-encoded.txt").unwrap();
let mut f = std::fs::File::open("2-encoded.txt").unwrap();
let mut devnull = std::fs::File::create("/dev/null").unwrap();
let mut group = c.benchmark_group("Decode");
group.throughput(Throughput::Bytes(data.len() as u64));
group.bench_function("string", |b| {
b.iter(|| decode_string(black_box(&data.as_bytes()), to_standard))
});
group.bench_function("stream", |b| {
b.iter(|| {
f.rewind().unwrap();
decode_stream(&mut f, &mut devnull, to_standard).unwrap();
})
});
group.finish();
}
criterion_group!(
benches,
ascii_benchmark,
unicode_benchmark,
decode_benchmark
);
criterion_main!(benches);