Skip to content

Commit 953c753

Browse files
committed
compare: Put each group into its own file to be able to run them independently using cargo bench --bench <group>
1 parent 6ad3201 commit 953c753

File tree

3 files changed

+97
-121
lines changed

3 files changed

+97
-121
lines changed

compare/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@ serde_derive = "1.0"
2929
pretty_assertions = "1.4"
3030

3131
[[bench]]
32-
name = "bench"
32+
name = "low-level"
33+
harness = false
34+
35+
[[bench]]
36+
name = "serde"
3337
harness = false

compare/benches/bench.rs renamed to compare/benches/low-level.rs

Lines changed: 3 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,11 @@ use criterion::{self, criterion_group, criterion_main, BenchmarkId, Criterion, T
22
use pretty_assertions::assert_eq;
33
use quick_xml::events::Event;
44
use quick_xml::reader::Reader;
5-
use serde::Deserialize;
6-
use serde_xml_rs;
75
use std::hint::black_box;
86
use xml::reader::{EventReader, XmlEvent};
97

10-
static RPM_PRIMARY: &str = include_str!("../../tests/documents/rpm_primary.xml");
11-
static RPM_PRIMARY2: &str = include_str!("../../tests/documents/rpm_primary2.xml");
12-
static RPM_FILELISTS: &str = include_str!("../../tests/documents/rpm_filelists.xml");
13-
static RPM_OTHER: &str = include_str!("../../tests/documents/rpm_other.xml");
14-
static LIBREOFFICE_DOCUMENT: &str = include_str!("../../tests/documents/libreoffice_document.fodt");
15-
static DOCUMENT: &str = include_str!("../../tests/documents/document.xml");
16-
static TEST_WRITER_INDENT: &str = include_str!("../../tests/documents/test_writer_indent.xml");
17-
static SAMPLE_1: &str = include_str!("../../tests/documents/sample_1.xml");
18-
static LINESCORE: &str = include_str!("../../tests/documents/linescore.xml");
19-
static SAMPLE_RSS: &str = include_str!("../../tests/documents/sample_rss.xml");
20-
static SAMPLE_NS: &str = include_str!("../../tests/documents/sample_ns.xml");
21-
static PLAYERS: &str = include_str!("../../tests/documents/players.xml");
22-
23-
static TEST_FILES: [(&str, &str, usize); 12] = [
24-
// long, mix of attributes and text, not much escaping, mix of attribute lengths, some namespaces
25-
("rpm_primary.xml", RPM_PRIMARY, 369),
26-
// long, mix of attributes and text, not much escaping, mix of attribute lengths, some namespaces
27-
("rpm_primary2.xml", RPM_PRIMARY2, 116),
28-
// long, mostly medium-length text elements, not much escaping
29-
("rpm_filelists.xml", RPM_FILELISTS, 184),
30-
// long, mix of attributes and text, lots of escaping (both entity and char literal), long attributes
31-
("rpm_other.xml", RPM_OTHER, 145),
32-
// long, mix of attributes and text, not much escaping, lots of non-ascii characters, lots of namespaces
33-
("libreoffice_document.fodt", LIBREOFFICE_DOCUMENT, 659),
34-
// medium length, mostly empty tags, a few short attributes per element, no escaping
35-
("document.xml", DOCUMENT, 342),
36-
// medium length, lots of namespaces, no escaping
37-
("test_writer_ident.xml", TEST_WRITER_INDENT, 34),
38-
// short, mix of attributes and text, lots of escapes
39-
("sample_1.xml", SAMPLE_1, 15),
40-
// medium length, lots of attributes, short attributes, few escapes
41-
("linescore.xml", LINESCORE, 11),
42-
// short, lots of namespaces, no escapes
43-
("sample_ns.xml", SAMPLE_NS, 11),
44-
// long, few attributes, mix of attribute lengths, escapes in text content
45-
("sample_rss.xml", SAMPLE_RSS, 1550),
46-
// long, lots of attributes, short attributes, no text, no escapes
47-
("players.xml", PLAYERS, 76),
48-
];
8+
mod common;
9+
use common::*;
4910

5011
// Comparison of low-level APIs from several XML libraries
5112
fn low_level_comparison(c: &mut Criterion) {
@@ -283,83 +244,5 @@ fn low_level_comparison(c: &mut Criterion) {
283244
group.finish();
284245
}
285246

286-
/// Runs benchmarks for several XML libraries using serde deserialization
287-
#[allow(dead_code)] // We do not use structs
288-
fn serde_comparison(c: &mut Criterion) {
289-
let mut group = c.benchmark_group("serde");
290-
291-
#[derive(Debug, Deserialize)]
292-
struct Rss<E> {
293-
channel: Channel<E>,
294-
}
295-
296-
#[derive(Debug, Deserialize)]
297-
struct Channel<E> {
298-
title: String,
299-
#[serde(rename = "item", default = "Vec::new")]
300-
items: Vec<Item<E>>,
301-
}
302-
303-
#[derive(Debug, Deserialize)]
304-
struct Item<E> {
305-
title: String,
306-
link: String,
307-
#[serde(rename = "pubDate")]
308-
pub_date: String,
309-
enclosure: Option<E>,
310-
}
311-
312-
#[derive(Debug, Deserialize)]
313-
struct Enclosure {
314-
#[serde(rename = "@url")]
315-
url: String,
316-
317-
#[serde(rename = "@length")]
318-
length: String,
319-
320-
#[serde(rename = "@type")]
321-
typ: String,
322-
}
323-
324-
group.throughput(Throughput::Bytes(SAMPLE_RSS.len() as u64));
325-
326-
group.bench_with_input(
327-
BenchmarkId::new("quick_xml", "sample_rss.xml"),
328-
SAMPLE_RSS,
329-
|b, input| {
330-
b.iter(|| {
331-
let rss: Rss<Enclosure> = black_box(quick_xml::de::from_str(input).unwrap());
332-
assert_eq!(rss.channel.items.len(), 99);
333-
})
334-
},
335-
);
336-
337-
/* NOTE: Most parts of deserializer are not implemented yet, so benchmark failed
338-
group.bench_with_input(BenchmarkId::new("rapid-xml", "sample_rss.xml"), SAMPLE_RSS, |b, input| {
339-
use rapid_xml::de::Deserializer;
340-
use rapid_xml::parser::Parser;
341-
342-
b.iter(|| {
343-
let mut r = Parser::new(input.as_bytes());
344-
let mut de = Deserializer::new(&mut r).unwrap();
345-
let rss = black_box(Rss::deserialize(&mut de).unwrap());
346-
assert_eq!(rss.channel.items.len(), 99);
347-
});
348-
});*/
349-
350-
group.bench_with_input(
351-
BenchmarkId::new("xml_rs", "sample_rss.xml"),
352-
SAMPLE_RSS,
353-
|b, input| {
354-
b.iter(|| {
355-
let rss: Rss<Enclosure> = black_box(serde_xml_rs::from_str(input).unwrap());
356-
assert_eq!(rss.channel.items.len(), 99);
357-
})
358-
},
359-
);
360-
361-
group.finish();
362-
}
363-
364-
criterion_group!(benches, low_level_comparison, serde_comparison);
247+
criterion_group!(benches, low_level_comparison);
365248
criterion_main!(benches);

compare/benches/serde.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use criterion::{self, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
2+
use pretty_assertions::assert_eq;
3+
use serde::Deserialize;
4+
use serde_xml_rs;
5+
use std::hint::black_box;
6+
7+
mod common;
8+
use common::*;
9+
10+
/// Runs benchmarks for several XML libraries using serde deserialization
11+
#[allow(dead_code)] // We do not use structs
12+
fn serde_comparison(c: &mut Criterion) {
13+
let mut group = c.benchmark_group("serde");
14+
15+
#[derive(Debug, Deserialize)]
16+
struct Rss<E> {
17+
channel: Channel<E>,
18+
}
19+
20+
#[derive(Debug, Deserialize)]
21+
struct Channel<E> {
22+
title: String,
23+
#[serde(rename = "item", default = "Vec::new")]
24+
items: Vec<Item<E>>,
25+
}
26+
27+
#[derive(Debug, Deserialize)]
28+
struct Item<E> {
29+
title: String,
30+
link: String,
31+
#[serde(rename = "pubDate")]
32+
pub_date: String,
33+
enclosure: Option<E>,
34+
}
35+
36+
#[derive(Debug, Deserialize)]
37+
struct Enclosure {
38+
#[serde(rename = "@url")]
39+
url: String,
40+
41+
#[serde(rename = "@length")]
42+
length: String,
43+
44+
#[serde(rename = "@type")]
45+
typ: String,
46+
}
47+
48+
group.throughput(Throughput::Bytes(SAMPLE_RSS.len() as u64));
49+
50+
group.bench_with_input(
51+
BenchmarkId::new("quick_xml", "sample_rss.xml"),
52+
SAMPLE_RSS,
53+
|b, input| {
54+
b.iter(|| {
55+
let rss: Rss<Enclosure> = black_box(quick_xml::de::from_str(input).unwrap());
56+
assert_eq!(rss.channel.items.len(), 99);
57+
})
58+
},
59+
);
60+
61+
/* NOTE: Most parts of deserializer are not implemented yet, so benchmark failed
62+
group.bench_with_input(BenchmarkId::new("rapid-xml", "sample_rss.xml"), SAMPLE_RSS, |b, input| {
63+
use rapid_xml::de::Deserializer;
64+
use rapid_xml::parser::Parser;
65+
66+
b.iter(|| {
67+
let mut r = Parser::new(input.as_bytes());
68+
let mut de = Deserializer::new(&mut r).unwrap();
69+
let rss = black_box(Rss::deserialize(&mut de).unwrap());
70+
assert_eq!(rss.channel.items.len(), 99);
71+
});
72+
});*/
73+
74+
group.bench_with_input(
75+
BenchmarkId::new("xml_rs", "sample_rss.xml"),
76+
SAMPLE_RSS,
77+
|b, input| {
78+
b.iter(|| {
79+
let rss: Rss<Enclosure> = black_box(serde_xml_rs::from_str(input).unwrap());
80+
assert_eq!(rss.channel.items.len(), 99);
81+
})
82+
},
83+
);
84+
85+
group.finish();
86+
}
87+
88+
criterion_group!(benches, serde_comparison);
89+
criterion_main!(benches);

0 commit comments

Comments
 (0)