From b908736c1c3d403356e7dbeb007b476d7c6479ca Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 23 Jul 2022 14:44:14 +0800 Subject: [PATCH] feat: add container timing outputs --- __wasm/wit-bindgen-sample/container/src/main.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/__wasm/wit-bindgen-sample/container/src/main.rs b/__wasm/wit-bindgen-sample/container/src/main.rs index 34536d3..1e51b87 100644 --- a/__wasm/wit-bindgen-sample/container/src/main.rs +++ b/__wasm/wit-bindgen-sample/container/src/main.rs @@ -1,5 +1,6 @@ extern crate core; +use std::time::SystemTime; use anyhow::Result; use serde_json::Value; use wasmtime::{Config, Engine, Instance, Linker, Module, Store}; @@ -17,17 +18,23 @@ pub struct MyContainer { impl container::Container for MyContainer { fn fetch(&mut self, params: &str) -> String { + let t1 = SystemTime::now(); if self.fetch_invoked_count > 10 { return FnResult::fail("Fetch invoke count cannot more than 10").to_json(); } self.fetch_invoked_count += 1; - fn_fetch::do_fetch(params).to_json() + let result = fn_fetch::do_fetch(params).to_json(); + let t2 = SystemTime::now(); + let e = t2.duration_since(t1).expect("get duration error"); + println!("+ fetch: {}, time cost: {}ms", params, e.as_millis()); + result } } wit_bindgen_wasmtime::import!("../exports.wit"); fn main() -> Result<()> { + let main_t1 = SystemTime::now(); use exports::*; let wasm = "../engine/target/wasm32-unknown-unknown/debug/engine.wasm"; let (exports, mut store) = instantiate( @@ -35,6 +42,7 @@ fn main() -> Result<()> { |linker| container::add_to_linker(linker, |cx| -> &mut MyContainer { &mut cx.imports }), |store, module, linker| Exports::instantiate(store, module, linker, |cx| &mut cx.exports), )?; + let eval_t1 = SystemTime::now(); let a = exports.eval_javascript(&mut store, r##" function hi(name) { return "hi: " + name; } let a = []; @@ -54,6 +62,8 @@ fn main() -> Result<()> { JSON.stringify(a) // a "##); + let eval_e = SystemTime::now().duration_since(eval_t1).expect("get duration error"); + println!("+ eval cost: {}ms", eval_e.as_millis()); let a = a.expect("error"); let a: Value = match serde_json::from_str(&a) { Ok(a) => a, @@ -72,6 +82,9 @@ fn main() -> Result<()> { // println!("------- {}", v); println!("{}", p); + let main_e = SystemTime::now().duration_since(main_t1).expect("get duration error"); + println!("+ total cost (-eval): {}ms", main_e.as_millis() - eval_e.as_millis()); + println!("+ total cost: {}ms", main_e.as_millis()); Ok(()) }