Skip to content

Commit

Permalink
Add simple benchmarks with static obj files
Browse files Browse the repository at this point in the history
  • Loading branch information
Bennett-Petzold committed Jan 23, 2025
1 parent 1c702b5 commit 72fe990
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ path = "src/cli.rs"
name = "testcli"
path = "src/testcli.rs"

[[bench]]
name = "executor_compare"
harness = false

[features]
default = []
consolidated = ["dep:pinned-init"]
Expand All @@ -36,6 +40,7 @@ bare_err_tree = "0.7"
pinned-init = { version = "0.0.9", optional = true, default-features = false, features = ["std"] }

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
# To reduce getter boilerplate
derive-getters = "0.5"
# More efficient map initialization
Expand Down
90 changes: 90 additions & 0 deletions benches/executor_compare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lc3sim_project::{
executors::{core::CoreLC3, populate_from_bin, LC3},
harnesses::{simple::FailIO, sync::step_continue},
};

macro_rules! setup_lc3 {
( $lc3:ident, $path:literal ) => {
|| {
let mut lc3 = $lc3.clone();
populate_from_bin(&mut lc3, include_bytes!("../penn_sim/lc3os.obj").as_slice());
populate_from_bin(&mut lc3, include_bytes!($path).as_slice());
lc3
}
};
}

pub fn create_new(c: &mut Criterion) {
let mut c = c.benchmark_group("create_new");

macro_rules! bench_new {
( $lc3:expr, $name: literal ) => {
c.bench_function($name, |b| {
b.iter($lc3);
});
};
}

bench_new!(CoreLC3::new, "core");
#[cfg(feature = "consolidated")]
bench_new!(
lc3sim_project::executors::consolidated::ConsolidatedLC3::boxed,
"consolidated"
);
}

pub fn load_os(c: &mut Criterion) {
let mut c = c.benchmark_group("load_os");

fn exec_setup<E: LC3>(mut lc3: E) -> E {
populate_from_bin(&mut lc3, include_bytes!("../penn_sim/lc3os.obj").as_slice());
black_box(lc3)
}

macro_rules! bench_load {
( $lc3:expr, $name: literal ) => {
c.bench_function($name, |b| {
b.iter_batched($lc3, exec_setup, criterion::BatchSize::SmallInput);
});
};
}

bench_load!(CoreLC3::new, "core");
#[cfg(feature = "consolidated")]
bench_load!(
lc3sim_project::executors::consolidated::ConsolidatedLC3::boxed,
"consolidated"
);
}

pub fn tiny_loop(c: &mut Criterion) {
let mut c = c.benchmark_group("tiny_loop");

fn exec_loop<E: LC3>(mut lc3: E) {
step_continue(&mut FailIO, &mut lc3).unwrap();
}

macro_rules! bench_loop {
( $lc3:expr, $name: literal ) => {
let lc3 = $lc3;
c.bench_function($name, |b| {
b.iter_batched(
setup_lc3!(lc3, "../test_data/custom/loop.obj"),
exec_loop,
criterion::BatchSize::SmallInput,
);
});
};
}

bench_loop!(CoreLC3::new(), "core");
#[cfg(feature = "consolidated")]
bench_loop!(
lc3sim_project::executors::consolidated::ConsolidatedLC3::boxed(),
"consolidated"
);
}

criterion_group!(speed_compare, create_new, load_os, tiny_loop);
criterion_main!(speed_compare);
Binary file added penn_sim/lc3os.obj
Binary file not shown.
5 changes: 5 additions & 0 deletions test_data/custom/loop.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.ORIG x3000
AND R0, R0, #0
LOOP ADD R0, R0, #1
BRnp LOOP
HALT
Binary file added test_data/custom/loop.obj
Binary file not shown.

0 comments on commit 72fe990

Please sign in to comment.