UNPKG

servertime

Version:

Add server-timing header to your node.js app, with nanosecond precision.

61 lines (60 loc) 2.03 kB
import { Clock } from './clocks'; interface TimerOptions { isDummy?: boolean; clock: Clock<any>; } /** * Keeps track of timing data for events and turns that data into a `server-timing` header. */ export default class Timer { private _isDummy; private _clock; private _records; constructor(options: TimerOptions); /** * Start timing an event. * @param slug - The slug to use for timing. The same slug must be supplied to `end(slug)` in order * for this timing to show up in the final header. * @param [label] - Label to use in the server-timing header. */ start(slug: string, label?: string): void; /** * Stop timing an event. * @param slug - The slug to supplied to `start()`. */ end(slug: string): void; /** * Set the timing for an event. * @param slug - The slug to use for timing. * @param ms - Time, in milliseconds. Can be a float. */ setTime(slug: string, ms: number): void; /** * Set the timing for an event. * @param slug - The slug to use for timing. * @param label - Label to use in the server-timing header. * @param ms - Time, in milliseconds. Can be a float. */ setTime(slug: string, label: string, ms: number): void; /** * Time the duration of a promise. * @param slug - The slug to use for timing. * @param promise - The promise to time. * @return - Returns the passed in `promise`. */ timePromise<T>(slug: string, promise: Promise<T>): Promise<T>; /** * Time the duration of a promise. * @param slug - The slug to use for timing. * @param label - Label to use in the server-timing header. * @param promise - The promise to time. * @return - Returns the passed in `promise`. */ timePromise<T>(slug: string, label: string, promise: Promise<T>): Promise<T>; /** * Return the server-timing header. * @return {string} - The header. */ getHeader(): string | null; } export {};