From 93d7c3d57286db11f75a5f42d3c489297dba56e3 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 22 Feb 2023 10:20:55 +0100 Subject: [PATCH] Add benchmarks for string decoding --- instant-xml/Cargo.toml | 5 +++ instant-xml/benches/decode.rs | 65 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 instant-xml/benches/decode.rs diff --git a/instant-xml/Cargo.toml b/instant-xml/Cargo.toml index 32d6c15..da019f0 100644 --- a/instant-xml/Cargo.toml +++ b/instant-xml/Cargo.toml @@ -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 diff --git a/instant-xml/benches/decode.rs b/instant-xml/benches/decode.rs new file mode 100644 index 0000000..e35c0ae --- /dev/null +++ b/instant-xml/benches/decode.rs @@ -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 = "foobar"; + bench.iter(|| { + from_str::(xml).unwrap(); + }) +} + +fn decode_longer_ascii(bench: &mut Bencher) { + let mut xml = String::with_capacity(4096); + xml.push_str(""); + for _ in 0..64 { + xml.push_str("abcdefghijklmnopqrstuvwxyz"); + xml.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + xml.push_str("0123456789"); + } + xml.push_str(""); + + bench.iter(|| { + from_str::(&xml).unwrap(); + }) +} + +fn decode_short_escaped(bench: &mut Bencher) { + let xml = "foo & bar"; + bench.iter(|| { + from_str::(xml).unwrap(); + }) +} + +fn decode_longer_escaped(bench: &mut Bencher) { + let mut xml = String::with_capacity(4096); + xml.push_str(""); + for _ in 0..64 { + xml.push_str("abcdefghijklmnopqrstuvwxyz"); + xml.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + xml.push_str("0123456789"); + xml.push_str("""); + } + xml.push_str(""); + + bench.iter(|| { + from_str::(&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);