@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
67 lines • 2.71 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as os from 'node:os';
import { performance } from 'node:perf_hooks';
import { AsyncParallelHook } from 'tapable';
import { InternalError } from '@rushstack/node-core-library';
/**
* @internal
* A simple performance metrics collector. A plugin is required to pipe data anywhere.
*/
export class MetricsCollector {
constructor() {
this.recordMetricsHook = new AsyncParallelHook(['recordMetricsHookOptions']);
}
/**
* Start metrics log timer.
*/
setStartTime() {
if (this._bootDurationMs === undefined) {
// Only set this once. This is for tracking boot overhead.
this._bootDurationMs = process.uptime() * 1000;
}
this._startTimeMs = performance.now();
}
/**
* Record metrics to the installed plugin(s).
*
* @param command - Describe the user command, e.g. `start` or `build`
* @param parameterMap - Optional map of parameters to their values
* @param performanceData - Optional performance data
*/
async recordAsync(command, performanceData, parameters) {
const { _bootDurationMs, _startTimeMs } = this;
if (_bootDurationMs === undefined || _startTimeMs === undefined) {
throw new InternalError('MetricsCollector has not been initialized with setStartTime() yet');
}
if (!command) {
throw new InternalError('The command name must be specified.');
}
const filledPerformanceData = {
taskTotalExecutionMs: performance.now() - _startTimeMs,
...(performanceData || {})
};
const { taskTotalExecutionMs } = filledPerformanceData;
const cpus = os.cpus();
const metricData = {
command: command,
encounteredError: filledPerformanceData.encounteredError,
bootDurationMs: _bootDurationMs,
taskTotalExecutionMs: taskTotalExecutionMs,
totalUptimeMs: process.uptime() * 1000,
machineOs: process.platform,
machineArch: process.arch,
machineCores: cpus.length,
// The Node.js model is sometimes padded, for example:
// "AMD Ryzen 7 3700X 8-Core Processor
machineProcessor: cpus[0].model.trim(),
machineTotalMemoryMB: os.totalmem(),
commandParameters: parameters || {}
};
await this.recordMetricsHook.promise({
metricName: 'inner_loop_heft',
metricData
});
}
}
//# sourceMappingURL=MetricsCollector.js.map