tuupola/server-timing-middleware

PSR-7 和 PSR-15 服务器定时中间件

0.10.0 2022-05-13 08:11 UTC

This package is auto-updated.

Last update: 2024-08-29 03:31:35 UTC


README

Latest Version Software License Build Status Coverage

此中间件实现了Server-Timing头,可以用于在Chrome DevTools中显示服务器端定时信息。

Server Timing

安装

使用Composer安装

$ composer require tuupola/server-timing-middleware

简单用法

要获取默认定时信息,请将中间件添加到管道中。在使用Zend Expressive时,这需要将文件重命名为config/pipeline.php

use Tuupola\Middleware\ServerTimingMiddleware;

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

Slim 框架没有特定的配置文件。否则添加中间件的方式与之前类似。

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

现在您应该可以在请求时看到默认的定时信息。

  1. Bootstrap是从请求开始到执行第一个传入中间件所花费的时间
  2. Process是服务器生成响应和处理中间件堆栈所花费的时间
  3. Total是总耗时
$ curl --include http://localhost:8080

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

请注意,必须将ServerTimingMiddleware作为最后一个中间件添加。否则定时信息将不准确。

修改默认值

如果您对上述内容不满意,可以使用可选的设置数组来更改描述。要禁用任何默认值,请将描述设置为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

高级用法

以下示例使用Slim 框架。请注意再次,必须将ServerTimingMiddleware作为最后一个中间件添加。否则定时信息将不准确。

您可以使用Stopwatch实例添加自己的定时信息。请参阅以下示例。

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

与Doctrine DBAL一起使用

如果您使用Doctrine DBAL,则可以使用提供的QueryTimer自动SQL查询定时。它实现了DBAL SQLLogger接口,可以作为独立使用或在LoggerChain中使用。您必须使用与QueryTimerServerTimingMiddleware中间件相同的Stopwatch实例。

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);

测试

您可以选择手动运行测试,或者在每次代码更改时自动运行测试。自动测试需要entr工作。

$ make test
$ brew install entr
$ make watch

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过tuupola@appelsiini.net发送电子邮件,而不是使用问题跟踪器。

许可

MIT 许可证(MIT)。请参阅许可文件以获取更多信息。