1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

add test case for metrics

This commit is contained in:
Bartek Iwańczuk 2018-10-17 20:30:23 +02:00 committed by Ryan Dahl
parent fd2bb015c7
commit 3a226f166f
2 changed files with 29 additions and 6 deletions

View file

@ -1,8 +1,8 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, assert } from "./test_util.ts";
import { test, testPerm, assert } from "./test_util.ts";
import * as deno from "deno";
test(function metrics() {
test(async function metrics() {
const m1 = deno.metrics();
assert(m1.opsDispatched > 0);
assert(m1.opsCompleted > 0);
@ -13,7 +13,7 @@ test(function metrics() {
// 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);
await deno.stdout.write(dataMsg);
const m2 = deno.metrics();
assert(m2.opsDispatched > m1.opsDispatched);
@ -22,3 +22,23 @@ test(function metrics() {
assert(m2.bytesSentData >= m1.bytesSentData + dataMsg.byteLength);
assert(m2.bytesReceived > m1.bytesReceived);
});
testPerm({ write: true }, function metricsUpdatedIfNoResponseSync() {
const filename = deno.makeTempDirSync() + "/test.txt";
const data = new Uint8Array([41, 42, 43]);
deno.writeFileSync(filename, data, 0o666);
const metrics = deno.metrics();
assert(metrics.opsDispatched === metrics.opsCompleted);
});
testPerm({ write: true }, async function metricsUpdatedIfNoResponseAsync() {
const filename = deno.makeTempDirSync() + "/test.txt";
const data = new Uint8Array([41, 42, 43]);
await deno.writeFile(filename, data, 0o666);
const metrics = deno.metrics();
assert(metrics.opsDispatched === metrics.opsCompleted);
});

View file

@ -171,6 +171,8 @@ impl Isolate {
}
pub fn respond(&mut self, req_id: i32, buf: Buf) {
self.state.metrics_op_completed(buf.len() as u64);
// TODO(zero-copy) Use Buf::leak(buf) to leak the heap allocated buf. And
// don't do the memcpy in ImportBuf() (in libdeno/binding.cc)
unsafe {
@ -188,9 +190,7 @@ impl Isolate {
// completing.
self.ntasks_decrement();
// Call into JS with the buf.
let buf_size = buf.len() as u64;
self.respond(req_id, buf);
self.state.metrics_op_completed(buf_size)
}
fn timeout(&mut self) {
@ -305,10 +305,13 @@ extern "C" fn pre_dispatch(
// Execute op synchronously.
let buf = tokio_util::block_on(op).unwrap();
let buf_size = buf.len();
if buf_size != 0 {
// Set the synchronous response, the value returned from isolate.send().
isolate.respond(req_id, buf);
isolate.state.metrics_op_completed(buf_size as u64);
} else {
// FIXME
isolate.state.metrics_op_completed(buf.len() as u64);
}
} else {
// Execute op asynchronously.