Skip to content

Commit

Permalink
bencher: drop iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Jun 23, 2024
1 parent 23d4e8c commit 831ca2c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 44 deletions.
36 changes: 10 additions & 26 deletions bencher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub type Bench<'a> = (&'a str, &'a Instruction, &'a [(Pubkey, AccountSharedData)

pub struct MolluskComputeUnitBencher<'a> {
benches: Vec<Bench<'a>>,
iterations: u64,
mollusk: Mollusk,
must_pass: bool,
out_dir: PathBuf,
Expand All @@ -25,7 +24,6 @@ impl<'a> MolluskComputeUnitBencher<'a> {
out_dir.push("benches");
Self {
benches: Vec::new(),
iterations: 25, // Default to 25 iterations.
mollusk,
must_pass: false,
out_dir,
Expand All @@ -37,11 +35,6 @@ impl<'a> MolluskComputeUnitBencher<'a> {
self
}

pub fn iterations(mut self, iterations: u64) -> Self {
self.iterations = iterations;
self
}

pub fn must_pass(mut self, must_pass: bool) -> Self {
self.must_pass = must_pass;
self
Expand All @@ -56,28 +49,19 @@ impl<'a> MolluskComputeUnitBencher<'a> {
let bench_results = std::mem::take(&mut self.benches)
.into_iter()
.map(|(name, instruction, accounts)| {
let mut results = vec![];

for _ in 0..self.iterations {
let result = self.mollusk.process_instruction(instruction, accounts);

match result.program_result {
ProgramResult::Success => (),
_ => {
if self.must_pass {
panic!(
"Program execution failed, but `must_pass` was set. Error: \
{:?}",
result.program_result
);
}
let result = self.mollusk.process_instruction(instruction, accounts);
match result.program_result {
ProgramResult::Success => (),
_ => {
if self.must_pass {
panic!(
"Program execution failed, but `must_pass` was set. Error: {:?}",
result.program_result
);
}
}

results.push(result);
}

MolluskComputeUnitBenchResult::new(name, results)
MolluskComputeUnitBenchResult::new(name, result)
})
.collect::<Vec<_>>();
write_results(&self.out_dir, bench_results);
Expand Down
26 changes: 9 additions & 17 deletions bencher/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ use {

pub(crate) struct MolluskComputeUnitBenchResult<'a> {
name: &'a str,
mean: u64,
cus_consumed: u64,
}

impl<'a> MolluskComputeUnitBenchResult<'a> {
pub fn new(name: &'a str, results: Vec<InstructionResult>) -> Self {
let mut runs = results
.iter()
.map(|result| result.compute_units_consumed)
.collect::<Vec<_>>();
runs.sort();

let len = runs.len();
let mean = runs.iter().sum::<u64>() / len as u64;

Self { name, mean }
pub fn new(name: &'a str, result: InstructionResult) -> Self {
let cus_consumed = result.compute_units_consumed;
Self { name, cus_consumed }
}
}

Expand Down Expand Up @@ -52,7 +44,7 @@ pub(crate) fn write_results(out_dir: &Path, results: Vec<MolluskComputeUnitBench
.find(|prev_result| prev_result.name == result.name)
}) {
Some(prev) => {
let delta = result.mean as i64 - prev.mean as i64;
let delta = result.cus_consumed as i64 - prev.cus_consumed as i64;
if delta == 0 {
"--".to_string()
} else {
Expand All @@ -71,7 +63,7 @@ pub(crate) fn write_results(out_dir: &Path, results: Vec<MolluskComputeUnitBench
};
md_table.push_str(&format!(
"| {} | {} | {} |\n",
result.name, result.mean, delta
result.name, result.cus_consumed, delta
));
}

Expand All @@ -87,7 +79,7 @@ fn md_header() -> String {
format!(
r#"#### Compute Units: {}
| Name | Mean | Delta |
| Name | CUs | Delta |
|------|------|-------|
"#,
now
Expand All @@ -104,9 +96,9 @@ fn parse_last_md_table(content: &str) -> Vec<MolluskComputeUnitBenchResult> {

let mut parts = line.split('|').skip(1).map(str::trim);
let name = parts.next().unwrap();
let mean = parts.next().unwrap().parse().unwrap();
let cus_consumed = parts.next().unwrap().parse().unwrap();

results.push(MolluskComputeUnitBenchResult { name, mean });
results.push(MolluskComputeUnitBenchResult { name, cus_consumed });
}

results
Expand Down
1 change: 0 additions & 1 deletion bencher/tests/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ fn test_markdown() {
.bench(("bench4", &instruction, &accounts))
.bench(("bench5", &instruction, &accounts))
.bench(("bench6", &instruction, &accounts))
.iterations(100)
.must_pass(true)
.out_dir("../target/benches")
.execute();
Expand Down

0 comments on commit 831ca2c

Please sign in to comment.