forked from tafia/quick-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macrobenches.rs
133 lines (116 loc) · 4.55 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
use criterion::{self, criterion_group, Criterion};
use quick_xml::events::Event;
use quick_xml::Reader;
use quick_xml::Result as XmlResult;
static RPM_PRIMARY: &[u8] = include_bytes!("../tests/documents/rpm_primary.xml");
static RPM_PRIMARY2: &[u8] = include_bytes!("../tests/documents/rpm_primary2.xml");
static RPM_FILELISTS: &[u8] = include_bytes!("../tests/documents/rpm_filelists.xml");
static RPM_OTHER: &[u8] = include_bytes!("../tests/documents/rpm_other.xml");
static LIBREOFFICE_DOCUMENT: &[u8] = include_bytes!("../tests/documents/libreoffice_document.fodt");
static DOCUMENT: &[u8] = include_bytes!("../tests/documents/document.xml");
static TEST_WRITER_INDENT: &[u8] = include_bytes!("../tests/documents/test_writer_indent.xml");
static SAMPLE_1: &[u8] = include_bytes!("../tests/documents/sample_1.xml");
static LINESCORE: &[u8] = include_bytes!("../tests/documents/linescore.xml");
static SAMPLE_RSS: &[u8] = include_bytes!("../tests/documents/sample_rss.xml");
static SAMPLE_NS: &[u8] = include_bytes!("../tests/documents/sample_ns.xml");
static PLAYERS: &[u8] = include_bytes!("../tests/documents/players.xml");
// TODO: read the namespaces too
fn parse_document(doc: &[u8]) -> XmlResult<()> {
let mut r = Reader::from_reader(doc);
loop {
match r.read_event_unbuffered()? {
Event::Start(e) | Event::Empty(e) => {
for attr in e.attributes() {
criterion::black_box(attr?.normalized_value()?);
}
}
Event::Text(e) => {
criterion::black_box(e.unescaped()?);
}
Event::CData(e) => {
criterion::black_box(e.into_inner());
}
Event::End(_) => (),
Event::Eof => break,
_ => (),
}
}
Ok(())
}
pub fn bench_fully_parse_document(c: &mut Criterion) {
let mut group = c.benchmark_group("fully_parse_document");
// long, mix of attributes and text, not much escaping, mix of attribute lengths, some namespaces
group.bench_function("rpm_primary.xml", |b| {
b.iter(|| {
parse_document(RPM_PRIMARY).unwrap();
})
});
// long, mix of attributes and text, not much escaping, mix of attribute lengths, some namespaces
group.bench_function("rpm_primary2.xml", |b| {
b.iter(|| {
parse_document(RPM_PRIMARY2).unwrap();
})
});
// long, mostly medium-length text elements, not much escaping
group.bench_function("rpm_filelists.xml", |b| {
b.iter(|| {
parse_document(RPM_FILELISTS).unwrap();
})
});
// long, mix of attributes and text, lots of escaping (both entity and char literal), long attributes
group.bench_function("rpm_other.xml", |b| {
b.iter(|| {
parse_document(RPM_OTHER).unwrap();
})
});
// long, mix of attributes and text, not much escaping, lots of non-ascii characters, lots of namespaces
group.bench_function("libreoffice_document.fodt", |b| {
b.iter(|| {
parse_document(LIBREOFFICE_DOCUMENT).unwrap();
})
});
// medium length, mostly empty tags, a few short attributes per element, no escaping
group.bench_function("document.xml", |b| {
b.iter(|| {
parse_document(DOCUMENT).unwrap();
})
});
// medium length, lots of namespaces, no escaping
group.bench_function("test_writer_ident.xml", |b| {
b.iter(|| {
parse_document(TEST_WRITER_INDENT).unwrap();
})
});
// short, mix of attributes and text, lots of escapes
group.bench_function("sample_1.xml", |b| {
b.iter(|| {
parse_document(SAMPLE_1).unwrap();
})
});
// medium length, lots of attributes, short attributes, few escapes
group.bench_function("linescore.xml", |b| {
b.iter(|| {
parse_document(LINESCORE).unwrap();
})
});
// short, lots of namespaces, no escapes
group.bench_function("sample_ns.xml", |b| {
b.iter(|| {
parse_document(SAMPLE_NS).unwrap();
})
});
// long, few attributes, mix of attribute lengths, escapes in text content
group.bench_function("sample_rss.xml", |b| {
b.iter(|| {
parse_document(SAMPLE_RSS).unwrap();
})
});
// long, lots of attributes, short attributes, no text, no escapes
group.bench_function("players.xml", |b| {
b.iter(|| {
parse_document(PLAYERS).unwrap();
})
});
group.finish();
}
criterion_group!(benches, bench_fully_parse_document,);