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

refactor: performance timers for node.js and browser #4340

Merged
merged 17 commits into from Jan 14, 2022
Merged
1 change: 1 addition & 0 deletions browser/performance.ts
@@ -0,0 +1 @@
export default performance;
Copy link
Contributor

Choose a reason for hiding this comment

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

Ignore this comment if it's a known issue with this Draft PR.

I think a level of indirection is needed here - via typeof or function. Although this would work in a recent browser, referencing the global performance at top level causes the browser bundle and its tests to fail on node <= v14:

$ node-v10.4.1 -e performance 2>/dev/null && echo OK || echo FAIL
FAIL
$ node-v12.13.0 -e performance 2>/dev/null && echo OK || echo FAIL
FAIL
$ node-v14.16.0 -e performance 2>/dev/null && echo OK || echo FAIL
FAIL
$ node-v16.1.0 -e performance 2>/dev/null && echo OK || echo FAIL
OK

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, ideally the global performance object would be used for both browser and node.js. I was under the impression it was supported as a global in v14, but you are right, it's only available in v16, unless someone would do a backport 😞

if I understood the bundle process correctly, the file used for the node.js build would be this one: https://github.com/rollup/rollup/pull/4340/files#diff-56aca2bfe43f23ab215af1f0c17d5ac2abbed4deb560487408bc588c47c2fc44R1 being replaced with this call:

replaceBrowserModules(),

originally I wrapped and exported the now function only, but I was thinking it's probably better to make the entire performance object (for future use) available. I was also thinking to remove the timer Map, and use performance.measure and/or performance observer instead. but it seems currently it wouldn't be possible to attach any other metadata, e.g. for the value for the memory consumption. (detail param was added in v16)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, I see. did you mean this: https://github.com/rollup/rollup/runs/4723627853?check_suite_focus=true#step:7:5615 just noticed. I have to check it out. I suppose those tests are not running in any browser but in node.js 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, the browser bundle name is a bit of a misnomer. It really means JS platform independent.

3 changes: 3 additions & 0 deletions browser/process.ts
@@ -0,0 +1,3 @@
export default function getMemory(): 0 {
return 0;
}
34 changes: 20 additions & 14 deletions build-plugins/replace-browser-modules.ts
@@ -1,29 +1,35 @@
import path from 'path';
import { Plugin } from 'rollup';
import { dirname, join, resolve } from 'path';
import type { Plugin } from 'rollup';

const ID_CRYPTO = path.resolve('src/utils/crypto');
const ID_FS = path.resolve('src/utils/fs');
const ID_HOOKACTIONS = path.resolve('src/utils/hookActions');
const ID_PATH = path.resolve('src/utils/path');
const ID_RESOLVEID = path.resolve('src/utils/resolveId');
const ID_CRYPTO = resolve('src/utils/crypto');
const ID_FS = resolve('src/utils/fs');
const ID_HOOKACTIONS = resolve('src/utils/hookActions');
const ID_PATH = resolve('src/utils/path');
const ID_PERFORMANCE = resolve('src/utils/performance');
const ID_PROCESS = resolve('src/utils/process');
const ID_RESOLVEID = resolve('src/utils/resolveId');

