Skip to content

Commit

Permalink
Add criterion and use it to benchmark the creation of elements
Browse files Browse the repository at this point in the history
"cargo bench" runs the benchmark
  • Loading branch information
DanielT committed Aug 14, 2024
1 parent 29db4c5 commit 96dc155
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
5 changes: 5 additions & 0 deletions autosar-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ num-traits = "0.2.18"

[dev-dependencies]
tempfile = "3.8"
criterion = { version = "0.3", features = ["html_reports"]}

[[example]]
name = "demo"

[[bench]]
name = "create_elements"
harness = false
85 changes: 85 additions & 0 deletions autosar-data/benches/create_elements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use autosar_data::*;
use criterion::{criterion_group, criterion_main, Criterion};
use std::fmt::Write;

fn create_elements_a() {
let model = AutosarModel::new();
let _file = model.create_file("file", AutosarVersion::LATEST);
let packages = model
.root_element()
.create_sub_element(ElementName::ArPackages)
.unwrap();
let package = packages
.create_named_sub_element(ElementName::ArPackage, "package")
.unwrap();
let elements = package.create_sub_element(ElementName::Elements).unwrap();

let sub_elem_info = elements.list_valid_sub_elements();

let mut name = String::with_capacity(20);
let mut seq_id = 0;
for ValidSubElementInfo {
element_name,
is_named,
is_allowed,
} in sub_elem_info
{
for _ in 0..4 {
name.clear();
seq_id += 1;
write!(name, "element_{seq_id}").unwrap();
if is_allowed {
if is_named {
elements.create_named_sub_element(element_name, &name).unwrap();
} else {
elements.create_sub_element(element_name).unwrap();
}
}
}
}
}

fn create_elements_b() {
let model = AutosarModel::new();
let _file = model.create_file("file", AutosarVersion::LATEST);
let packages = model
.root_element()
.create_sub_element(ElementName::ArPackages)
.unwrap();
let package = packages
.create_named_sub_element(ElementName::ArPackage, "package")
.unwrap();
let elements = package.create_sub_element(ElementName::Elements).unwrap();

let sub_elem_info = elements.list_valid_sub_elements();

let mut name = String::with_capacity(20);
let mut seq_id = 0;
for _ in 0..4 {
for ValidSubElementInfo {
element_name,
is_named,
is_allowed,
} in &sub_elem_info
{
name.clear();
seq_id += 1;
write!(name, "element_{seq_id}").unwrap();
if *is_allowed {
if *is_named {
elements.create_named_sub_element(*element_name, &name).unwrap();
} else {
elements.create_sub_element(*element_name).unwrap();
}
}
}
}
}

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("create_elements (a)", |b| b.iter(|| create_elements_a()));
c.bench_function("create_elements (b)", |b| b.iter(|| create_elements_b()));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit 96dc155

Please sign in to comment.