Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better stubs for perf_hooks.{createHistogram,monitorEventLoopDelay} #10702

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 97 additions & 7 deletions src/js/node/perf_hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Hardcoded module "node:perf_hooks"
const { throwNotImplemented } = require("internal/shared");
const { throwNotImplemented, warnNotImplementedOnce } = require("internal/shared");

var {
Performance,
Expand Down Expand Up @@ -80,13 +80,107 @@ function createPerformanceNodeTiming() {
}

function eventLoopUtilization(utilization1, utilization2) {
warnNotImplementedOnce("perf_hooks.eventLoopUtilization");
return {
idle: 0,
active: 0,
utilization: 0,
};
}

// https://nodejs.org/api/perf_hooks.html#class-histogram
// https://github.com/nodejs/node/blob/5976985a58a8635b8f1272519020c817f4ccdd1b/src/histogram.h#L25
class Histogram {
count: number;
countBigInt: bigint;
exceeds: number;
exceedsBigInt: bigint;
max: number;
maxBigInt: bigint;
mean: number;
min: number;
minBigInt: bigint;
percentiles: Map<number, number>;
percentilesBigInt: Map<number, bigint>;
stddev: number;

constructor() {
this.count = 0;
this.countBigInt = 0n;
this.exceeds = 0;
this.exceedsBigInt = 0n;
this.max = 0;
this.maxBigInt = 0n;
this.mean = 0;
this.min = 0;
this.minBigInt = 0n;
this.percentiles = new Map();
this.percentilesBigInt = new Map();
this.stddev = 0;
}

percentile(p: number) {
return 0;
}

percentileBigInt(p: number) {
return 0n;
}

reset() {
this.percentiles.clear();
this.percentilesBigInt.clear();
}
}

class IntervalHistogram extends Histogram {
#enabled: boolean = false;

enable() {
const wasEnabled = this.#enabled;
if (!wasEnabled) {
this.#enabled = true;
}
return wasEnabled;
}

disable() {
const wasEnabled = this.#enabled;
if (wasEnabled) {
this.#enabled = false;
}
return wasEnabled;
}
}

class RecordableHistogram extends Histogram {
constructor(options?: unknown) {
super();
}

add(other: RecordableHistogram) {
// TODO
}

record(value: number | bigint) {
// TODO
}

recordDelta() {
// TODO
}
}

function createHistogram(options) {
warnNotImplementedOnce("perf_hooks.createHistogram");
return new RecordableHistogram(options);
}

function monitorEventLoopDelay() {
warnNotImplementedOnce("perf_hooks.monitorEventLoopDelay");
return new IntervalHistogram();
}

// PerformanceEntry is not a valid constructor, so we have to fake it.
class PerformanceResourceTiming {
constructor() {
Expand Down Expand Up @@ -155,11 +249,7 @@ export default {
PerformanceObserver,
PerformanceObserverEntryList,
PerformanceNodeTiming,
monitorEventLoopDelay() {
throwNotImplemented("perf_hooks.monitorEventLoopDelay");
},
createHistogram() {
throwNotImplemented("perf_hooks.createHistogram");
},
PerformanceResourceTiming,
monitorEventLoopDelay,
createHistogram,
};
7 changes: 4 additions & 3 deletions test/js/node/perf_hooks/perf_hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import perf from "perf_hooks";
import { test, expect } from "bun:test";

test("stubs", () => {
expect(() => perf.monitorEventLoopDelay()).toThrow();
expect(() => perf.createHistogram()).toThrow();
expect(perf.performance.nodeTiming).toBeObject();

expect(perf.performance.now()).toBeNumber();
expect(perf.performance.timeOrigin).toBeNumber();
expect(perf.performance.eventLoopUtilization()).toBeObject();
expect(perf.createHistogram).toBeFunction();
expect(perf.createHistogram()).toBeObject();
expect(perf.monitorEventLoopDelay).toBeFunction();
expect(perf.monitorEventLoopDelay()).toBeObject();
});

test("doesn't throw", () => {
Expand Down