mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
Add performance.now (#1633)
This commit is contained in:
parent
e5899b14e2
commit
16ed1f2545
7 changed files with 79 additions and 3 deletions
1
BUILD.gn
1
BUILD.gn
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
29
js/performance.ts
Normal file
29
js/performance.ts
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
10
js/performance_test.ts
Normal file
10
js/performance_test.ts
Normal file
|
@ -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);
|
||||
});
|
|
@ -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";
|
||||
|
||||
|
|
10
src/msg.fbs
10
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;
|
||||
|
|
28
src/ops.rs
28
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<'_>,
|
||||
|
|
Loading…
Reference in a new issue