Add benchmarks for string decoding
This commit is contained in:
parent
1e5525c9cf
commit
93d7c3d572
|
@ -13,4 +13,9 @@ thiserror = "1.0.29"
|
|||
xmlparser = "0.13.3"
|
||||
|
||||
[dev-dependencies]
|
||||
bencher = "0.1.5"
|
||||
similar-asserts = "1.4.2"
|
||||
|
||||
[[bench]]
|
||||
name = "decode"
|
||||
harness = false
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use bencher::Bencher;
|
||||
use bencher::{benchmark_group, benchmark_main};
|
||||
use instant_xml::{from_str, FromXml};
|
||||
|
||||
fn decode_short_ascii(bench: &mut Bencher) {
|
||||
let xml = "<Element><inner>foobar</inner></Element>";
|
||||
bench.iter(|| {
|
||||
from_str::<Element>(xml).unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
fn decode_longer_ascii(bench: &mut Bencher) {
|
||||
let mut xml = String::with_capacity(4096);
|
||||
xml.push_str("<Element><inner>");
|
||||
for _ in 0..64 {
|
||||
xml.push_str("abcdefghijklmnopqrstuvwxyz");
|
||||
xml.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
xml.push_str("0123456789");
|
||||
}
|
||||
xml.push_str("</inner></Element>");
|
||||
|
||||
bench.iter(|| {
|
||||
from_str::<Element>(&xml).unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
fn decode_short_escaped(bench: &mut Bencher) {
|
||||
let xml = "<Element><inner>foo & bar</inner></Element>";
|
||||
bench.iter(|| {
|
||||
from_str::<Element>(xml).unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
fn decode_longer_escaped(bench: &mut Bencher) {
|
||||
let mut xml = String::with_capacity(4096);
|
||||
xml.push_str("<Element><inner>");
|
||||
for _ in 0..64 {
|
||||
xml.push_str("abcdefghijklmnopqrstuvwxyz");
|
||||
xml.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
xml.push_str("0123456789");
|
||||
xml.push_str(""");
|
||||
}
|
||||
xml.push_str("</inner></Element>");
|
||||
|
||||
bench.iter(|| {
|
||||
from_str::<Element>(&xml).unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, FromXml)]
|
||||
struct Element<'a> {
|
||||
#[allow(dead_code)]
|
||||
inner: Cow<'a, str>,
|
||||
}
|
||||
|
||||
benchmark_group!(
|
||||
benches,
|
||||
decode_short_ascii,
|
||||
decode_longer_ascii,
|
||||
decode_short_escaped,
|
||||
decode_longer_escaped,
|
||||
);
|
||||
benchmark_main!(benches);
|
Loading…
Reference in New Issue