ServerTiming class

Adds a Server-Timing header to the answer. The browser dev tools show it in the Network tab.

AppConfig(
  globalMiddleware: <dynamic>[
    if (Env.get<String>('APP_ENV') == 'development') const ServerTiming(),
  ],
);

It always sends total (the middleware after it and the controller) and handler (only the controller). Other code adds its own timing to the current request with add or measure. The names total and handler are kept for these two: your own total is sent as app_total, and your own handler as app_handler. If you also add a time named app_total, the two are added together.

ServerTiming.add('db', query.duration);
String html = await ServerTiming.measure('render', () => view.render());

The names come in the order they were first added. So the times of the controller (like db) come first, then handler, then total: db;dur=4.10;desc="3 calls", handler;dur=12.34, total;dur=13.02.

When the answer already has a Server-Timing header (set with res.header), these times are added after it.

Security: this header tells every client how long your code takes. That can help an attacker (for example to guess if a user exists). It is off unless you add it. Use it in development, or only for clients you trust.

Implemented types

Constructors

ServerTiming()
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

handle(DoxRequest req, Next next) → Future<DoxResponse>
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

add(String name, Duration duration, {String? description}) → void
Adds duration to the timing name of the current request. When the same name is added again, the times are summed and counted.
measure<T>(String name, FutureOr<T> fn()) → Future<T>
Runs fn and adds the time it took as name, also when it throws. Without the middleware it only runs fn.

Constants

the header name
maxNames → const int
at most this many names in one header (plus handler and total), so a loop that makes new names can not make a huge header