Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/profiler/runtime/clock.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { performance } from "node:perf_hooks";

interface CpuUsage {
user: number;
system: number;
}

export interface ProfilerClock {
/** High-resolution monotonic time in milliseconds. It is suitable for measuring elapsed time. */
now(): number;
/** Unix wall-clock timestamp in milliseconds. It is suitable for timestamps shared between processes. */
epochNow(): number;
/** Process CPU usage since startup, or since `previousValue` when it is supplied. */
cpuUsage(previousValue?: NodeJS.CpuUsage): NodeJS.CpuUsage;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"NodeJS.CpuUsage" causes typescript to add reference to mocha types

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmmmm why is that so, I wonder?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because NodeJS is a global namespace, which is expanded by:

  • @types/node
  • @types/mocha
  • expect-webdriverio

So all those 3 are referenced

cpuUsage(previousValue?: CpuUsage): CpuUsage;
/** Current-thread CPU usage since startup, or since `previousValue` when it is supplied. */
threadCpuUsage?(previousValue?: NodeJS.CpuUsage): NodeJS.CpuUsage;
threadCpuUsage?(previousValue?: CpuUsage): CpuUsage;
}

export const systemClock: ProfilerClock = {
now: () => performance.now(),
epochNow: () => Date.now(),
cpuUsage: (previousValue): NodeJS.CpuUsage => process.cpuUsage(previousValue),
cpuUsage: (previousValue): CpuUsage => process.cpuUsage(previousValue),
threadCpuUsage:
typeof process.threadCpuUsage === "function"
? (previousValue): NodeJS.CpuUsage => process.threadCpuUsage(previousValue)
? (previousValue): CpuUsage => process.threadCpuUsage(previousValue)
: undefined,
};

export const cpuUsageToMs = (usage: NodeJS.CpuUsage): number => (usage.user + usage.system) / 1000;
export const cpuUsageToMs = (usage: CpuUsage): number => (usage.user + usage.system) / 1000;
Loading