Code Monkey home page Code Monkey logo

server-timing-middleware's Introduction

PSR-7 and PSR-15 Server Timing Middleware

Latest Version Software License Build Status Coverage

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

Server Timing

Install

Install using Composer:

$ 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.

use Tuupola\Middleware\ServerTimingMiddleware;

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

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

$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.

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.

require __DIR__ . "/vendor/autoload.php";

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Tuupola\Middleware\ServerTimingMiddleware;
use Tuupola\Middleware\ServerTiming\Stopwatch;

$app = new Slim\App;
$container = $app->getContainer();

$container["stopwatch"] = function ($container) {
    return new Stopwatch;
};

$container["ServerTimingMiddleware"] = function ($container) {
    return new ServerTimingMiddleware($container["stopwatch"]);
};

$container["DummyMiddleware"] = function ($container) {
    return function ($request, $response, $next) {
        usleep(200000);
        return $next($request, $response);
    };
};

$app->add("DummyMiddleware");
$app->add("ServerTimingMiddleware");

$app->get("/test", function (Request $request, Response $response) {
    $this->stopwatch->start("External API");
    usleep(100000);
    $this->stopwatch->stop("External API");

    $this->stopwatch->closure("Magic", function () {
        usleep(50000);
    });

    $this->stopwatch->set("SQL", 34);

    return $response;
});

$app->run();
$ 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.

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.

$ make test
$ 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.

server-timing-middleware's People

Contributors

drewm avatar esetnik avatar jyggen avatar laurinomme-other avatar lex111 avatar tuupola avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

server-timing-middleware's Issues

Update header format

Please note that the response header format has evolved:
Old:

Server-Timing: <name>=<duration>; <description>

New:

Server-Timing: <name>; dur=<duration>; desc=<description>

Example:

Server-Timing: db; dur=123.4; desc="database access"

Spec PR

Updates in Chrome

Output can exceed header size limit

I got a really indescriptive error message in my logs:

[Wed Apr 10 09:33:24.816821 2024] [proxy_fcgi:error] [pid 730003] [client 10.0.0.32:52632] Premature end of script headers: index.php, referer: http://xxx
[Wed Apr 10 09:33:24.816849 2024] [proxy_fcgi:error] [pid 730003] [client 10.0.0.32:52632] AH01070: Error parsing script headers, referer: http://xxx
[Wed Apr 10 09:33:24.816856 2024] [proxy_fcgi:error] [pid 730003] (22)Invalid argument: [client 10.0.0.32:52632] AH01075: Error dispatching request to : , referer: http://xxx

After some debugging I found that the Server-Timing header was way too large, due to having too many timing entries.

On most apache configurations, the LimitRequestFieldSize variable is around 8 KB, although I don't know of a way to read it in PHP.

My proposal here is to introduce a configurable option of e.g. maxHeaderSize. This rule would be best enforced in front of this line probably:

return $header = (string) preg_replace("/, $/", "", $header);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.