Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

server-timing-middleware

> 编程语言
Open source

PSR-7 & PSR-15 middleware to add the Server-Timing header

200 stars0 likes2 views
WebsiteGitHub

About

PSR-7 & PSR-15 middleware to add the Server-Timing header

PSR-7 and PSR-15 Server Timing Middleware

This middleware implements the Server-Timing header which can be used for displaying server side timing information on Chrome DevTools.

Install

Install using Composer:

bash
$ composer require tuupola/server-timing-middleware

Simple usage

To get the default timings add the middleware to the pipeline. With Zend Expressive this goes go to the file named config/pipeline.php.

php
use Tuupola\Middleware\ServerTimingMiddleware;

$app->pipe(ServerTimingMiddleware::class);

Slim Framework does not have specific config files. Otherwise adding the middleware is similar with previous.

php
$app->add(new Tuupola\Middleware\ServerTimingMiddleware);

You should now see the default timings when doing a request.

  1. Bootstrap is the time taken from start of the request to execution of the first incoming middleware
  2. Process is the time taken for server to generate the response and process the middleware stack
  3. Total is the total time taken
$ curl --include http://localhost:8080

HTTP/1.1 200 OK
Server-Timing: Bootstrap;dur=54, Process;dur=2, Total;dur=58

Note that ServerTimingMiddleware must be added as last middleware. Otherwise timings will be inaccurate.

Changing the defaults

If you are not happy with the above you can change the description by using an optional settings array. To disable any of the defaults set the description to null.

php
use Tuupola\Middleware\ServerTimingMiddleware;
use Tuupola\Middleware\ServerTiming\Stopwatch;

$app->add(new ServerTimingMiddleware(
    new Stopwatch,
    [
        "bootstrap" => "Startup",
        "process" => null,
        "total" => "Sum"
    ])
);
$ curl --include http://localhost:8080

HTTP/1.1 200 OK
Server-Timing: Startup;dur=52, Sum;dur=57

Advanced usage

Example below uses Slim Framework. Note again that ServerTimingMiddleware must be added as last middleware. Otherwise timings will be inaccurate.

You can add your own timings by using the Stopwatch instance. See example below.

…
$ curl --include http://0.0.0.0:8080/test

HTTP/1.1 200 OK
Server-Timing: Bootstrap;dur=9, externalapi;dur=101;desc="External API", Magic;dur=50, SQL;dur=34, Process;dur=360, Total;dur=369

Usage with Doctrine DBAL

If you use Doctrine DBAL you can automate SQL query timings by using the provided QueryTimer. It implements the DBAL SQLLogger interface and can be used as standalone or in a LoggerChain. You must use the same Stopwatch instance with both QueryTimer and ServerTimingMiddleware middleware.

php
use Doctrine\DBAL\Logging\EchoSQLLogger;
use Doctrine\DBAL\Logging\LoggerChain;

use Tuupola\Middleware\ServerTiming\QueryTimer;
use Tuupola\Middleware\ServerTiming\Stopwatch;

$logger = new LoggerChain;
$echo = new EchoSQLLogger;
$stopwatch = new Stopwatch;
$timer = new QueryTimer($stopwatch);

$logger->addLogger($echo);
$logger->addLogger($timer);

/* Use your Doctrine DBAL connection here. */
$connection->getConfiguration()->setSQLLogger($logger);

Testing

You can run tests either manually or automatically on every code change. Automatic tests require entr to work.

bash
$ make test
bash
$ brew install entr
$ make watch

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

Issues· 3 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPpsr-15psr-7

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言