ndrx / profiler
该软件包已被废弃,不再维护。未建议替代软件包。
简单的性能分析软件包
0.7
2015-12-26 15:43 UTC
Requires
- php: >=5.5.0
- php-jsonpatch/php-jsonpatch: 1.2.*
- psr/log: ^1.0
- symfony/event-dispatcher: ^2.7
- symfony/filesystem: ^2.7
- symfony/finder: ^2.7
- symfony/http-foundation: ^2.7
- symfony/options-resolver: ^2.7
- twig/twig: ~1.0
Requires (Dev)
- monolog/monolog: ^1.17
- phpunit/phpunit: 4.*
README
安装
通过Composer
$ composer require ndrx-io/profiler
使用方法
初始化性能分析器
// build a new profiler $profiler = ProfilerFactory::build([ ProfilerFactory::OPTION_ENABLE => true, ProfilerFactory::OPTION_DATASOURCE_PROFILES_FOLDER => '/tmp', ProfilerFactory::OPTION_COLLECTORS => [ \Ndrx\Profiler\Collectors\Data\PhpVersion::class, \Ndrx\Profiler\Collectors\Data\CpuUsage::class, \Ndrx\Profiler\Collectors\Data\Context::class, \Ndrx\Profiler\Collectors\Data\Timeline::class, \Ndrx\Profiler\Collectors\Data\Request::class, \Ndrx\Profiler\Collectors\Data\Log::class, \Ndrx\Profiler\Collectors\Data\Duration::class, // add other data collector ... ], /** * Ndrx\Profiler\Components\Logs\Monolog * or Ndrx\Profiler\Components\Logs\Simple available **/ ProfilerFactory::OPTION_LOGGER => \Ndrx\Profiler\Components\Logs\Monolog::class ]); // initialize the profiler $profiler->initiate();
将事件添加到时间轴
$profiler->start('foo', 'Bar'); $profiler->stop('foo'); $this->profiler->monitor('Foobar', function() { // very long process });
日志记录器
$profiler->debug('No beer'); $profiler->info('No beer'); $profiler->notice('No beer'); $profiler->alert('No beer'); $profiler->error('No beer'); $profiler->emergency('No beer'); $profiler->critical('No beer');
获取最后分析结果
$profiles = $profiler->getDatasource()->all(0, 10);
获取分析结果详情
$id = '1576efef8ea36c74b533238affc3eaec7f94561d'; $profile = $profiler->getProfile($id);
清除所有数据
$profile = $profiler->getDatasource()->clear();
使用Monolog处理器
$profiler = ProfilerFactory::build([ // ... ProfilerFactory::LOGGER => Ndrx\Profiler\Components\Logs\Monolog::class ]); // $log is your instance of Monolog\Logger $log->pushHandler($profiler->getLogger();
添加新的收集器
所有数据收集器必须实现以下接口之一
- Ndrx\Profiler\Collectors\Contracts\FinalCollectorInterface 用于仅在进程末尾可用的数据,例如响应数据
- Ndrx\Profiler\Collectors\Contracts\StartCollectorInterface 用于在进程开始时即可用的数据,例如请求数据
- Ndrx\Profiler\Collectors\Contracts\StreamCollectorInterface 用于在进程过程中可用的数据,如日志、事件、查询等
初始收集器
<?php namespace /Your/Namespace; use Ndrx\Profiler\Collectors\Collector; use Ndrx\Profiler\Collectors\Contracts\StartCollectorInterface; class Foo extends Collector implements StartCollectorInterface { /** * Fetch data * @return mixed */ public function resolve() { $this->data = 'bar'; } /** * The path in the final json * @example * path /aa/bb * will be transformed to * { * aa : { * bb: <VALUE OF RESOLVE> * } * } * @return string */ public function getPath() { return 'foo'; } }
最终收集器
<?php namespace /Your/Namespace; use Ndrx\Profiler\Collectors\Collector; use Ndrx\Profiler\Collectors\Contracts\FinalCollectorInterface; class Foo extends Collector implements FinalCollectorInterface { /** * Fetch data * @return mixed */ public function resolve() { $this->data = 'bar'; } /** * The path in the final json * @example * path /aa/bb * will be transformed to * { * aa : { * bb: <VALUE OF RESOLVE> * } * } * @return string */ public function getPath() { return 'foo'; } }
流收集器
流收集器稍微复杂一些,因为你需要监听事件并将事件数据流式传输到数据源。
<?php namespace Ndrx\Profiler\Collectors\Data; use Ndrx\Profiler\Collectors\StreamCollector; use Ndrx\Profiler\Events\Log as LogEvent; use Ndrx\Profiler\JsonPatch; class Log extends StreamCollector { protected function registerListeners() { // add a listener to your event dispatcher, the profiler has a build-in dispatcher than use can use $this->process->getDispatcher()->addListener(LogEvent::EVENT_NAME, function (LogEvent $event) { // fetch event data $this->data = $event->toArray(); // stream to the data source $this->stream(); }); } /** * The path in the final json * @example * path /aa/bb * will be transformed to * { * aa : { * bb: <VALUE OF RESOLVE> * } * } * @return mixed */ public function getPath() { return 'logs'; } /** * Write data in the datasource and clean current buffer * @return mixed */ public function stream() { // generation of the json patch from data $patch = $this->jsonPatch->generate($this->getPath(), JsonPatch::ACTION_ADD, $this->data, true); // save the json patch in the datasource $this->dataSource->save($this->process, [$patch]); // clean data array to avoid duplicate entry $this->data = []; } }
变更日志
请参阅CHANGELOG了解最近有哪些更改。
测试
$ composer test
贡献
请参阅CONTRIBUTING和CONDUCT了解详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件 arnaud.lahaxe[at]versusmind.eu 联系,而不是使用问题跟踪器。
致谢
许可
MIT许可(MIT)。请参阅许可文件获取更多信息。