diff options
-rw-r--r-- | BUILD.gn | 1 | ||||
-rw-r--r-- | js/globals.ts | 3 | ||||
-rw-r--r-- | js/performance.ts | 29 | ||||
-rw-r--r-- | js/performance_test.ts | 10 | ||||
-rw-r--r-- | js/unit_tests.ts | 1 | ||||
-rw-r--r-- | src/msg.fbs | 10 | ||||
-rw-r--r-- | src/ops.rs | 28 |
7 files changed, 79 insertions, 3 deletions
@@ -104,6 +104,7 @@ ts_sources = [ "js/util.ts", "js/workers.ts", "js/write_file.ts", + "js/performance.ts", "tsconfig.json", # Listing package.json and yarn.lock as sources ensures the bundle is rebuilt diff --git a/js/globals.ts b/js/globals.ts index e45aee9e2..9e7d20bcc 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -21,6 +21,7 @@ import * as timers from "./timers"; import * as url from "./url"; import * as urlSearchParams from "./url_search_params"; import * as workers from "./workers"; +import * as performanceUtil from "./performance"; // These imports are not exposed and therefore are fine to just import the // symbols required. @@ -93,4 +94,6 @@ export type TextEncoder = textEncoding.TextEncoder; window.TextDecoder = textEncoding.TextDecoder; export type TextDecoder = textEncoding.TextDecoder; +window.performance = new performanceUtil.Performance(); + window.workerMain = workers.workerMain; diff --git a/js/performance.ts b/js/performance.ts new file mode 100644 index 000000000..73378b15c --- /dev/null +++ b/js/performance.ts @@ -0,0 +1,29 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import * as msg from "gen/msg_generated"; +import { sendSync } from "./dispatch"; +import * as flatbuffers from "./flatbuffers"; +import { assert } from "./util"; + +export class Performance { + timeOrigin = 0; + + constructor() { + this.timeOrigin = new Date().getTime(); + } + + /** Returns a current time from Deno's start + * + * const t = performance.now(); + * console.log(`${t} ms since start!`); + */ + now(): number { + const builder = flatbuffers.createBuilder(); + msg.Now.startNow(builder); + const inner = msg.Now.endNow(builder); + const baseRes = sendSync(builder, msg.Any.Now, inner)!; + assert(msg.Any.NowRes === baseRes.innerType()); + const res = new msg.NowRes(); + assert(baseRes.inner(res) != null); + return res.time().toFloat64() - this.timeOrigin; + } +} diff --git a/js/performance_test.ts b/js/performance_test.ts new file mode 100644 index 000000000..154dadbbe --- /dev/null +++ b/js/performance_test.ts @@ -0,0 +1,10 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { test, assert } from "./test_util.ts"; + +test(function now() { + const start = performance.now(); + setTimeout(() => { + const end = performance.now(); + assert(end - start >= 10); + }, 10); +}); diff --git a/js/unit_tests.ts b/js/unit_tests.ts index 418779f7f..edad57893 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -43,6 +43,7 @@ import "./truncate_test.ts"; import "./url_test.ts"; import "./url_search_params_test.ts"; import "./write_file_test.ts"; +import "./performance_test.ts"; import "../tools/util_test.ts"; diff --git a/src/msg.fbs b/src/msg.fbs index e13b15daa..06a2d8660 100644 --- a/src/msg.fbs +++ b/src/msg.fbs @@ -59,7 +59,9 @@ union Any { Run, RunRes, RunStatus, - RunStatusRes + RunStatusRes, + Now, + NowRes } enum ErrorKind: byte { @@ -475,4 +477,10 @@ table RunStatusRes { exit_signal: int; } +table Now {} + +table NowRes { + time: uint64; +} + root_type Base; diff --git a/src/ops.rs b/src/ops.rs index 85a731c6a..3826b73ff 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -39,8 +39,7 @@ use std::path::Path; use std::path::PathBuf; use std::process::Command; use std::sync::Arc; -use std::time::UNIX_EPOCH; -use std::time::{Duration, Instant}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use tokio; use tokio::net::TcpListener; use tokio::net::TcpStream; @@ -121,6 +120,7 @@ pub fn dispatch( msg::Any::WorkerPostMessage => op_worker_post_message, msg::Any::Write => op_write, msg::Any::WriteFile => op_write_file, + msg::Any::Now => op_now, _ => panic!(format!( "Unhandled message {}", msg::enum_name_any(inner_type) @@ -173,6 +173,30 @@ pub fn dispatch( (base.sync(), boxed_op) } +fn op_now( + _state: &Arc<IsolateState>, + base: &msg::Base<'_>, + data: libdeno::deno_buf, +) -> Box<Op> { + assert_eq!(data.len(), 0); + let start = SystemTime::now(); + let since_the_epoch = start.duration_since(UNIX_EPOCH).unwrap(); + let time = since_the_epoch.as_secs() as u64 * 1000 + + since_the_epoch.subsec_millis() as u64; + + let builder = &mut FlatBufferBuilder::new(); + let inner = msg::NowRes::create(builder, &msg::NowResArgs { time: time }); + ok_future(serialize_response( + base.cmd_id(), + builder, + msg::BaseArgs { + inner: Some(inner.as_union_value()), + inner_type: msg::Any::NowRes, + ..Default::default() + }, + )) +} + fn op_exit( _config: &Arc<IsolateState>, base: &msg::Base<'_>, |