export default function replaceBrowserModules(): Plugin {
return {
name: 'replace-browser-modules',
resolveId: (source, importee) => {
resolveId(source, importee) {
if (importee && source[0] === '.') {
const resolved = path.join(path.dirname(importee), source);
const resolved = join(dirname(importee), source);
switch (resolved) {
case ID_CRYPTO:
return path.resolve('browser/crypto.ts');
return resolve('browser/crypto.ts');
case ID_FS:
return path.resolve('browser/fs.ts');
return resolve('browser/fs.ts');
case ID_HOOKACTIONS:
return path.resolve('browser/hookActions.ts');
return resolve('browser/hookActions.ts');
case ID_PATH:
return path.resolve('browser/path.ts');
return resolve('browser/path.ts');
case ID_PERFORMANCE:
return resolve('browser/performance.ts');
case ID_PROCESS:
return resolve('browser/process.ts');
case ID_RESOLVEID:
return path.resolve('browser/resolveId.ts');
return resolve('browser/resolveId.ts');
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion rollup.config.ts
Expand Up @@ -90,8 +90,10 @@ export default (command: Record<string, unknown>): RollupOptions | RollupOptions
'fs',
'fsevents',
'module',
'path',
'os',
'path',
'perf_hooks',
'process',
'stream',
'url',
'util'
Expand Down
4 changes: 2 additions & 2 deletions src/utils/collapseSourcemaps.ts
Expand Up @@ -49,7 +49,7 @@ class Link {
const sources: string[] = [];
const sourcesContent: string[] = [];
const names: string[] = [];
const nameIndexMap: Map<string, number> = new Map();
const nameIndexMap = new Map<string, number>();

const mappings = [];

Expand Down Expand Up @@ -147,7 +147,7 @@ class Link {
}

function getLinkMap(warn: WarningHandler) {
return function linkMap(source: Source | Link, map: DecodedSourceMapOrMissing) {
return function linkMap(source: Source | Link, map: DecodedSourceMapOrMissing): Link {
if (map.mappings) {
return new Link(map, [source]);
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/performance.ts
@@ -0,0 +1 @@
export { performance as default } from 'perf_hooks';
5 changes: 5 additions & 0 deletions src/utils/process.ts
@@ -0,0 +1,5 @@
import { memoryUsage } from 'process';

export default function getMemory(): number {
return memoryUsage().heapUsed;
}
98 changes: 41 additions & 57 deletions src/utils/timers.ts
@@ -1,39 +1,18 @@
import { InputOptions, Plugin, SerializedTimings } from '../rollup/types';

type StartTime = [number, number];
import performance from './performance';
import getMemory from './process';

interface Timer {
memory: number;
startMemory: number;
startTime: StartTime;
startTime: number;
time: number;
totalMemory: number;
}

interface Timers {
[label: string]: Timer;
}

const NOOP = (): void => {};
let getStartTime = (): StartTime => [0, 0];
let getElapsedTime: (previous: StartTime) => number = () => 0;
let getMemory: () => number = () => 0;
let timers: Timers = {};

const normalizeHrTime = (time: [number, number]): number => time[0] * 1e3 + time[1] / 1e6;

function setTimeHelpers(): void {
if (typeof process !== 'undefined' && typeof process.hrtime === 'function') {
getStartTime = process.hrtime.bind(process);
getElapsedTime = previous => normalizeHrTime(process.hrtime(previous));
} else if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
getStartTime = () => [performance.now(), 0];
getElapsedTime = previous => performance.now() - previous[0];
}
if (typeof process !== 'undefined' && typeof process.memoryUsage === 'function') {
getMemory = () => process.memoryUsage().heapUsed;
}
}

let timers = new Map<string, Timer>();

function getPersistedLabel(label: string, level: number): string {
switch (level) {
Expand All @@ -50,82 +29,87 @@ function getPersistedLabel(label: string, level: number): string {

function timeStartImpl(label: string, level = 3): void {
label = getPersistedLabel(label, level);
if (!timers.hasOwnProperty(label)) {
timers[label] = {

const startMemory = getMemory();
const startTime = performance.now();

const timer = timers.get(label);

if (timer === undefined) {
timers.set(label, {
memory: 0,
startMemory: undefined as never,
startTime: undefined as never,
startMemory,
startTime,
time: 0,
totalMemory: 0
};
});
} else {
timer.startMemory = startMemory;
timer.startTime = startTime;
}
const currentMemory = getMemory();
timers[label].startTime = getStartTime();
timers[label].startMemory = currentMemory;
}

function timeEndImpl(label: string, level = 3): void {
label = getPersistedLabel(label, level);
if (timers.hasOwnProperty(label)) {

const timer = timers.get(label);

if (timer !== undefined) {
const currentMemory = getMemory();
timers[label].time += getElapsedTime(timers[label].startTime);
timers[label].totalMemory = Math.max(timers[label].totalMemory, currentMemory);
timers[label].memory += currentMemory - timers[label].startMemory;
timer.memory += currentMemory - timer.startMemory;
timer.time += performance.now() - timer.startTime;
timer.totalMemory = Math.max(timer.totalMemory, currentMemory);
}
}

export function getTimings(): SerializedTimings {
const newTimings: SerializedTimings = {};
for (const [label, { time, memory, totalMemory }] of Object.entries(timers)) {

for (const [label, { memory, time, totalMemory }] of timers) {
newTimings[label] = [time, memory, totalMemory];
}
return newTimings;
}

export let timeStart: (label: string, level?: number) => void = NOOP,
timeEnd: (label: string, level?: number) => void = NOOP;
export let timeStart: (label: string, level?: number) => void = NOOP;
export let timeEnd: (label: string, level?: number) => void = NOOP;

const TIMED_PLUGIN_HOOKS: { [hook: string]: boolean } = {
load: true,
resolveDynamicImport: true,
resolveId: true,
transform: true
};
const TIMED_PLUGIN_HOOKS = ['load', 'resolveDynamicImport', 'resolveId', 'transform'] as const;

function getPluginWithTimers(plugin: any, index: number): Plugin {
const timedPlugin: { [hook: string]: any } = {};
const timedPlugin: Pick<Plugin, typeof TIMED_PLUGIN_HOOKS[number]> = {};

for (const hook of Object.keys(plugin)) {
if (TIMED_PLUGIN_HOOKS[hook] === true) {
for (const hook of TIMED_PLUGIN_HOOKS) {
if (hook in plugin) {
let timerLabel = `plugin ${index}`;
if (plugin.name) {
timerLabel += ` (${plugin.name})`;
}
timerLabel += ` - ${hook}`;
timedPlugin[hook] = function (...args: unknown[]) {
timeStart(timerLabel, 4);
let result = plugin[hook].apply(this === timedPlugin ? plugin : this, args);
const result = plugin[hook](...args);
timeEnd(timerLabel, 4);
if (result && typeof result.then === 'function') {
timeStart(`${timerLabel} (async)`, 4);
result = result.then((hookResult: unknown) => {
return result.then((hookResult: unknown) => {
timeEnd(`${timerLabel} (async)`, 4);
return hookResult;
});
}
return result;
};
} else {
timedPlugin[hook] = plugin[hook];
}
}
return timedPlugin as Plugin;
return {
...plugin,
...timedPlugin
};
}

export function initialiseTimers(inputOptions: InputOptions): void {
if (inputOptions.perf) {
timers = {};
setTimeHelpers();
timers = new Map();
timeStart = timeStartImpl;
timeEnd = timeEndImpl;
inputOptions.plugins = inputOptions.plugins!.map(getPluginWithTimers);
Expand Down