mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-14 13:31:22 +00:00
Add an example of the performance
API
This commit is contained in:
51
examples/performance/src/lib.rs
Normal file
51
examples/performance/src/lib.rs
Normal file
@ -0,0 +1,51 @@
|
||||
#![feature(proc_macro)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate humantime;
|
||||
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// Like with the `dom` example this block will eventually be auto-generated, but
|
||||
// for now we can write it by hand to get by!
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
type Performance;
|
||||
static performance: Performance;
|
||||
#[wasm_bindgen(method)]
|
||||
fn now(this: &Performance) -> f64;
|
||||
#[wasm_bindgen(method, getter)]
|
||||
fn timing(this: &Performance) -> PerformanceTiming;
|
||||
|
||||
type PerformanceTiming;
|
||||
#[wasm_bindgen(method, getter)]
|
||||
fn requestStart(this: &PerformanceTiming) -> f64;
|
||||
#[wasm_bindgen(method, getter)]
|
||||
fn responseEnd(this: &PerformanceTiming) -> f64;
|
||||
|
||||
#[wasm_bindgen(namespace = console)]
|
||||
fn log(a: &str);
|
||||
}
|
||||
|
||||
macro_rules! println {
|
||||
($($t:tt)*) => (console::log(&format_args!($($t)*).to_string()))
|
||||
}
|
||||
|
||||
// Called by our JS entry point to run the example
|
||||
#[wasm_bindgen]
|
||||
pub fn run() {
|
||||
println!("the current time is {}", performance.now());
|
||||
|
||||
let start = perf_to_system(performance.timing().requestStart());
|
||||
let end = perf_to_system(performance.timing().responseEnd());
|
||||
|
||||
println!("request started at {}", humantime::format_rfc3339(start));
|
||||
println!("request ended at {}", humantime::format_rfc3339(end));
|
||||
}
|
||||
|
||||
fn perf_to_system(amt: f64) -> SystemTime {
|
||||
let secs = (amt as u64) / 1_000;
|
||||
let nanos = ((amt as u32) % 1_000) * 1_000_000;
|
||||
UNIX_EPOCH + Duration::new(secs, nanos)
|
||||
}
|
Reference in New Issue
Block a user