From ee081ca70322cd01d0642fdb1860e6bfbfb48e85 Mon Sep 17 00:00:00 2001 From: steven-vd Date: Sun, 16 Aug 2026 15:00:32 +0200 Subject: Initial commit --- .../quick-xml/src/count_highway_crossings.rs | 74 +++++++++++++++++++ benchmarks/quick-xml/src/main.rs | 39 ++++++++++ benchmarks/quick-xml/src/pubmed_len.rs | 82 ++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 benchmarks/quick-xml/src/count_highway_crossings.rs create mode 100644 benchmarks/quick-xml/src/main.rs create mode 100644 benchmarks/quick-xml/src/pubmed_len.rs (limited to 'benchmarks/quick-xml/src') diff --git a/benchmarks/quick-xml/src/count_highway_crossings.rs b/benchmarks/quick-xml/src/count_highway_crossings.rs new file mode 100644 index 0000000..9b68142 --- /dev/null +++ b/benchmarks/quick-xml/src/count_highway_crossings.rs @@ -0,0 +1,74 @@ +use quick_xml::events::Event; +use quick_xml::Reader; + +pub fn count_highway_crossings(buffer: &[u8]) -> usize { + let mut reader = Reader::from_reader(buffer); + reader.config_mut().check_end_names = false; + + let mut count = 0usize; + let mut in_node = false; + + loop { + match reader.read_event() { + Ok(Event::Eof) => break, + + Ok(Event::Start(e)) if e.name().as_ref() == b"node" => { + in_node = true; + } + + Ok(Event::End(e)) if e.name().as_ref() == b"node" => { + in_node = false; + } + + Ok(Event::Start(e)) | Ok(Event::Empty(e)) + if in_node && e.name().as_ref() == b"tag" => + { + let mut attrs = e.attributes(); + if attrs.next().unwrap().unwrap().value.as_ref() == b"highway" + && attrs.next().unwrap().unwrap().value.as_ref() == b"crossing" { + count += 1; + } + } + + Ok(_) => {} + Err(_) => break, + } + } + + count +} + +pub fn count_highway_crossings_cheat_and_early_out(buffer: &[u8]) -> usize { + let mut reader = Reader::from_reader(buffer); + reader.config_mut().check_end_names = false; + + let mut count = 0usize; + + loop { + match reader.read_event() { + Ok(Event::Eof) => break, + + Ok(Event::Start(e)) | Ok(Event::Empty(e)) + if e.name().as_ref() == b"tag" => + { + let mut attrs = e.attributes(); + if attrs.next().unwrap().unwrap().value.as_ref() == b"highway" + && attrs.next().unwrap().unwrap().value.as_ref() == b"crossing" { + count += 1; + } + } + + Ok(Event::Start(e)) | Ok(Event::Empty(e)) + if e.name().as_ref() == b"way" => + { + break; + } + + Ok(_) => {} + Err(_) => break, + } + } + + count +} + diff --git a/benchmarks/quick-xml/src/main.rs b/benchmarks/quick-xml/src/main.rs new file mode 100644 index 0000000..fef0ee7 --- /dev/null +++ b/benchmarks/quick-xml/src/main.rs @@ -0,0 +1,39 @@ +use std::fs; + +#[path = "../../harness.rs"] +mod harness; +use harness::*; + +mod pubmed_len; +use pubmed_len::*; +mod count_highway_crossings; +use count_highway_crossings::*; + +macro_rules! bench { + ($f:expr, $buffer:expr, $out:expr) => { + bench(String::from(stringify!($f)), $f, $buffer.as_slice(), &mut $out) + }; +} + +fn main() { + let mut out = String::from( + "\n\ + \n", + ); + + if true { + let buffer = fs::read("../../dat/yellowstone.osm").expect("Failed to read file"); + bench!(count_highway_crossings, buffer, out); + bench!(count_highway_crossings_cheat_and_early_out, buffer, out); + } + + if true { + let buffer = fs::read("../../dat/PMC176545.xml").expect("Failed to read file"); + bench!(pubmed_len, buffer, out); + bench!(pubmed_len_with_entities, buffer, out); + } + + out.push_str("\n\n"); + std::fs::write("benchmark-results.xml", out).unwrap(); +} + diff --git a/benchmarks/quick-xml/src/pubmed_len.rs b/benchmarks/quick-xml/src/pubmed_len.rs new file mode 100644 index 0000000..592e6e3 --- /dev/null +++ b/benchmarks/quick-xml/src/pubmed_len.rs @@ -0,0 +1,82 @@ +use quick_xml::events::Event; +use quick_xml::Reader; + +pub fn pubmed_len(buffer: &[u8]) -> usize { + let mut reader = Reader::from_reader(buffer); + let mut in_content = 0usize; + let mut total = 0usize; + + loop { + match reader.read_event() { + Ok(Event::Start(e)) => { + if in_content != 0 { + in_content += 1; + } else { + if e.name().as_ref() == b"abstract" + || e.name().as_ref()== b"body" { + in_content = 1; + } + } + } + + Ok(Event::End(_)) => { + if in_content != 0 { + in_content -= 1; + } + } + + Ok(Event::Text(e)) => { + if in_content != 0 { + total += e.len(); + } + } + + Ok(Event::Eof) => break, + + Ok(_) => {} + Err(_) => unreachable!("buffer is valid PMC XML"), + } + } + + total +} + +pub fn pubmed_len_with_entities(buffer: &[u8]) -> usize { + let mut reader = Reader::from_reader(buffer); + let mut in_content = 0usize; + let mut total = 0usize; + + loop { + match reader.read_event() { + Ok(Event::Start(e)) => { + if in_content != 0 { + in_content += 1; + } else if e.name().as_ref() == b"abstract" + || e.name().as_ref() == b"body" + { + in_content = 1; + } + } + + Ok(Event::End(_)) => { + if in_content != 0 { + in_content -= 1; + } + } + + Ok(Event::Text(e)) => { + if in_content != 0 { + total += e.decode().unwrap().len(); + } + } + + Ok(Event::Eof) => break, + + Ok(_) => {} + Err(_) => unreachable!("buffer is valid PMC XML"), + } + } + + total +} + -- cgit v1.2.3