tuupola / server-timing-middleware
PSR-7 和 PSR-15 服务器定时中间件
Requires
- php: ^7.1|^8.0
- psr/http-server-middleware: ^1.0
- symfony/stopwatch: ^4.0|^5.0|^6.0
- tuupola/callable-handler: ^1.0
Requires (Dev)
- doctrine/dbal: ^2.0
- equip/dispatch: ^2.0
- laminas/laminas-diactoros: ^2.4
- overtrue/phplint: ^0.2.0
- phpstan/phpstan: ^0.12.45
- phpunit/phpunit: ^7.0|^8.0|^9.0
- psr/http-message: ^1.0.1
- squizlabs/php_codesniffer: ^3.5
- tuupola/http-factory: ^1.0
Suggests
- doctrine/dbal: If you want to use the DBAL query timer.
This package is auto-updated.
Last update: 2024-08-29 03:31:35 UTC
README
此中间件实现了Server-Timing头,可以用于在Chrome DevTools中显示服务器端定时信息。
安装
使用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);
现在您应该可以在请求时看到默认的定时信息。
Bootstrap
是从请求开始到执行第一个传入中间件所花费的时间Process
是服务器生成响应和处理中间件堆栈所花费的时间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
中使用。您必须使用与QueryTimer
和ServerTimingMiddleware
中间件相同的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)。请参阅许可文件以获取更多信息。