UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

233 lines • 10.7 kB
"use strict"; /******************************************************************************** * Copyright (c) 2021 STMicroelectronics and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 *******************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.MeasurementContext = exports.Stopwatch = void 0; const tslib_1 = require("tslib"); /* eslint-disable @typescript-eslint/no-explicit-any */ const inversify_1 = require("inversify"); const logger_1 = require("../logger"); const event_1 = require("../event"); /** The default log level for measurements that are not otherwise configured with a default. */ const DEFAULT_LOG_LEVEL = logger_1.LogLevel.INFO; /** * A factory of {@link Measurement}s for performance logging. */ let Stopwatch = class Stopwatch { get onDidAddMeasurementResult() { return this.onDidAddMeasurementResultEmitter.event; } constructor(defaultLogOptions) { this.defaultLogOptions = defaultLogOptions; this._storedMeasurements = []; this.onDidAddMeasurementResultEmitter = new event_1.Emitter(); if (!defaultLogOptions.defaultLogLevel) { defaultLogOptions.defaultLogLevel = DEFAULT_LOG_LEVEL; } if (defaultLogOptions.storeResults === undefined) { defaultLogOptions.storeResults = true; } } /** * Wrap an asynchronous function in a {@link Measurement} that logs itself on completion. * If obtaining and awaiting the `computation` runs too long according to the threshold * set in the `options`, then the log message is a warning, otherwise a debug log. * * @param name the {@link Measurement.name name of the measurement} to wrap around the function * @param description a description of what the function does, to be included in the log * @param computation a supplier of the asynchronous function to wrap * @param options optional addition configuration as for {@link measure} * @returns the wrapped `computation` * * @see {@link MeasurementOptions.thresholdMillis} */ async startAsync(name, description, computation, options) { const threshold = options?.thresholdMillis ?? Number.POSITIVE_INFINITY; const measure = this.start(name, options); const result = await computation(); if (measure.stop() > threshold) { measure.warn(`${description} took longer than the expected maximum ${threshold} milliseconds`); } else { measure.log(description); } return result; } createMeasurement(name, measure, options) { const logOptions = this.mergeLogOptions(options); const measurement = { name, stop: () => { if (measurement.elapsed === undefined) { const { startTime, duration } = measure(); measurement.elapsed = duration; const result = { name, elapsed: duration, startTime, owner: logOptions.owner }; if (logOptions.storeResults) { this._storedMeasurements.push(result); } this.onDidAddMeasurementResultEmitter.fire(result); } return measurement.elapsed; }, log: (activity, ...optionalArgs) => this.log(measurement, activity, this.atLevel(logOptions, undefined, optionalArgs)), debug: (activity, ...optionalArgs) => this.log(measurement, activity, this.atLevel(logOptions, logger_1.LogLevel.DEBUG, optionalArgs)), info: (activity, ...optionalArgs) => this.log(measurement, activity, this.atLevel(logOptions, logger_1.LogLevel.INFO, optionalArgs)), warn: (activity, ...optionalArgs) => this.log(measurement, activity, this.atLevel(logOptions, logger_1.LogLevel.WARN, optionalArgs)), error: (activity, ...optionalArgs) => this.log(measurement, activity, this.atLevel(logOptions, logger_1.LogLevel.ERROR, optionalArgs)), }; return measurement; } mergeLogOptions(logOptions) { const result = { ...this.defaultLogOptions }; if (logOptions) { Object.assign(result, logOptions); } return result; } atLevel(logOptions, levelOverride, optionalArgs) { return { ...logOptions, levelOverride, arguments: optionalArgs }; } logLevel(elapsed, options) { if (options?.levelOverride) { return options.levelOverride; } return options?.defaultLogLevel ?? this.defaultLogOptions.defaultLogLevel ?? DEFAULT_LOG_LEVEL; } log(measurement, activity, options) { const elapsed = measurement.stop(); const level = this.logLevel(elapsed, options); if (Number.isNaN(elapsed)) { switch (level) { case logger_1.LogLevel.ERROR: case logger_1.LogLevel.FATAL: // Always log errors, even if NaN duration from native API preventing a measurement break; default: // Measurement was prevented by native API, do not log NaN duration return; } } const origin = options.owner ?? 'application'; const timeFromStart = `${(options.now() / 1000).toFixed(3)} s since ${origin} start`; const whatWasMeasured = options.context ? `[${options.context}] ${activity}` : activity; this.logger.log(level, `${whatWasMeasured}: ${elapsed.toFixed(1)} ms [${timeFromStart}]`, ...(options.arguments ?? [])); } get storedMeasurements() { return this._storedMeasurements; } }; exports.Stopwatch = Stopwatch; tslib_1.__decorate([ (0, inversify_1.inject)(logger_1.ILogger), tslib_1.__metadata("design:type", Object) ], Stopwatch.prototype, "logger", void 0); exports.Stopwatch = Stopwatch = tslib_1.__decorate([ (0, inversify_1.injectable)(), tslib_1.__param(0, (0, inversify_1.unmanaged)()), tslib_1.__metadata("design:paramtypes", [Object]) ], Stopwatch); /** * Tracks the settlement of async work initiated by contributions during application startup. * * A contribution "settles" when all promises it returned from lifecycle methods (initialize, configure, onStart, etc.) * have resolved. Individual settlement is only logged when a contribution returned promises from more than one lifecycle * method; otherwise the single lifecycle measurement already describes the work. An aggregate "all settled" message is * logged once all tracked promises across all contributions have resolved. * * Typical usage: * 1. Create the context at the start of the application lifecycle. * 2. Before each lifecycle call, call {@link ensureEntry} to start the per-contribution clock. * 3. After each lifecycle call, call {@link trackSettlement} with the return value. * 4. After the startup sequence completes, call {@link armAllSettled} to enable the aggregate message. */ class MeasurementContext { constructor(stopwatch, owner, thresholdMillis) { this.stopwatch = stopwatch; this.owner = owner; this.thresholdMillis = thresholdMillis; this.entries = new Map(); this.allSettledPending = 0; this.allSettledArmed = false; this.allSettledMeasurement = this.stopwatch.start(`${owner.toLowerCase()}-all-settled`); } /** * Ensure that settlement tracking has been started for the given contribution. * Starts the per-contribution measurement clock on the first call for each contribution. */ ensureEntry(item) { if (!this.entries.has(item)) { const name = item.constructor.name; this.entries.set(item, { name, measurement: this.stopwatch.start(`${name}.settled`, { thresholdMillis: this.thresholdMillis }), pending: 0, total: 0 }); } } /** * Track a promise returned by a contribution's lifecycle method. * Must be called after the corresponding {@link Stopwatch.startAsync} has completed so that * the settlement log appears after the lifecycle measurement log. */ trackSettlement(item, result) { if (result instanceof Promise) { const entry = this.entries.get(item); entry.pending++; entry.total++; this.allSettledPending++; const onSettled = () => { this.onPromiseSettled(item); }; result.then(onSettled, onSettled); } } /** * Arm the aggregate "all settled" log message. Call this after the startup sequence has finished * collecting all promises. If all promises have already settled, the message is logged immediately. */ armAllSettled() { this.allSettledArmed = true; if (this.allSettledPending === 0) { this.allSettledMeasurement.info(`All ${this.owner.toLowerCase()} contributions settled`); } } onPromiseSettled(item) { const entry = this.entries.get(item); if (entry && --entry.pending === 0) { const { name, measurement, total } = entry; this.entries.delete(item); if (total > 1) { if (measurement.stop() > this.thresholdMillis) { measurement.warn(`${this.owner} ${name} took longer than expected to settle`); } else { measurement.debug(`${this.owner} ${name} settled`); } } } if (--this.allSettledPending === 0 && this.allSettledArmed) { this.allSettledMeasurement.info(`All ${this.owner.toLowerCase()} contributions settled`); } } } exports.MeasurementContext = MeasurementContext; //# sourceMappingURL=stopwatch.js.map