feat: add container timing outputs
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
extern crate core;
|
extern crate core;
|
||||||
|
|
||||||
|
use std::time::SystemTime;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
||||||
@@ -17,17 +18,23 @@ pub struct MyContainer {
|
|||||||
|
|
||||||
impl container::Container for MyContainer {
|
impl container::Container for MyContainer {
|
||||||
fn fetch(&mut self, params: &str) -> String {
|
fn fetch(&mut self, params: &str) -> String {
|
||||||
|
let t1 = SystemTime::now();
|
||||||
if self.fetch_invoked_count > 10 {
|
if self.fetch_invoked_count > 10 {
|
||||||
return FnResult::fail("Fetch invoke count cannot more than 10").to_json();
|
return FnResult::fail("Fetch invoke count cannot more than 10").to_json();
|
||||||
}
|
}
|
||||||
self.fetch_invoked_count += 1;
|
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");
|
wit_bindgen_wasmtime::import!("../exports.wit");
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
let main_t1 = SystemTime::now();
|
||||||
use exports::*;
|
use exports::*;
|
||||||
let wasm = "../engine/target/wasm32-unknown-unknown/debug/engine.wasm";
|
let wasm = "../engine/target/wasm32-unknown-unknown/debug/engine.wasm";
|
||||||
let (exports, mut store) = instantiate(
|
let (exports, mut store) = instantiate(
|
||||||
@@ -35,6 +42,7 @@ fn main() -> Result<()> {
|
|||||||
|linker| container::add_to_linker(linker, |cx| -> &mut MyContainer { &mut cx.imports }),
|
|linker| container::add_to_linker(linker, |cx| -> &mut MyContainer { &mut cx.imports }),
|
||||||
|store, module, linker| Exports::instantiate(store, module, linker, |cx| &mut cx.exports),
|
|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##"
|
let a = exports.eval_javascript(&mut store, r##"
|
||||||
function hi(name) { return "hi: " + name; }
|
function hi(name) { return "hi: " + name; }
|
||||||
let a = [];
|
let a = [];
|
||||||
@@ -54,6 +62,8 @@ fn main() -> Result<()> {
|
|||||||
JSON.stringify(a)
|
JSON.stringify(a)
|
||||||
// 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 = a.expect("error");
|
||||||
let a: Value = match serde_json::from_str(&a) {
|
let a: Value = match serde_json::from_str(&a) {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
@@ -72,6 +82,9 @@ fn main() -> Result<()> {
|
|||||||
// println!("------- {}", v);
|
// println!("------- {}", v);
|
||||||
println!("{}", p);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user