Skip to content

Commit 5a2e898

Browse files
committed
Move benchmarking to Criterion
1 parent 989bffe commit 5a2e898

File tree

4 files changed

+79
-44
lines changed

4 files changed

+79
-44
lines changed

Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@ documentation = "https://docs.rs/sxd-document"
1414

1515
license = "MIT"
1616

17+
[[bench]]
18+
name = "string_pool"
19+
harness = false
20+
required-features = ["__internal_expose_string_pool"]
21+
1722
[features]
1823
compile_failure = []
19-
unstable = []
24+
25+
__internal_expose_string_pool = []
2026

2127
[dependencies]
2228
peresil = "0.3.0"
2329
typed-arena = "2.0"
30+
31+
[dev-dependencies]
32+
criterion = "0.5.1"

benches/string_pool.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
2+
use sxd_document::__internal::StringPool;
3+
4+
fn single_string(c: &mut Criterion) {
5+
let mut group = c.benchmark_group("single_string");
6+
7+
let original = "hello";
8+
9+
group.throughput(Throughput::Bytes(original.len() as u64));
10+
group.bench_function("single_string", |b| {
11+
let pool = StringPool::new();
12+
b.iter(|| pool.intern(original));
13+
});
14+
15+
group.finish();
16+
}
17+
18+
fn many_unique_string(c: &mut Criterion) {
19+
let mut group = c.benchmark_group("many_unique_string");
20+
21+
let strings: Vec<_> = (0..1000).map(|i| format!("str{i}str")).collect();
22+
let total_len = strings.iter().map(|s| s.len()).sum::<usize>();
23+
24+
group.throughput(Throughput::Bytes(total_len as u64));
25+
group.bench_function("many_unique_string", |b| {
26+
let pool = StringPool::new();
27+
28+
b.iter(|| {
29+
for s in &strings {
30+
pool.intern(s);
31+
}
32+
});
33+
});
34+
35+
group.finish();
36+
}
37+
38+
fn many_repeated_string(c: &mut Criterion) {
39+
let mut group = c.benchmark_group("many_repeated_string");
40+
41+
let strings: Vec<_> = (0..1000).map(|i| format!("str{}str", i % 100)).collect();
42+
let total_len = strings.iter().map(|s| s.len()).sum::<usize>();
43+
44+
group.throughput(Throughput::Bytes(total_len as u64));
45+
group.bench_function("many_unique_string", |b| {
46+
let pool = StringPool::new();
47+
48+
b.iter(|| {
49+
for s in &strings {
50+
pool.intern(s);
51+
}
52+
});
53+
});
54+
55+
group.finish();
56+
}
57+
58+
criterion_group!(
59+
benches,
60+
single_string,
61+
many_unique_string,
62+
many_repeated_string,
63+
);
64+
criterion_main!(benches);

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
//! Try to leverage the type system as much as possible.
5050
5151
#![deny(rust_2018_idioms)]
52-
#![cfg_attr(feature = "unstable", feature(test))]
5352

5453
#[macro_use]
5554
extern crate peresil;
@@ -68,6 +67,11 @@ pub mod parser;
6867
pub mod thindom;
6968
pub mod writer;
7069

70+
#[cfg(feature = "__internal_expose_string_pool")]
71+
pub mod __internal {
72+
pub use super::string_pool::StringPool;
73+
}
74+
7175
pub use crate::str::XmlChar;
7276

7377
static XML_NS_PREFIX: &str = "xml";

src/string_pool.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -275,45 +275,3 @@ mod test {
275275
assert_eq!(interned.as_ptr(), ptr_val);
276276
}
277277
}
278-
279-
#[cfg(feature = "unstable")]
280-
mod bench {
281-
extern crate test;
282-
283-
use self::test::Bencher;
284-
285-
use super::StringPool;
286-
287-
#[bench]
288-
fn single_string(b: &mut Bencher) {
289-
let s = StringPool::new();
290-
b.iter(|| s.intern("hello"));
291-
b.bytes = "hello".len() as u64;
292-
}
293-
294-
#[bench]
295-
fn many_unique_string(b: &mut Bencher) {
296-
let s = StringPool::new();
297-
298-
let strings: Vec<String> = (0..1000).map(|i| format!("str{}str", i)).collect();
299-
b.iter(|| {
300-
for ss in strings.iter() {
301-
s.intern(ss);
302-
}
303-
});
304-
b.bytes = strings.iter().fold(0, |a, s| a + s.len()) as u64;
305-
}
306-
307-
#[bench]
308-
fn many_repeated_string(b: &mut Bencher) {
309-
let s = StringPool::new();
310-
311-
let strings: Vec<String> = (0..1000).map(|i| format!("str{}str", i % 100)).collect();
312-
b.iter(|| {
313-
for ss in strings.iter() {
314-
s.intern(ss);
315-
}
316-
});
317-
b.bytes = strings.iter().fold(0, |a, s| a + s.len()) as u64;
318-
}
319-
}

0 commit comments

Comments
 (0)