@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
104 lines • 4.33 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetricsCollector = void 0;
const os = __importStar(require("node:os"));
const node_perf_hooks_1 = require("node:perf_hooks");
const tapable_1 = require("tapable");
const node_core_library_1 = require("@rushstack/node-core-library");
/**
* @internal
* A simple performance metrics collector. A plugin is required to pipe data anywhere.
*/
class MetricsCollector {
constructor() {
this.recordMetricsHook = new tapable_1.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 = node_perf_hooks_1.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 node_core_library_1.InternalError('MetricsCollector has not been initialized with setStartTime() yet');
}
if (!command) {
throw new node_core_library_1.InternalError('The command name must be specified.');
}
const filledPerformanceData = {
taskTotalExecutionMs: node_perf_hooks_1.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
});
}
}
exports.MetricsCollector = MetricsCollector;
//# sourceMappingURL=MetricsCollector.js.map