Add test to measure size overhead (#8)

* Don't use parity-wasm directly

* Add test that output size over head of metering
This commit is contained in:
Alexander Theißen
2022-01-31 14:50:32 +01:00
committed by GitHub
parent ff68bee449
commit 4548a86329
2 changed files with 74 additions and 2 deletions
+1 -2
View File
@@ -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<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
+73
View File
@@ -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);
}
}