tuupola/server-timing-middleware
Composer 安装命令:
composer require tuupola/server-timing-middleware
包简介
PSR-7 and PSR-15 server timing middleware
关键字:
README 文档
README
This middleware implements the Server-Timing header which can be used for displaying server side timing information on Chrome DevTools.
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.
Bootstrapis the time taken from start of the request to execution of the first incoming middlewareProcessis the time taken for server to generate the response and process the middleware stackTotalis 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 tuupola@appelsiini.net instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
tuupola/server-timing-middleware 适用场景与选型建议
tuupola/server-timing-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 90.36k 次下载、GitHub Stars 达 200, 最近一次更新时间为 2017 年 03 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「psr-7」 「psr-15」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tuupola/server-timing-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tuupola/server-timing-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tuupola/server-timing-middleware 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Model of a web-based resource
Interfaces for web resource models and services to retrieve and create them
Interface for a factory to create Psr\Http\Message\StreamInterface objects
A PSR-7 compatible library for making CRUD API endpoints
Retrieve a WebResourceInterface instance over HTTP
统计信息
- 总下载量: 90.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 201
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-07