1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

feat: update metrics to track different op types (#4221)

This commit is contained in:
Bartek Iwańczuk 2020-03-02 19:13:36 +01:00 committed by GitHub
parent cfe4369ded
commit ff5bba3be8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 40 deletions

View file

@ -1669,7 +1669,13 @@ declare namespace Deno {
/** **UNSTABLE**: not sure if broken or not */
export interface Metrics {
opsDispatched: number;
opsDispatchedSync: number;
opsDispatchedAsync: number;
opsDispatchedAsyncUnref: number;
opsCompleted: number;
opsCompletedSync: number;
opsCompletedAsync: number;
opsCompletedAsyncUnref: number;
bytesSentControl: number;
bytesSentData: number;
bytesReceived: number;
@ -1680,15 +1686,21 @@ declare namespace Deno {
* Receive metrics from the privileged side of Deno.
*
* > console.table(Deno.metrics())
*
* (index) Values
*
* opsDispatched 9
* opsCompleted 9
* bytesSentControl 504
* bytesSentData 0
* bytesReceived 856
*
*
* (index) Values
*
* opsDispatched 3
* opsDispatchedSync 2
* opsDispatchedAsync 1
* opsDispatchedAsyncUnref 0
* opsCompleted 3
* opsCompletedSync 2
* opsCompletedAsync 1
* opsCompletedAsyncUnref 0
* bytesSentControl 73
* bytesSentData 0
* bytesReceived 375
*
*/
export function metrics(): Metrics;

View file

@ -3,7 +3,13 @@ import { sendSync } from "./dispatch_json.ts";
export interface Metrics {
opsDispatched: number;
opsDispatchedSync: number;
opsDispatchedAsync: number;
opsDispatchedAsyncUnref: number;
opsCompleted: number;
opsCompletedSync: number;
opsCompletedAsync: number;
opsCompletedAsyncUnref: number;
bytesSentControl: number;
bytesSentData: number;
bytesReceived: number;
@ -12,15 +18,21 @@ export interface Metrics {
/** Receive metrics from the privileged side of Deno.
*
* > console.table(Deno.metrics())
*
* (index) Values
*
* opsDispatched 9
* opsCompleted 9
* bytesSentControl 504
* bytesSentData 0
* bytesReceived 856
*
*
* (index) Values
*
* opsDispatched 3
* opsDispatchedSync 2
* opsDispatchedAsync 1
* opsDispatchedAsyncUnref 0
* opsCompleted 3
* opsCompletedSync 2
* opsCompletedAsync 1
* opsCompletedAsyncUnref 0
* bytesSentControl 73
* bytesSentData 0
* bytesReceived 375
*
*/
export function metrics(): Metrics {
return sendSync("op_metrics");

View file

@ -4,7 +4,9 @@ import { test, testPerm, assert } from "./test_util.ts";
test(async function metrics(): Promise<void> {
const m1 = Deno.metrics();
assert(m1.opsDispatched > 0);
assert(m1.opsDispatchedSync > 0);
assert(m1.opsCompleted > 0);
assert(m1.opsCompletedSync > 0);
assert(m1.bytesSentControl > 0);
assert(m1.bytesSentData >= 0);
assert(m1.bytesReceived > 0);
@ -16,7 +18,11 @@ test(async function metrics(): Promise<void> {
const m2 = Deno.metrics();
assert(m2.opsDispatched > m1.opsDispatched);
assert(m2.opsDispatchedSync > m1.opsDispatchedSync);
assert(m2.opsDispatchedAsync > m1.opsDispatchedAsync);
assert(m2.opsCompleted > m1.opsCompleted);
assert(m2.opsCompletedSync > m1.opsCompletedSync);
assert(m2.opsCompletedAsync > m1.opsCompletedAsync);
assert(m2.bytesSentControl > m1.bytesSentControl);
assert(m2.bytesSentData >= m1.bytesSentData + dataMsg.byteLength);
assert(m2.bytesReceived > m1.bytesReceived);
@ -30,6 +36,7 @@ testPerm({ write: true }, function metricsUpdatedIfNoResponseSync(): void {
const metrics = Deno.metrics();
assert(metrics.opsDispatched === metrics.opsCompleted);
assert(metrics.opsDispatchedSync === metrics.opsCompletedSync);
});
testPerm(
@ -42,5 +49,7 @@ testPerm(
const metrics = Deno.metrics();
assert(metrics.opsDispatched === metrics.opsCompleted);
assert(metrics.opsDispatchedSync === metrics.opsCompletedSync);
assert(metrics.opsDispatchedAsync === metrics.opsCompletedAsync);
}
);

View file

@ -1,10 +1,69 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
#[derive(Default)]
#[derive(Default, Debug)]
pub struct Metrics {
pub ops_dispatched: u64,
pub ops_dispatched_sync: u64,
pub ops_dispatched_async: u64,
pub ops_dispatched_async_unref: u64,
pub ops_completed: u64,
pub ops_completed_sync: u64,
pub ops_completed_async: u64,
pub ops_completed_async_unref: u64,
pub bytes_sent_control: u64,
pub bytes_sent_data: u64,
pub bytes_received: u64,
pub resolve_count: u64,
}
impl Metrics {
fn op_dispatched(&mut self, bytes_sent_control: u64, bytes_sent_data: u64) {
self.ops_dispatched += 1;
self.bytes_sent_control += bytes_sent_control;
self.bytes_sent_data += bytes_sent_data;
}
fn op_completed(&mut self, bytes_received: u64) {
self.ops_completed += 1;
self.bytes_received += bytes_received;
}
pub fn op_sync(
&mut self,
bytes_sent_control: u64,
bytes_sent_data: u64,
bytes_received: u64,
) {
self.ops_dispatched_sync += 1;
self.op_dispatched(bytes_sent_control, bytes_sent_data);
self.ops_completed_sync += 1;
self.op_completed(bytes_received);
}
pub fn op_dispatched_async(
&mut self,
bytes_sent_control: u64,
bytes_sent_data: u64,
) {
self.ops_dispatched_async += 1;
self.op_dispatched(bytes_sent_control, bytes_sent_data)
}
pub fn op_dispatched_async_unref(
&mut self,
bytes_sent_control: u64,
bytes_sent_data: u64,
) {
self.ops_dispatched_async_unref += 1;
self.op_dispatched(bytes_sent_control, bytes_sent_data)
}
pub fn op_completed_async(&mut self, bytes_received: u64) {
self.ops_completed_async += 1;
self.op_completed(bytes_received);
}
pub fn op_completed_async_unref(&mut self, bytes_received: u64) {
self.ops_completed_async_unref += 1;
self.op_completed(bytes_received);
}
}

View file

@ -59,7 +59,13 @@ fn op_metrics(
Ok(JsonOp::Sync(json!({
"opsDispatched": m.ops_dispatched,
"opsDispatchedSync": m.ops_dispatched_sync,
"opsDispatchedAsync": m.ops_dispatched_async,
"opsDispatchedAsyncUnref": m.ops_dispatched_async_unref,
"opsCompleted": m.ops_completed,
"opsCompletedSync": m.ops_completed_sync,
"opsCompletedAsync": m.ops_completed_async,
"opsCompletedAsyncUnref": m.ops_completed_async_unref,
"bytesSentControl": m.bytes_sent_control,
"bytesSentData": m.bytes_sent_data,
"bytesReceived": m.bytes_received

View file

@ -89,25 +89,40 @@ impl State {
zero_copy.as_ref().map(|b| b.len()).unwrap_or(0) as u64;
let op = dispatcher(control, zero_copy);
state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy);
match op {
Op::Sync(buf) => {
state.metrics_op_completed(buf.len() as u64);
let mut state_ = state.borrow_mut();
state_.metrics.op_sync(
bytes_sent_control,
bytes_sent_zero_copy,
buf.len() as u64,
);
Op::Sync(buf)
}
Op::Async(fut) => {
let mut state_ = state.borrow_mut();
state_
.metrics
.op_dispatched_async(bytes_sent_control, bytes_sent_zero_copy);
let state = state.clone();
let result_fut = fut.map_ok(move |buf: Buf| {
state.metrics_op_completed(buf.len() as u64);
let mut state_ = state.borrow_mut();
state_.metrics.op_completed_async(buf.len() as u64);
buf
});
Op::Async(result_fut.boxed_local())
}
Op::AsyncUnref(fut) => {
let mut state_ = state.borrow_mut();
state_.metrics.op_dispatched_async_unref(
bytes_sent_control,
bytes_sent_zero_copy,
);
let state = state.clone();
let result_fut = fut.map_ok(move |buf: Buf| {
state.metrics_op_completed(buf.len() as u64);
let mut state_ = state.borrow_mut();
state_.metrics.op_completed_async_unref(buf.len() as u64);
buf
});
Op::AsyncUnref(result_fut.boxed_local())
@ -358,21 +373,4 @@ impl State {
)
.unwrap()
}
pub fn metrics_op_dispatched(
&self,
bytes_sent_control: u64,
bytes_sent_data: u64,
) {
let mut state = self.borrow_mut();
state.metrics.ops_dispatched += 1;
state.metrics.bytes_sent_control += bytes_sent_control;
state.metrics.bytes_sent_data += bytes_sent_data;
}
pub fn metrics_op_completed(&self, bytes_received: u64) {
let mut state = self.borrow_mut();
state.metrics.ops_completed += 1;
state.metrics.bytes_received += bytes_received;
}
}