Skip to content

Commit d0c4a1f

Browse files
committed
Add benchmarks
1 parent 92e8881 commit d0c4a1f

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ name = "macrobenches"
6262
harness = false
6363
path = "benches/macrobenches.rs"
6464

65+
[[bench]]
66+
name = "encoding"
67+
harness = false
68+
path = "benches/encoding.rs"
69+
6570
[features]
6671
default = []
6772

benches/encoding.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
2+
use quick_xml::encoding::Utf8ValidatingReader;
3+
use std::io::{BufReader, Read};
4+
5+
static SAMPLE: &[u8] = include_bytes!("../tests/documents/sample_rss.xml");
6+
7+
/// Read the entire input through the reader using a fixed-size buffer,
8+
/// returning the total number of bytes read.
9+
fn drain_reader(reader: &mut impl Read, buf: &mut [u8]) -> usize {
10+
let mut total = 0;
11+
loop {
12+
match reader.read(buf) {
13+
Ok(0) => break,
14+
Ok(n) => total += n,
15+
Err(e) => panic!("unexpected error: {e}"),
16+
}
17+
}
18+
total
19+
}
20+
21+
fn bench_utf8_validation(c: &mut Criterion) {
22+
let mut group = c.benchmark_group("utf8_read");
23+
group.throughput(Throughput::Bytes(SAMPLE.len() as u64));
24+
25+
for buf_size in [64, 1024, 8192] {
26+
group.bench_with_input(
27+
BenchmarkId::new("BufReader_only", buf_size),
28+
&buf_size,
29+
|b, &buf_size| {
30+
b.iter(|| {
31+
let mut reader = BufReader::new(SAMPLE);
32+
let mut buf = vec![0u8; buf_size];
33+
let n = drain_reader(&mut reader, &mut buf);
34+
assert_eq!(n, SAMPLE.len());
35+
});
36+
},
37+
);
38+
39+
group.bench_with_input(
40+
BenchmarkId::new("Utf8ValidatingReader", buf_size),
41+
&buf_size,
42+
|b, &buf_size| {
43+
b.iter(|| {
44+
let mut reader = Utf8ValidatingReader::new(BufReader::new(SAMPLE));
45+
let mut buf = vec![0u8; buf_size];
46+
let n = drain_reader(&mut reader, &mut buf);
47+
assert_eq!(n, SAMPLE.len());
48+
});
49+
},
50+
);
51+
}
52+
53+
group.finish();
54+
}
55+
56+
criterion_group!(benches, bench_utf8_validation);
57+
criterion_main!(benches);

0 commit comments

Comments
 (0)