summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/perf_hooks.ts
blob: ec76b3ce2d89c9a23ba1c5f6044f6e926a97daa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials

import { notImplemented } from "ext:deno_node/_utils.ts";
import {
  performance as shimPerformance,
  PerformanceEntry,
} from "ext:deno_web/15_performance.js";
import { EldHistogram } from "ext:core/ops";

class PerformanceObserver {
  static supportedEntryTypes: string[] = [];
  observe() {
    // todo(lucacasonato): actually implement this
  }
  disconnect() {
    // todo(lucacasonato): actually implement this
  }
}

const constants = {};

const performance:
  & Omit<
    Performance,
    "clearMeasures" | "getEntries"
  >
  & {
    eventLoopUtilization(): {
      idle: number;
      active: number;
      utilization: number;
    };
    nodeTiming: Record<string, string>;
    // deno-lint-ignore no-explicit-any
    timerify: any;
    // deno-lint-ignore no-explicit-any
    timeOrigin: any;
    // deno-lint-ignore no-explicit-any
    markResourceTiming: any;
  } = {
    clearMarks: (markName: string) => shimPerformance.clearMarks(markName),
    eventLoopUtilization: () => {
      // TODO(@marvinhagemeister): Return actual non-stubbed values
      return { idle: 0, active: 0, utilization: 0 };
    },
    mark: (markName: string) => shimPerformance.mark(markName),
    measure: (
      measureName: string,
      startMark?: string | PerformanceMeasureOptions,
      endMark?: string,
    ): PerformanceMeasure => {
      if (endMark) {
        return shimPerformance.measure(
          measureName,
          startMark as string,
          endMark,
        );
      } else {
        return shimPerformance.measure(
          measureName,
          startMark as PerformanceMeasureOptions,
        );
      }
    },
    nodeTiming: {},
    now: () => shimPerformance.now(),
    timerify: () => notImplemented("timerify from performance"),
    get timeOrigin() {
      // deno-lint-ignore no-explicit-any
      return (shimPerformance as any).timeOrigin;
    },
    getEntriesByName: (name, type) =>
      shimPerformance.getEntriesByName(name, type),
    getEntriesByType: (type) => shimPerformance.getEntriesByType(type),
    markResourceTiming: () => {},
    // @ts-ignore waiting on update in `deno`, but currently this is
    // a circular dependency
    toJSON: () => shimPerformance.toJSON(),
    addEventListener: (
      ...args: Parameters<typeof shimPerformance.addEventListener>
    ) => shimPerformance.addEventListener(...args),
    removeEventListener: (
      ...args: Parameters<typeof shimPerformance.removeEventListener>
    ) => shimPerformance.removeEventListener(...args),
    dispatchEvent: (
      ...args: Parameters<typeof shimPerformance.dispatchEvent>
    ) => shimPerformance.dispatchEvent(...args),
  };

function monitorEventLoopDelay(options = {}) {
  const { resolution = 10 } = options;

  return new EldHistogram(resolution);
}

export default {
  performance,
  PerformanceObserver,
  PerformanceEntry,
  monitorEventLoopDelay,
  constants,
};

export {
  constants,
  monitorEventLoopDelay,
  performance,
  PerformanceEntry,
  PerformanceObserver,
};