UNPKG

3.68 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 WITH Classpath-exception-2.0
15*******************************************************************************/
16import { ILogger, LogLevel } from '../logger';
17import { MaybePromise } from '../types';
18import { Measurement, MeasurementOptions } from './measurement';
19/**
20 * Configuration of the log messages written by a {@link Measurement}.
21 */
22interface LogOptions extends MeasurementOptions {
23 /** A function that computes the current time, in millis, since the start of the application. */
24 now: () => number;
25 /** An optional label for the application the start of which (in real time) is the basis of all measurements. */
26 owner?: string;
27 /** An optional log level to override any default or dynamic log level for a specific log message. */
28 levelOverride?: LogLevel;
29 /** Optional arguments to the log message. The 'optionalArgs' coming in from the {@link Measurement} API are slotted in here. */
30 arguments?: any[];
31}
32/**
33 * A factory of {@link Measurement}s for performance logging.
34 */
35export declare abstract class Stopwatch {
36 protected readonly defaultLogOptions: LogOptions;
37 protected readonly logger: ILogger;
38 protected constructor(defaultLogOptions: LogOptions);
39 /**
40 * Create a {@link Measurement} that will compute its elapsed time when logged.
41 *
42 * @param name the {@link Measurement.name measurement name}
43 * @param options optional configuration of the new measurement
44 * @returns a self-timing measurement
45 */
46 abstract start(name: string, options?: MeasurementOptions): Measurement;
47 /**
48 * Wrap an asynchronous function in a {@link Measurement} that logs itself on completion.
49 * If obtaining and awaiting the `computation` runs too long according to the threshold
50 * set in the `options`, then the log message is a warning, otherwise a debug log.
51 *
52 * @param name the {@link Measurement.name name of the measurement} to wrap around the function
53 * @param description a description of what the function does, to be included in the log
54 * @param computation a supplier of the asynchronous function to wrap
55 * @param options optional addition configuration as for {@link measure}
56 * @returns the wrapped `computation`
57 *
58 * @see {@link MeasurementOptions.thresholdMillis}
59 */
60 startAsync<T>(name: string, description: string, computation: () => MaybePromise<T>, options?: MeasurementOptions): Promise<T>;
61 protected createMeasurement(name: string, measurement: () => number, options?: MeasurementOptions): Measurement;
62 protected mergeLogOptions(logOptions?: Partial<LogOptions>): LogOptions;
63 protected atLevel(logOptions: LogOptions, levelOverride?: LogLevel, optionalArgs?: any[]): LogOptions;
64 protected logLevel(elapsed: number, options?: Partial<LogOptions>): LogLevel;
65 protected log(measurement: Measurement, activity: string, options: LogOptions): void;
66}
67export {};
68//# sourceMappingURL=stopwatch.d.ts.map
\No newline at end of file