forked from tafia/quick-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update comparison crate to criterion as well
- Loading branch information
Showing
3 changed files
with
90 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |