diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/deno.ts | 1 | ||||
-rw-r--r-- | js/metrics.ts | 39 | ||||
-rw-r--r-- | js/metrics_test.ts | 24 | ||||
-rw-r--r-- | js/unit_tests.ts | 1 |
4 files changed, 65 insertions, 0 deletions
diff --git a/js/deno.ts b/js/deno.ts index a7350e03e..212544541 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -35,6 +35,7 @@ export { trace } from "./trace"; export { truncateSync, truncate } from "./truncate"; export { FileInfo } from "./file_info"; export { connect, dial, listen, Listener, Conn } from "./net"; +export { metrics } from "./metrics"; export const args: string[] = []; // Provide the compiler API in an obfuscated way diff --git a/js/metrics.ts b/js/metrics.ts new file mode 100644 index 000000000..d76b781db --- /dev/null +++ b/js/metrics.ts @@ -0,0 +1,39 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +import * as msg from "gen/msg_generated"; +import { flatbuffers } from "flatbuffers"; +import { assert } from "./util"; +import * as dispatch from "./dispatch"; + +interface Metrics { + opsDispatched: number; + opsCompleted: number; + bytesSentControl: number; + bytesSentData: number; + bytesReceived: number; +} + +export function metrics(): Metrics { + return res(dispatch.sendSync(...req())); +} + +function req(): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { + const builder = new flatbuffers.Builder(); + msg.Metrics.startMetrics(builder); + const inner = msg.Metrics.endMetrics(builder); + return [builder, msg.Any.Metrics, inner]; +} + +function res(baseRes: null | msg.Base): Metrics { + assert(baseRes !== null); + assert(msg.Any.MetricsRes === baseRes!.innerType()); + const res = new msg.MetricsRes(); + assert(baseRes!.inner(res) !== null); + + return { + opsDispatched: res.opsDispatched().toFloat64(), + opsCompleted: res.opsCompleted().toFloat64(), + bytesSentControl: res.bytesSentControl().toFloat64(), + bytesSentData: res.bytesSentData().toFloat64(), + bytesReceived: res.bytesReceived().toFloat64() + }; +} diff --git a/js/metrics_test.ts b/js/metrics_test.ts new file mode 100644 index 000000000..6954ae2ce --- /dev/null +++ b/js/metrics_test.ts @@ -0,0 +1,24 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +import { test, assert } from "./test_util.ts"; +import * as deno from "deno"; + +test(function metrics() { + const m1 = deno.metrics(); + assert(m1.opsDispatched > 0); + assert(m1.opsCompleted > 0); + assert(m1.bytesSentControl > 0); + assert(m1.bytesSentData >= 0); + assert(m1.bytesReceived > 0); + + // Write to stdout to ensure a "data" message gets sent instead of just + // control messages. + const dataMsg = new Uint8Array([41, 42, 43]); + deno.stdout.write(dataMsg); + + const m2 = deno.metrics(); + assert(m2.opsDispatched > m1.opsDispatched); + assert(m2.opsCompleted > m1.opsCompleted); + assert(m2.bytesSentControl > m1.bytesSentControl); + assert(m2.bytesSentData >= m1.bytesSentData + dataMsg.byteLength); + assert(m2.bytesReceived > m1.bytesReceived); +}); diff --git a/js/unit_tests.ts b/js/unit_tests.ts index ca152ad39..24fdac823 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -25,3 +25,4 @@ import "./trace_test.ts"; import "./truncate_test.ts"; import "./v8_source_maps_test.ts"; import "../website/app_test.js"; +import "./metrics_test.ts"; |