feat: add container timing outputs

This commit is contained in:
2022-07-23 14:44:14 +08:00
parent 46b730e235
commit b908736c1c

View File

@@ -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(())
}