Skip to content

Commit

Permalink
✅ tests: refactor decompile tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Sep 28, 2023
1 parent c18f418 commit dbe5832
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 85 deletions.
48 changes: 48 additions & 0 deletions common/src/testing/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,54 @@ pub fn benchmark(benchmark_name: &str, runs: usize, to_bench: fn()) {
);
}

#[allow(dead_code)]
pub async fn async_bench<F, Fut>(benchmark_name: &str, runs: usize, to_bench: F)
where
F: Fn() -> Fut,
Fut: std::future::Future<Output = ()>, {
let mut time = 0usize;
let mut times = Vec::with_capacity(runs);
let mut max = usize::MIN;
let mut min = usize::MAX;

// warm up
thread::sleep(std::time::Duration::from_secs(2));

for _ in 0..runs {
let start_time = Instant::now();
to_bench().await;
let end_time = start_time.elapsed().as_nanos() as usize;

max = std::cmp::max(max, end_time);
min = std::cmp::min(min, end_time);
time += end_time;
times.push(end_time);
}

let mean = time / runs;
let variance = times
.iter()
.map(|x| {
let x_i64 = *x as i64;
let diff = x_i64 - mean as i64;
diff * diff
})
.sum::<i64>() /
(runs - 1) as i64;
let std_dev = f64::sqrt(variance as f64) as usize;

let _ = io::stdout().write_all(
format!(
" {}:\n {} ± {} per run ( with {} runs ).\n\n",
benchmark_name,
format_nanos(mean),
format_nanos(std_dev),
runs
)
.as_bytes(),
);
}

#[allow(dead_code)]
fn format_nanos(nanos: usize) -> String {
let mut nanos = nanos;
Expand Down
22 changes: 18 additions & 4 deletions core/src/decompile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod util;

use crate::decompile::{
analyzers::{solidity::analyze_sol, yul::analyze_yul},
out::abi::build_abi,
out::{abi::build_abi, solidity::build_solidity_output, yul::build_yul_output},
resolve::*,
util::*,
};
Expand Down Expand Up @@ -591,15 +591,29 @@ pub async fn decompile(
logger.info("symbolic execution completed.");
logger.info("building decompilation output.");

let abi = build_abi(&args, analyzed_functions, &mut trace, decompile_call)?;
let abi = build_abi(&args, analyzed_functions.clone(), &mut trace, decompile_call)?;
trace.display();
logger.debug(&format!("decompilation completed in {:?}.", now.elapsed()));

Ok(DecompileResult {
source: if args.include_solidity {
None
Some(build_solidity_output(
&args,
&abi,
analyzed_functions,
all_resolved_errors,
all_resolved_events,
&mut trace,
decompile_call,
)?)
} else if args.include_yul {
None
Some(build_yul_output(
&args,
analyzed_functions,
all_resolved_events,
&mut trace,
decompile_call,
)?)
} else {
None
},
Expand Down
Loading

0 comments on commit dbe5832

Please sign in to comment.