Compare commits

...

3 Commits

Author SHA1 Message Date
Jarred Sumner
4d1f0dacd8 Merge branch 'main' into fix-9432 2024-05-01 19:55:46 -07:00
Ashcon Partovi
7fd67a4151 Fix missing import 2024-04-30 11:30:04 -07:00
Ashcon Partovi
a1803347d9 Better stubs for perf_hooks.{createHistogram,monitorEventLoopDelay} 2024-04-30 11:02:01 -07:00
2 changed files with 101 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
// Hardcoded module "node:perf_hooks"
const { throwNotImplemented } = require("internal/shared");
const { throwNotImplemented, warnNotImplementedOnce } = require("internal/shared");
var {
Performance,
@@ -80,6 +80,7 @@ function createPerformanceNodeTiming() {
}
function eventLoopUtilization(utilization1, utilization2) {
warnNotImplementedOnce("perf_hooks.eventLoopUtilization");
return {
idle: 0,
active: 0,
@@ -87,6 +88,99 @@ function eventLoopUtilization(utilization1, utilization2) {
};
}
// 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() {
@@ -155,11 +249,7 @@ export default {
PerformanceObserver,
PerformanceObserverEntryList,
PerformanceNodeTiming,
monitorEventLoopDelay() {
throwNotImplemented("perf_hooks.monitorEventLoopDelay");
},
createHistogram() {
throwNotImplemented("perf_hooks.createHistogram");
},
PerformanceResourceTiming,
monitorEventLoopDelay,
createHistogram,
};

View File

@@ -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", () => {