extern crate dotscope;
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use dotscope::metadata::security::PermissionSet;
use std::{fs, hint::black_box, path::PathBuf};
fn bench_permission_set_parse(c: &mut Criterion) {
let path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WB_DeclSecurity_1.bin");
let data = fs::read(&path).expect("Failed to read security declaration file");
let file_size = data.len();
let mut group = c.benchmark_group("permission_set");
group.throughput(Throughput::Bytes(file_size as u64));
group.bench_function("parse_binary", |b| {
b.iter(|| {
let perm_set = PermissionSet::new(black_box(&data)).unwrap();
black_box(perm_set)
});
});
group.finish();
}
fn bench_permission_set_minimal(c: &mut Criterion) {
let data = [
0x2E, 0x01, 0x09, b'T', b'e', b's', b't', b'C', b'l', b'a', b's', b's', 0x01, 0x00, ];
c.bench_function("permission_set_minimal", |b| {
b.iter(|| {
let perm_set = PermissionSet::new(black_box(&data)).unwrap();
black_box(perm_set)
});
});
}
fn bench_permission_set_xml_minimal(c: &mut Criterion) {
let data = b"<PermissionSet class=\"System.Security.PermissionSet\" version=\"1\"/>";
c.bench_function("permission_set_xml_minimal", |b| {
b.iter(|| {
let perm_set = PermissionSet::new(black_box(data)).unwrap();
black_box(perm_set)
});
});
}
fn bench_permission_set_xml_with_permission(c: &mut Criterion) {
let data = br#"<PermissionSet class="System.Security.PermissionSet" version="1">
<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib" version="1" Flags="UnmanagedCode"/>
</PermissionSet>"#;
c.bench_function("permission_set_xml_with_permission", |b| {
b.iter(|| {
let perm_set = PermissionSet::new(black_box(data)).unwrap();
black_box(perm_set)
});
});
}
fn bench_permission_set_repeated(c: &mut Criterion) {
let path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WB_DeclSecurity_1.bin");
let data = fs::read(&path).expect("Failed to read security declaration file");
let mut group = c.benchmark_group("permission_set");
group.throughput(Throughput::Elements(100));
group.bench_function("parse_100x", |b| {
b.iter(|| {
for _ in 0..100 {
let perm_set = PermissionSet::new(black_box(&data)).unwrap();
black_box(perm_set);
}
});
});
group.finish();
}
criterion_group!(
benches,
bench_permission_set_parse,
bench_permission_set_minimal,
bench_permission_set_xml_minimal,
bench_permission_set_xml_with_permission,
bench_permission_set_repeated,
);
criterion_main!(benches);