-
Notifications
You must be signed in to change notification settings - Fork 223
/
macrobenches.rs
211 lines (192 loc) · 7.87 KB
/
macrobenches.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use criterion::{self, criterion_group, criterion_main, Criterion, Throughput};
use quick_xml::events::Event;
use quick_xml::reader::{NsReader, Reader};
use quick_xml::Result as XmlResult;
static RPM_PRIMARY: &str = include_str!("../tests/documents/rpm_primary.xml");
static RPM_PRIMARY2: &str = include_str!("../tests/documents/rpm_primary2.xml");
static RPM_FILELISTS: &str = include_str!("../tests/documents/rpm_filelists.xml");
static RPM_OTHER: &str = include_str!("../tests/documents/rpm_other.xml");
static LIBREOFFICE_DOCUMENT: &str = include_str!("../tests/documents/libreoffice_document.fodt");
static DOCUMENT: &str = include_str!("../tests/documents/document.xml");
static TEST_WRITER_INDENT: &str = include_str!("../tests/documents/test_writer_indent.xml");
static SAMPLE_1: &str = include_str!("../tests/documents/sample_1.xml");
static LINESCORE: &str = include_str!("../tests/documents/linescore.xml");
static SAMPLE_RSS: &str = include_str!("../tests/documents/sample_rss.xml");
static SAMPLE_NS: &str = include_str!("../tests/documents/sample_ns.xml");
static PLAYERS: &str = include_str!("../tests/documents/players.xml");
static INPUTS: &[(&str, &str)] = &[
// long, mix of attributes and text, not much escaping, mix of attribute lengths, some namespaces
("rpm_primary.xml", RPM_PRIMARY),
// long, mix of attributes and text, not much escaping, mix of attribute lengths, some namespaces
("rpm_primary2.xml", RPM_PRIMARY2),
// long, mostly medium-length text elements, not much escaping
("rpm_filelists.xml", RPM_FILELISTS),
// long, mix of attributes and text, lots of escaping (both entity and char literal), long attributes
("rpm_other.xml", RPM_OTHER),
// long, mix of attributes and text, not much escaping, lots of non-ascii characters, lots of namespaces
("libreoffice_document.fodt", LIBREOFFICE_DOCUMENT),
// medium length, mostly empty tags, a few short attributes per element, no escaping
("document.xml", DOCUMENT),
// medium length, lots of namespaces, no escaping
("test_writer_ident.xml", TEST_WRITER_INDENT),
// short, mix of attributes and text, lots of escapes
("sample_1.xml", SAMPLE_1),
// medium length, lots of attributes, short attributes, few escapes
("linescore.xml", LINESCORE),
// short, lots of namespaces, no escapes
("sample_ns.xml", SAMPLE_NS),
// long, few attributes, mix of attribute lengths, escapes in text content
("sample_rss.xml", SAMPLE_RSS),
// long, lots of attributes, short attributes, no text, no escapes
("players.xml", PLAYERS),
];
fn parse_document_from_str(doc: &str) -> XmlResult<()> {
let mut r = Reader::from_str(doc);
loop {
match criterion::black_box(r.read_event()?) {
Event::Start(e) | Event::Empty(e) => {
for attr in e.attributes() {
criterion::black_box(attr?.decode_and_normalize_value(&r)?);
}
}
Event::Text(e) => {
criterion::black_box(e.unescape()?);
}
Event::CData(e) => {
criterion::black_box(e.into_inner());
}
Event::End(_) => (),
Event::Eof => break,
_ => (),
}
}
Ok(())
}
fn parse_document_from_bytes(doc: &[u8]) -> XmlResult<()> {
let mut r = Reader::from_reader(doc);
let mut buf = Vec::new();
loop {
match criterion::black_box(r.read_event_into(&mut buf)?) {
Event::Start(e) | Event::Empty(e) => {
for attr in e.attributes() {
criterion::black_box(attr?.decode_and_normalize_value(&r)?);
}
}
Event::Text(e) => {
criterion::black_box(e.unescape()?);
}
Event::CData(e) => {
criterion::black_box(e.into_inner());
}
Event::End(_) => (),
Event::Eof => break,
_ => (),
}
buf.clear();
}
Ok(())
}
fn parse_document_from_str_with_namespaces(doc: &str) -> XmlResult<()> {
let mut r = NsReader::from_str(doc);
loop {
match criterion::black_box(r.read_resolved_event()?) {
(resolved_ns, Event::Start(e) | Event::Empty(e)) => {
criterion::black_box(resolved_ns);
for attr in e.attributes() {
criterion::black_box(attr?.decode_and_normalize_value(&r)?);
}
}
(resolved_ns, Event::Text(e)) => {
criterion::black_box(e.unescape()?);
criterion::black_box(resolved_ns);
}
(resolved_ns, Event::CData(e)) => {
criterion::black_box(e.into_inner());
criterion::black_box(resolved_ns);
}
(_, Event::End(_)) => (),
(_, Event::Eof) => break,
_ => (),
}
}
Ok(())
}
fn parse_document_from_bytes_with_namespaces(doc: &[u8]) -> XmlResult<()> {
let mut r = NsReader::from_reader(doc);
let mut buf = Vec::new();
loop {
match criterion::black_box(r.read_resolved_event_into(&mut buf)?) {
(resolved_ns, Event::Start(e) | Event::Empty(e)) => {
criterion::black_box(resolved_ns);
for attr in e.attributes() {
criterion::black_box(attr?.decode_and_normalize_value(&r)?);
}
}
(resolved_ns, Event::Text(e)) => {
criterion::black_box(e.unescape()?);
criterion::black_box(resolved_ns);
}
(resolved_ns, Event::CData(e)) => {
criterion::black_box(e.into_inner());
criterion::black_box(resolved_ns);
}
(_, Event::End(_)) => (),
(_, Event::Eof) => break,
_ => (),
}
buf.clear();
}
Ok(())
}
/// Just parse - no decoding overhead
pub fn bench_parse_document_nocopy(c: &mut Criterion) {
let mut group = c.benchmark_group("parse_document_nocopy");
for (id, data) in INPUTS.iter() {
group.throughput(Throughput::Bytes(data.len() as u64));
group.bench_with_input(*id, *data, |b, input| {
b.iter(|| parse_document_from_str(input).unwrap())
});
}
group.finish();
}
/// Decode into a buffer, then parse
pub fn bench_decode_and_parse_document(c: &mut Criterion) {
let mut group = c.benchmark_group("decode_and_parse_document");
for (id, data) in INPUTS.iter() {
group.throughput(Throughput::Bytes(data.len() as u64));
group.bench_with_input(*id, *data, |b, input| {
b.iter(|| parse_document_from_bytes(input.as_bytes()).unwrap())
});
}
group.finish();
}
/// Just parse - no decoding overhead - including namespaces
pub fn bench_parse_document_nocopy_with_namespaces(c: &mut Criterion) {
let mut group = c.benchmark_group("parse_document_nocopy_with_namespaces");
for (id, data) in INPUTS.iter() {
group.throughput(Throughput::Bytes(data.len() as u64));
group.bench_with_input(*id, *data, |b, input| {
b.iter(|| parse_document_from_str_with_namespaces(input).unwrap())
});
}
group.finish();
}
/// Decode into a buffer, then parse - including namespaces
pub fn bench_decode_and_parse_document_with_namespaces(c: &mut Criterion) {
let mut group = c.benchmark_group("decode_and_parse_document_with_namespaces");
for (id, data) in INPUTS.iter() {
group.throughput(Throughput::Bytes(data.len() as u64));
group.bench_with_input(*id, *data, |b, input| {
b.iter(|| parse_document_from_bytes_with_namespaces(input.as_bytes()).unwrap())
});
}
group.finish();
}
criterion_group!(
benches,
bench_parse_document_nocopy,
bench_decode_and_parse_document,
bench_parse_document_nocopy_with_namespaces,
bench_decode_and_parse_document_with_namespaces,
);
criterion_main!(benches);