diff --git a/src/engine.rs b/src/engine.rs index 5c3c5a93..104bf436 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -26,6 +26,7 @@ pub fn run(script_path: PathBuf, input_path: PathBuf) -> Result Result = instance + .exports(&mut store) + .into_iter() + .filter(|export| export.clone().into_memory().is_some()) + .map(|export| export.name().to_string()) + .collect(); + + memory_usage = memory_names + .iter() + .map(|name| { + let memory = instance.get_memory(&mut store, name).unwrap(); + memory.size(&store) + }) + .sum(); + let module_result = instance .get_typed_func::<(), (), _>(&mut store, "_start")? .call(&mut store, ()); @@ -71,7 +91,8 @@ pub fn run(script_path: PathBuf, input_path: PathBuf) -> Result FUNCTION_SLEEP_DURATION); + } + + #[test] + fn test_memory_usage_under_threshold() { let function_run_result = run( Path::new("tests/benchmarks/hello_world.wasm").to_path_buf(), Path::new("tests/benchmarks/hello_world.json").to_path_buf(), ) .unwrap(); - assert!(function_run_result.runtime <= Duration::from_millis(5)); + assert_eq!(function_run_result.memory_usage, HELLO_WORLD_MEMORY_USAGE); } #[test] - fn test_runtime_over_threshold() { + fn test_memory_usage_over_threshold() { let function_run_result = run( - Path::new("tests/benchmarks/sleeps.wasm").to_path_buf(), - Path::new("tests/benchmarks/sleeps.json").to_path_buf(), + Path::new("tests/benchmarks/hello_42_pages.wasm").to_path_buf(), + Path::new("tests/benchmarks/hello_42_pages.json").to_path_buf(), ) .unwrap(); - assert!(function_run_result.runtime > Duration::from_millis(5)); + assert_eq!( + function_run_result.memory_usage, + MODIFIED_HELLO_WORLD_MEMORY_USAGE + ); + } + + #[test] + fn test_stack_overflow() { + let function_run_result = run( + Path::new("tests/benchmarks/stack_overflow.wasm").to_path_buf(), + Path::new("tests/benchmarks/stack_overflow.json").to_path_buf(), + ); + + assert!(function_run_result.is_err()); } } diff --git a/src/function_run_result.rs b/src/function_run_result.rs index 417e36d1..a37b5d64 100644 --- a/src/function_run_result.rs +++ b/src/function_run_result.rs @@ -3,14 +3,21 @@ use std::{fmt, time::Duration}; pub struct FunctionRunResult { pub runtime: Duration, + pub memory_usage: u64, pub logs: String, pub output: serde_json::Value, } impl FunctionRunResult { - pub fn new(runtime: Duration, output: serde_json::Value, logs: String) -> Self { + pub fn new( + runtime: Duration, + memory_usage: u64, + output: serde_json::Value, + logs: String, + ) -> Self { FunctionRunResult { runtime, + memory_usage, output, logs, } @@ -22,7 +29,8 @@ impl fmt::Display for FunctionRunResult { let title = " Benchmark Results ".black().on_bright_green(); write!(f, "{}\n\n", title)?; - writeln!(f, "Runtime: {:?}\n", self.runtime)?; + writeln!(f, "Runtime: {:?}", self.runtime)?; + writeln!(f, "Memory Usage: {}KB\n", self.memory_usage * 64)?; writeln!( f, diff --git a/tests/benchmarks/hello_42_pages.json b/tests/benchmarks/hello_42_pages.json new file mode 100644 index 00000000..ecbcdfb3 --- /dev/null +++ b/tests/benchmarks/hello_42_pages.json @@ -0,0 +1,11 @@ +{ + "input": { + "context": { + "suffix": "From Core!" + } + }, + "configuration": { + "message": "From Core!" + }, + "function_api": "hello_world" +} diff --git a/tests/benchmarks/hello_42_pages.wasm b/tests/benchmarks/hello_42_pages.wasm new file mode 100755 index 00000000..361c2814 Binary files /dev/null and b/tests/benchmarks/hello_42_pages.wasm differ diff --git a/tests/benchmarks/hello_world.json b/tests/benchmarks/hello_world.json index 24e55378..ecbcdfb3 100644 --- a/tests/benchmarks/hello_world.json +++ b/tests/benchmarks/hello_world.json @@ -7,5 +7,5 @@ "configuration": { "message": "From Core!" }, - "extension_point": "hello_world" + "function_api": "hello_world" } diff --git a/tests/benchmarks/sleeps.json b/tests/benchmarks/sleeps.json index 24e55378..ecbcdfb3 100644 --- a/tests/benchmarks/sleeps.json +++ b/tests/benchmarks/sleeps.json @@ -7,5 +7,5 @@ "configuration": { "message": "From Core!" }, - "extension_point": "hello_world" + "function_api": "hello_world" } diff --git a/tests/benchmarks/stack_overflow.json b/tests/benchmarks/stack_overflow.json new file mode 100644 index 00000000..ecbcdfb3 --- /dev/null +++ b/tests/benchmarks/stack_overflow.json @@ -0,0 +1,11 @@ +{ + "input": { + "context": { + "suffix": "From Core!" + } + }, + "configuration": { + "message": "From Core!" + }, + "function_api": "hello_world" +} diff --git a/tests/benchmarks/stack_overflow.wasm b/tests/benchmarks/stack_overflow.wasm new file mode 100755 index 00000000..1205de50 Binary files /dev/null and b/tests/benchmarks/stack_overflow.wasm differ