Skip to content

Commit

Permalink
port cli/ops/performance.rs to json ops
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Aug 23, 2019
1 parent 9125726 commit ea0a571
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 45 deletions.
9 changes: 0 additions & 9 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ union Any {
Metrics,
MetricsRes,
Mkdir,
Now,
NowRes,
Open,
OpenRes,
PermissionRevoke,
Expand Down Expand Up @@ -480,13 +478,6 @@ table RunStatusRes {
exit_signal: int;
}

table Now {}

table NowRes {
seconds: uint64;
subsec_nanos: uint32;
}

table Seek {
rid: uint32;
offset: int;
Expand Down
2 changes: 0 additions & 2 deletions cli/ops/dispatch_flatbuffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use super::fs::{
};
use super::metrics::op_metrics;
use super::os::{op_home_dir, op_set_env, op_start};
use super::performance::op_now;
use super::permissions::{op_permissions, op_revoke_permission};
use super::process::{op_kill, op_run, op_run_status};
use super::workers::{
Expand Down Expand Up @@ -157,7 +156,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
msg::Any::MakeTempDir => Some(op_make_temp_dir),
msg::Any::Metrics => Some(op_metrics),
msg::Any::Mkdir => Some(op_mkdir),
msg::Any::Now => Some(op_now),
msg::Any::Open => Some(op_open),
msg::Any::PermissionRevoke => Some(op_revoke_permission),
msg::Any::Permissions => Some(op_permissions),
Expand Down
4 changes: 4 additions & 0 deletions cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub const OP_RESOURCES: OpId = 26;
pub const OP_GET_RANDOM_VALUES: OpId = 27;
pub const OP_GLOBAL_TIMER_STOP: OpId = 28;
pub const OP_GLOBAL_TIMER: OpId = 29;
pub const OP_NOW: OpId = 30;

pub fn dispatch(
state: &ThreadSafeState,
Expand Down Expand Up @@ -112,6 +113,9 @@ pub fn dispatch(
control,
zero_copy,
),
OP_NOW => {
dispatch_json::dispatch(performance::op_now, state, control, zero_copy)
}
OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy),
_ => panic!("bad op_id"),
};
Expand Down
34 changes: 8 additions & 26 deletions cli/ops/performance.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use super::dispatch_flatbuffers::serialize_response;
use super::utils::*;
use crate::msg;
use super::dispatch_json::{JsonOp, Value};
use crate::state::ThreadSafeState;
use deno::*;
use flatbuffers::FlatBufferBuilder;

// Returns a milliseconds and nanoseconds subsec
// since the start time of the deno runtime.
// If the High precision flag is not set, the
// nanoseconds are rounded on 2ms.
pub fn op_now(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
_args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let seconds = state.start_time.elapsed().as_secs();
let mut subsec_nanos = state.start_time.elapsed().subsec_nanos();
let reduced_time_precision = 2_000_000; // 2ms in nanoseconds
Expand All @@ -27,22 +23,8 @@ pub fn op_now(
subsec_nanos -= subsec_nanos % reduced_time_precision
}

let builder = &mut FlatBufferBuilder::new();
let inner = msg::NowRes::create(
builder,
&msg::NowResArgs {
seconds,
subsec_nanos,
},
);

ok_buf(serialize_response(
base.cmd_id(),
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::NowRes,
..Default::default()
},
))
Ok(JsonOp::Sync(json!({
"seconds": seconds,
"subsecNanos": subsec_nanos,
})))
}
1 change: 1 addition & 0 deletions js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const OP_RESOURCES = 26;
export const OP_GET_RANDOM_VALUES = 27;
export const OP_GLOBAL_TIMER_STOP = 28;
export const OP_GLOBAL_TIMER = 29;
export const OP_NOW = 30;

export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) {
Expand Down
11 changes: 3 additions & 8 deletions js/performance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers";
import { assert } from "./util";
import * as dispatch from "./dispatch";
import { sendSync } from "./dispatch_json";

export class Performance {
/** Returns a current time from Deno's start in milliseconds.
Expand All @@ -11,12 +11,7 @@ export class Performance {
* console.log(`${t} ms since start!`);
*/
now(): number {
const builder = flatbuffers.createBuilder();
const inner = msg.Now.createNow(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);
const res = sendSync(dispatch.OP_NOW);
return res.seconds().toFloat64() * 1e3 + res.subsecNanos() / 1e6;
}
}

0 comments on commit ea0a571

Please sign in to comment.