Skip to content

Commit

Permalink
Update comparison crate to criterion as well
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Jan 5, 2022
1 parent e08e490 commit d96c5c9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 92 deletions.
7 changes: 6 additions & 1 deletion compare/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ edition = "2018"
[dev-dependencies]
quick-xml = { path = "..", features = ["serialize"] }
xml-rs = "0.8.0"
serde-xml-rs = "0.4.1"
serde-xml-rs = "0.5.1"
serde = { version = "1.0.103", features = [ "derive" ] }
criterion = "0.3.5"

[[bench]]
name = "bench"
harness = false
56 changes: 0 additions & 56 deletions compare/benches/bench-serde.rs

This file was deleted.

119 changes: 84 additions & 35 deletions compare/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,93 @@
#![feature(test)]
use criterion::{self, criterion_group, criterion_main, Criterion};
use quick_xml::{self, events::Event, Reader};
use serde::Deserialize;
use serde_xml_rs;
use xml::reader::{EventReader, XmlEvent};

extern crate quick_xml;
extern crate test;
extern crate xml;
static SOURCE: &str = include_str!("../../tests/sample_rss.xml");

use quick_xml::{events::Event, Reader};
use test::Bencher;
use xml::reader::{EventReader, XmlEvent};
fn comparison(c: &mut Criterion) {
let mut group = c.benchmark_group("raw_parse_comparison");

#[bench]
fn bench_quick_xml(b: &mut Bencher) {
let src: &[u8] = include_bytes!("../../tests/sample_rss.xml");
b.iter(|| {
let mut r = Reader::from_reader(src);
r.check_end_names(false).check_comments(false);
let mut count = test::black_box(0);
let mut buf = Vec::new();
loop {
match r.read_event(&mut buf) {
Ok(Event::Start(_)) | Ok(Event::Empty(_)) => count += 1,
Ok(Event::Eof) => break,
_ => (),
group.bench_function("quick_xml_normal", |b| {
b.iter(|| {
let mut r = Reader::from_reader(SOURCE.as_bytes());
r.check_end_names(false).check_comments(false);
let mut count = criterion::black_box(0);
let mut buf = Vec::new();
loop {
match r.read_event(&mut buf) {
Ok(Event::Start(_)) | Ok(Event::Empty(_)) => count += 1,
Ok(Event::Eof) => break,
_ => (),
}
buf.clear();
}
buf.clear();
}
assert_eq!(count, 1550);
assert_eq!(count, 1550);
})
});
}

#[bench]
fn bench_xml_rs(b: &mut Bencher) {
let src: &[u8] = include_bytes!("../../tests/sample_rss.xml");
b.iter(|| {
let r = EventReader::new(src);
let mut count = test::black_box(0);
for e in r {
if let Ok(XmlEvent::StartElement { .. }) = e {
count += 1;
group.bench_function("xml_rs_normal", |b| {
b.iter(|| {
let r = EventReader::new(SOURCE.as_bytes());
let mut count = criterion::black_box(0);
for e in r {
if let Ok(XmlEvent::StartElement { .. }) = e {
count += 1;
}
}
}
assert_eq!(count, 1550);
assert_eq!(count, 1550);
})
});
group.finish();
}

fn serde_comparison(c: &mut Criterion) {
let mut group = c.benchmark_group("serde_parse_comparison");
#[derive(Debug, Deserialize)]
struct Rss {
channel: Channel,
}

#[derive(Debug, Deserialize)]
struct Channel {
title: String,
#[serde(rename = "item", default)]
items: Vec<Item>,
}

#[derive(Debug, Deserialize)]
struct Item {
title: String,
link: String,
#[serde(rename = "pubDate")]
pub_date: String,
enclosure: Option<Enclosure>,
}

#[derive(Debug, Deserialize)]
struct Enclosure {
url: String,
length: String,
#[serde(rename = "type")]
typ: String,
}

group.bench_function("quick_xml_serde", |b| {
b.iter(|| {
let rss: Rss = quick_xml::de::from_str(SOURCE).unwrap();
assert_eq!(rss.channel.items.len(), 99);
})
});

group.bench_function("xml_rs_serde", |b| {
b.iter(|| {
let rss: Rss = serde_xml_rs::from_str(SOURCE).unwrap();
assert_eq!(rss.channel.items.len(), 99);
});
});
group.finish();
}

criterion_group!(benches, comparison, serde_comparison);
criterion_main!(benches);

0 comments on commit d96c5c9

Please sign in to comment.