use std::hint::black_box;
use config_disassembler::xml::build_xml_string;
use config_disassembler::xml::parsers::{
extract_xml_declaration_from_raw, parse_unique_id_element, parse_xml_from_str,
short_hash_for_element,
};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
mod support;
const SIZES: [usize; 2] = [1_000, 2_000];
const FILE_NAME: &str = "Bench.permissionset-meta.xml";
fn parse(c: &mut Criterion) {
let mut group = c.benchmark_group("parse_xml_from_str");
for n in SIZES {
let xml = support::permission_set_xml(n);
group.throughput(Throughput::Bytes(xml.len() as u64));
group.bench_with_input(BenchmarkId::from_parameter(n), &xml, |b, xml| {
b.iter(|| parse_xml_from_str(black_box(xml), FILE_NAME))
});
}
group.finish();
}
fn unique_ids(c: &mut Criterion) {
let mut group = c.benchmark_group("unique_id");
for n in SIZES {
let doc =
parse_xml_from_str(&support::permission_set_xml(n), FILE_NAME).expect("fixture parses");
let siblings = support::largest_sibling_array(&doc).to_vec();
assert_eq!(
siblings.len(),
n,
"expected the n <fieldPermissions> siblings"
);
group.throughput(Throughput::Elements(siblings.len() as u64));
group.bench_with_input(
BenchmarkId::new("configured_field", n),
&siblings,
|b, siblings| {
b.iter(|| {
for el in siblings {
black_box(parse_unique_id_element(
el,
Some(support::PERMISSION_SET_UNIQUE_IDS),
));
}
})
},
);
group.bench_with_input(
BenchmarkId::new("hash_fallback", n),
&siblings,
|b, siblings| {
b.iter(|| {
for el in siblings {
black_box(short_hash_for_element(el));
}
})
},
);
}
group.finish();
}
fn parse_shards(c: &mut Criterion) {
let mut group = c.benchmark_group("parse_shards");
for n in SIZES {
let shards = support::field_permission_shards(n);
group.throughput(Throughput::Elements(n as u64));
group.bench_with_input(BenchmarkId::from_parameter(n), &shards, |b, shards| {
b.iter(|| {
for shard in shards {
black_box(parse_xml_from_str(black_box(shard), FILE_NAME));
black_box(extract_xml_declaration_from_raw(black_box(shard)));
}
})
});
}
group.finish();
}
fn build(c: &mut Criterion) {
let mut group = c.benchmark_group("build_xml_string");
for n in SIZES {
let xml = support::permission_set_xml(n);
let doc = parse_xml_from_str(&xml, FILE_NAME).expect("fixture parses");
group.throughput(Throughput::Bytes(xml.len() as u64));
group.bench_with_input(BenchmarkId::from_parameter(n), &doc, |b, doc| {
b.iter(|| build_xml_string(black_box(doc)))
});
}
group.finish();
}
criterion_group!(benches, parse, parse_shards, unique_ids, build);
criterion_main!(benches);