UNPKG

4.04 kBTypeScriptView Raw
1/********************************************************************************
2* Copyright (c) 2021 STMicroelectronics and others.
3*
4* This program and the accompanying materials are made available under the
5* terms of the Eclipse Public License 2.0 which is available at
6* http://www.eclipse.org/legal/epl-2.0.
7*
8* This Source Code may also be made available under the following Secondary
9* Licenses when the conditions for such availability set forth in the Eclipse
10* Public License v. 2.0 are satisfied: GNU General Public License, version 2
11* with the GNU Classpath Exception which is available at
12* https://www.gnu.org/software/classpath/license.html.
13*
14* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15*******************************************************************************/
16import { ILogger, LogLevel } from '../logger';
17import { MaybePromise } from '../types';
18import { Measurement, MeasurementOptions, MeasurementResult } from './measurement';
19import { Emitter, Event } from '../event';
20/**
21 * Configuration of the log messages written by a {@link Measurement}.
22 */
23interface LogOptions extends MeasurementOptions {
24 /** A function that computes the current time, in millis, since the start of the application. */
25 now: () => number;
26 /** An optional label for the application the start of which (in real time) is the basis of all measurements. */
27 owner?: string;
28 /** An optional log level to override any default or dynamic log level for a specific log message. */
29 levelOverride?: LogLevel;
30 /** Optional arguments to the log message. The 'optionalArgs' coming in from the {@link Measurement} API are slotted in here. */
31 arguments?: any[];
32}
33/**
34 * A factory of {@link Measurement}s for performance logging.
35 */
36export declare abstract class Stopwatch {
37 protected readonly defaultLogOptions: LogOptions;
38 protected readonly logger: ILogger;
39 protected _storedMeasurements: MeasurementResult[];
40 protected onDidAddMeasurementResultEmitter: Emitter<MeasurementResult>;
41 get onDidAddMeasurementResult(): Event<MeasurementResult>;
42 constructor(defaultLogOptions: LogOptions);
43 /**
44 * Create a {@link Measurement} that will compute its elapsed time when logged.
45 *
46 * @param name the {@link Measurement.name measurement name}
47 * @param options optional configuration of the new measurement
48 * @returns a self-timing measurement
49 */
50 abstract start(name: string, options?: MeasurementOptions): Measurement;
51 /**
52 * Wrap an asynchronous function in a {@link Measurement} that logs itself on completion.
53 * If obtaining and awaiting the `computation` runs too long according to the threshold
54 * set in the `options`, then the log message is a warning, otherwise a debug log.
55 *
56 * @param name the {@link Measurement.name name of the measurement} to wrap around the function
57 * @param description a description of what the function does, to be included in the log
58 * @param computation a supplier of the asynchronous function to wrap
59 * @param options optional addition configuration as for {@link measure}
60 * @returns the wrapped `computation`
61 *
62 * @see {@link MeasurementOptions.thresholdMillis}
63 */
64 startAsync<T>(name: string, description: string, computation: () => MaybePromise<T>, options?: MeasurementOptions): Promise<T>;
65 protected createMeasurement(name: string, measure: () => {
66 startTime: number;
67 duration: number;
68 }, options?: MeasurementOptions): Measurement;
69 protected mergeLogOptions(logOptions?: Partial<LogOptions>): LogOptions;
70 protected atLevel(logOptions: LogOptions, levelOverride?: LogLevel, optionalArgs?: any[]): LogOptions;
71 protected logLevel(elapsed: number, options?: Partial<LogOptions>): LogLevel;
72 protected log(measurement: Measurement, activity: string, options: LogOptions): void;
73 get storedMeasurements(): ReadonlyArray<MeasurementResult>;
74}
75export {};
76//# sourceMappingURL=stopwatch.d.ts.map
\No newline at end of file