diff --git a/benches/bench.rs b/benches/bench.rs index 0ba781fa..e801b335 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -2,15 +2,14 @@ use criterion::{self, criterion_group, criterion_main, Criterion}; use quick_xml::events::Event; use quick_xml::Reader; -fn quick_xml_benchmark(c: &mut Criterion) { - let mut group = c.benchmark_group("metadata_parse"); +static SAMPLE: &[u8] = include_bytes!("../tests/sample_rss.xml"); +static PLAYERS: &[u8] = include_bytes!("../tests/players.xml"); - let sample: &[u8] = include_bytes!("../tests/sample_rss.xml"); - let players: &[u8] = include_bytes!("../tests/players.xml"); - - group.bench_function("quick_xml_normal", |b| { +fn quick_xml_normal(c: &mut Criterion) { + let mut group = c.benchmark_group("quick_xml_normal"); + group.bench_function("untrimmed", |b| { b.iter(|| { - let mut r = Reader::from_reader(sample); + let mut r = Reader::from_reader(SAMPLE); r.check_end_names(false).check_comments(false); let mut count = criterion::black_box(0); let mut buf = Vec::new(); @@ -26,9 +25,9 @@ fn quick_xml_benchmark(c: &mut Criterion) { }) }); - group.bench_function("quick_xml_normal_trimmed", |b| { + group.bench_function("trimmed", |b| { b.iter(|| { - let mut r = Reader::from_reader(sample); + let mut r = Reader::from_reader(SAMPLE); r.check_end_names(false) .check_comments(false) .trim_text(true); @@ -45,10 +44,14 @@ fn quick_xml_benchmark(c: &mut Criterion) { assert_eq!(count, 1550); }); }); + group.finish(); +} - group.bench_function("quick_xml_namespaced", |b| { +fn quick_xml_namespaced(c: &mut Criterion) { + let mut group = c.benchmark_group("quick_xml_namespaced"); + group.bench_function("untrimmed", |b| { b.iter(|| { - let mut r = Reader::from_reader(sample); + let mut r = Reader::from_reader(SAMPLE); r.check_end_names(false).check_comments(false); let mut count = criterion::black_box(0); let mut buf = Vec::new(); @@ -65,9 +68,9 @@ fn quick_xml_benchmark(c: &mut Criterion) { }); }); - group.bench_function("quick_xml_namespaced_trimmed", |b| { + group.bench_function("trimmed", |b| { b.iter(|| { - let mut r = Reader::from_reader(sample); + let mut r = Reader::from_reader(SAMPLE); r.check_end_names(false) .check_comments(false) .trim_text(true); @@ -85,11 +88,15 @@ fn quick_xml_benchmark(c: &mut Criterion) { assert_eq!(count, 1550); }); }); + group.finish(); +} - group.bench_function("quick_xml_escaped", |b| { +fn quick_xml_escaped(c: &mut Criterion) { + let mut group = c.benchmark_group("quick_xml_escaped"); + group.bench_function("untrimmed", |b| { b.iter(|| { let mut buf = Vec::new(); - let mut r = Reader::from_reader(sample); + let mut r = Reader::from_reader(SAMPLE); r.check_end_names(false).check_comments(false); let mut count = criterion::black_box(0); let mut nbtxt = criterion::black_box(0); @@ -113,10 +120,10 @@ fn quick_xml_benchmark(c: &mut Criterion) { }); }); - group.bench_function("quick_xml_escaped_trimmed", |b| { + group.bench_function("trimmed", |b| { b.iter(|| { let mut buf = Vec::new(); - let mut r = Reader::from_reader(sample); + let mut r = Reader::from_reader(SAMPLE); r.check_end_names(false) .check_comments(false) .trim_text(true); @@ -141,8 +148,12 @@ fn quick_xml_benchmark(c: &mut Criterion) { assert_eq!(nbtxt, 50261); }); }); + group.finish(); +} - group.bench_function("quick_xml_one_text_event", |b| { +fn quick_xml_one_event(c: &mut Criterion) { + let mut group = c.benchmark_group("quick_xml_one_event"); + group.bench_function("text_event", |b| { let src = "Hello world!".repeat(512 / 12).into_bytes(); let mut buf = Vec::with_capacity(1024); b.iter(|| { @@ -160,7 +171,7 @@ fn quick_xml_benchmark(c: &mut Criterion) { }) }); - group.bench_function("quick_xml_one_start_event_trimmed", |b| { + group.bench_function("start_event_trimmed", |b| { let src = format!(r#""#, "world".repeat(512 / 5)).into_bytes(); let mut buf = Vec::with_capacity(1024); b.iter(|| { @@ -180,7 +191,7 @@ fn quick_xml_benchmark(c: &mut Criterion) { }) }); - group.bench_function("quick_xml_one_comment_event_trimmed", |b| { + group.bench_function("comment_event_trimmed", |b| { let src = format!(r#""#, "world".repeat(512 / 5)).into_bytes(); let mut buf = Vec::with_capacity(1024); b.iter(|| { @@ -200,7 +211,7 @@ fn quick_xml_benchmark(c: &mut Criterion) { }) }); - group.bench_function("quick_xml_one_cdata_event_trimmed", |b| { + group.bench_function("cdata_event_trimmed", |b| { let src = format!(r#""#, "world".repeat(512 / 5)).into_bytes(); let mut buf = Vec::with_capacity(1024); b.iter(|| { @@ -219,10 +230,14 @@ fn quick_xml_benchmark(c: &mut Criterion) { assert_eq!(nbtxt, 518); }) }); + group.finish(); +} - group.bench_function("quick_xml_iter_attributes", |b| { +fn quick_xml_attributes(c: &mut Criterion) { + let mut group = c.benchmark_group("quick_xml_attributes"); + group.bench_function("iter_attributes", |b| { b.iter(|| { - let mut r = Reader::from_reader(players); + let mut r = Reader::from_reader(PLAYERS); r.check_end_names(false).check_comments(false); let mut count = criterion::black_box(0); let mut buf = Vec::new(); @@ -243,9 +258,9 @@ fn quick_xml_benchmark(c: &mut Criterion) { }) }); - group.bench_function("quick_xml_iter_attributes_no_checks", |b| { + group.bench_function("iter_attributes_no_checks", |b| { b.iter(|| { - let mut r = Reader::from_reader(players); + let mut r = Reader::from_reader(PLAYERS); r.check_end_names(false).check_comments(false); let mut count = criterion::black_box(0); let mut buf = Vec::new(); @@ -266,9 +281,9 @@ fn quick_xml_benchmark(c: &mut Criterion) { }) }); - group.bench_function("quick_xml_try_get_attribute", |b| { + group.bench_function("try_get_attribute", |b| { b.iter(|| { - let mut r = Reader::from_reader(players); + let mut r = Reader::from_reader(PLAYERS); r.check_end_names(false).check_comments(false); let mut count = criterion::black_box(0); let mut buf = Vec::new(); @@ -293,7 +308,15 @@ fn quick_xml_benchmark(c: &mut Criterion) { assert_eq!(count, 150); }) }); + group.finish(); } -criterion_group!(benches, quick_xml_benchmark); +criterion_group!( + benches, + quick_xml_normal, + quick_xml_escaped, + quick_xml_namespaced, + quick_xml_one_event, + quick_xml_attributes +); criterion_main!(benches);