diff --git a/tests/diff.rs b/tests/diff.rs index f53109a..c880a72 100644 --- a/tests/diff.rs +++ b/tests/diff.rs @@ -1,10 +1,9 @@ -use parity_wasm::elements; use std::{ fs, io::{self, Read, Write}, path::{Path, PathBuf}, }; -use wasm_instrument as instrument; +use wasm_instrument::{self as instrument, parity_wasm::elements}; use wasmparser::validate; fn slurp>(path: P) -> io::Result> { diff --git a/tests/overhead.rs b/tests/overhead.rs new file mode 100644 index 0000000..066f34c --- /dev/null +++ b/tests/overhead.rs @@ -0,0 +1,73 @@ +use std::{ + fs::{read, read_dir}, + path::PathBuf, +}; +use wasm_instrument::{ + gas_metering, inject_stack_limiter, + parity_wasm::{deserialize_buffer, elements::Module, serialize}, +}; + +fn fixture_dir() -> PathBuf { + let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + path.push("benches"); + path.push("fixtures"); + path +} + +/// Print the overhead of applying gas metering, stack height limiting or both. +/// +/// Use `cargo test print_overhead -- --nocapture`. +#[test] +fn print_size_overhead() { + let mut results: Vec<_> = read_dir(fixture_dir()) + .unwrap() + .map(|entry| { + let entry = entry.unwrap(); + let (orig_len, orig_module) = { + let bytes = read(&entry.path()).unwrap(); + let len = bytes.len(); + let module: Module = deserialize_buffer(&bytes).unwrap(); + (len, module) + }; + let (gas_metering_len, gas_module) = { + let module = gas_metering::inject( + orig_module.clone(), + &gas_metering::ConstantCostRules::default(), + "env", + ) + .unwrap(); + let bytes = serialize(module.clone()).unwrap(); + let len = bytes.len(); + (len, module) + }; + let stack_height_len = { + let module = inject_stack_limiter(orig_module, 128).unwrap(); + let bytes = serialize(module).unwrap(); + bytes.len() + }; + let both_len = { + let module = inject_stack_limiter(gas_module, 128).unwrap(); + let bytes = serialize(module).unwrap(); + bytes.len() + }; + + let overhead = both_len * 100 / orig_len; + + ( + overhead, + format!( + "{:30}: orig = {:4} kb, gas_metering = {} %, stack_limiter = {} %, both = {} %", + entry.file_name().to_str().unwrap(), + orig_len / 1024, + gas_metering_len * 100 / orig_len, + stack_height_len * 100 / orig_len, + overhead, + ), + ) + }) + .collect(); + results.sort_unstable_by(|a, b| b.0.cmp(&a.0)); + for entry in results { + println!("{}", entry.1); + } +}