EventTiming
event.timing: records your own metrics for the Server-Timing header and for the onTimings hook.
event.timing: records your own metrics for the Server-Timing header and for the onTimings hook. When
serverTiming is off (the production default) both methods only run your function and record nothing.
Import#
ts
import type { EventTiming } from "@shadow-js/dusk/server";
Signature#
ts
interface EventTiming {
/**
* Runs `fn` and records how long it took under `name`. A function that returns a promise is measured until the
* promise settles. With a tracer, the call is also a child span `dusk.timing`. The same name measured again adds to
* its total.
*
* @param name A token (letters, digits and `!#$%&'*+-.^_\`|~`), at most 64 characters; not `total`, `route`,
* `loaders` or `render`.
* @param fn The work. Its result, or its error, is passed through.
* @param description An optional label (reduced to safe ASCII, at most 100 characters).
* @returns What `fn` returns.
* @throws TypeError for an invalid name, or for a 21st distinct name, while `serverTiming` is on.
* @example
* const user = await event.timing.measure("db", () => loadUser(id), "load user");
* @since 1.0.0
*/
measure<T>(name: string, fn: () => T, description?: string): T;
/**
* Records a duration you measured yourself.
*
* @param name See {@link EventTiming.measure}.
* @param durationMs Milliseconds; a negative or non-finite value is recorded as 0.
* @param description An optional label.
* @throws TypeError for an invalid name, or for a 21st distinct name, while `serverTiming` is on.
* @since 1.0.0
*/
add(name: string, durationMs: number, description?: string): void;
}
Members#
| Name | Type | Description |
|---|---|---|
measure |
measure<T>(name: string, fn: () => T, description?: string): T |
Runs fn and records how long it took under name. A function that returns a promise is measured until the promise settles. With a tracer, the call is also a child span dusk.timing. The same name measured again adds to its total. |
add |
add(name: string, durationMs: number, description?: string): void |
Records a duration you measured yourself. |