2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-05-24 16:21:32 -04:00
|
|
|
import { assert, assertEquals } from "./test_util.ts";
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function metrics() {
|
2021-02-21 13:20:31 -05:00
|
|
|
// Write to stdout to ensure a "data" message gets sent instead of just
|
|
|
|
// control messages.
|
|
|
|
const dataMsg = new Uint8Array([13, 13, 13]); // "\r\r\r",
|
|
|
|
await Deno.stdout.write(dataMsg);
|
|
|
|
|
2021-03-31 10:37:38 -04:00
|
|
|
// WARNING: bytesReceived & bytesSentControl are now always zero
|
|
|
|
// following https://github.com/denoland/deno/pull/9843
|
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
const m1 = Deno.metrics();
|
2018-10-05 13:21:15 -04:00
|
|
|
assert(m1.opsDispatched > 0);
|
|
|
|
assert(m1.opsCompleted > 0);
|
2021-03-31 10:37:38 -04:00
|
|
|
assert(m1.bytesSentControl === 0);
|
2021-05-06 13:32:03 -04:00
|
|
|
assert(m1.bytesSentData === 0);
|
2021-03-31 10:37:38 -04:00
|
|
|
assert(m1.bytesReceived === 0);
|
2021-11-09 13:26:17 -05:00
|
|
|
const m1OpWrite = m1.ops["op_write"];
|
2021-02-21 13:20:31 -05:00
|
|
|
assert(m1OpWrite.opsDispatchedAsync > 0);
|
|
|
|
assert(m1OpWrite.opsCompletedAsync > 0);
|
2021-03-31 10:37:38 -04:00
|
|
|
assert(m1OpWrite.bytesSentControl === 0);
|
2021-02-21 13:20:31 -05:00
|
|
|
assert(m1OpWrite.bytesSentData >= 0);
|
2021-03-31 10:37:38 -04:00
|
|
|
assert(m1OpWrite.bytesReceived === 0);
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.stdout.write(dataMsg);
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
const m2 = Deno.metrics();
|
2020-03-02 13:13:36 -05:00
|
|
|
assert(m2.opsDispatchedAsync > m1.opsDispatchedAsync);
|
|
|
|
assert(m2.opsCompletedAsync > m1.opsCompletedAsync);
|
2021-03-31 10:37:38 -04:00
|
|
|
assert(m2.bytesSentControl === m1.bytesSentControl);
|
2021-05-06 13:32:03 -04:00
|
|
|
assert(m2.bytesSentData === 0);
|
2021-03-31 10:37:38 -04:00
|
|
|
assert(m2.bytesReceived === m1.bytesReceived);
|
2021-11-09 13:26:17 -05:00
|
|
|
const m2OpWrite = m2.ops["op_write"];
|
2021-02-21 13:20:31 -05:00
|
|
|
assert(m2OpWrite.opsDispatchedAsync > m1OpWrite.opsDispatchedAsync);
|
|
|
|
assert(m2OpWrite.opsCompletedAsync > m1OpWrite.opsCompletedAsync);
|
2021-03-31 10:37:38 -04:00
|
|
|
assert(m2OpWrite.bytesSentControl === m1OpWrite.bytesSentControl);
|
2021-05-06 13:32:03 -04:00
|
|
|
assert(m2OpWrite.bytesSentData === 0);
|
2021-03-31 10:37:38 -04:00
|
|
|
assert(m2OpWrite.bytesReceived === m1OpWrite.bytesReceived);
|
2018-10-05 13:21:15 -04:00
|
|
|
});
|
2018-10-17 14:30:23 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function metricsUpdatedIfNoResponseSync() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2018-10-17 14:30:23 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const data = new Uint8Array([41, 42, 43]);
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2018-10-17 14:30:23 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const metrics = Deno.metrics();
|
|
|
|
assert(metrics.opsDispatched === metrics.opsCompleted);
|
|
|
|
assert(metrics.opsDispatchedSync === metrics.opsCompletedSync);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-10-17 14:30:23 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function metricsUpdatedIfNoResponseAsync() {
|
2019-04-21 16:40:10 -04:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2018-10-17 14:30:23 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
const data = new Uint8Array([41, 42, 43]);
|
2020-03-07 22:29:12 -05:00
|
|
|
await Deno.writeFile(filename, data, { mode: 0o666 });
|
2018-10-17 14:30:23 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
const metrics = Deno.metrics();
|
|
|
|
assert(metrics.opsDispatched === metrics.opsCompleted);
|
2020-03-02 13:13:36 -05:00
|
|
|
assert(metrics.opsDispatchedSync === metrics.opsCompletedSync);
|
|
|
|
assert(metrics.opsDispatchedAsync === metrics.opsCompletedAsync);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2021-04-28 12:41:50 -04:00
|
|
|
|
2021-04-30 15:51:48 -04:00
|
|
|
// Test that ops from extensions have metrics (via OpMiddleware)
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function metricsForOpCrates() {
|
2021-04-28 12:41:50 -04:00
|
|
|
const _ = new URL("https://deno.land");
|
|
|
|
|
|
|
|
const m1 = Deno.metrics().ops["op_url_parse"];
|
|
|
|
assert(m1.opsDispatched > 0);
|
|
|
|
assert(m1.opsCompleted > 0);
|
|
|
|
});
|
2022-05-24 16:21:32 -04:00
|
|
|
|
2023-01-24 12:54:10 -05:00
|
|
|
// Test that op_names == Objects.keys(Deno[Deno.internal].core.ops)
|
2022-05-24 16:21:32 -04:00
|
|
|
// since building the per-op metrics depends on op_names being complete
|
|
|
|
Deno.test(function opNamesMatch() {
|
2023-04-30 04:50:24 -04:00
|
|
|
// @ts-ignore: Deno[Deno.internal].core allowed
|
|
|
|
const ops = Object.keys(Deno[Deno.internal].core.ops);
|
|
|
|
// @ts-ignore: Deno[Deno.internal].core allowed
|
|
|
|
ops.concat(Object.keys(Deno[Deno.internal].core.asyncOps));
|
|
|
|
|
2022-05-24 16:21:32 -04:00
|
|
|
assertEquals(
|
2023-01-24 12:54:10 -05:00
|
|
|
// @ts-ignore: Deno[Deno.internal].core allowed
|
|
|
|
Deno[Deno.internal].core.opNames().sort(),
|
2023-04-30 04:50:24 -04:00
|
|
|
ops.sort().filter((name) => name !== "asyncOpsInfo"),
|
2022-05-24 16:21:32 -04:00
|
|
|
);
|
|
|
|
});
|