2017 © Pedro Peláez
 

library profiler

Simple profiler package

image

ndrx/profiler

Simple profiler package

  • Sunday, December 27, 2015
  • by lahaxearnaud
  • Repository
  • 3 Watchers
  • 1 Stars
  • 84 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 5 Open issues
  • 15 Versions
  • 0 % Grown

The README.md

profiler

Latest Version on Packagist ![Software License][ico-license] Build Status ![Quality Status][ico-scrutinizer] Coverage Status SensioLabsInsight, (*1)

Build Status, (*2)

Install

Via Composer, (*3)

``` bash $ composer require ndrx-io/profiler, (*4)


## Usage ### Initialize a profiler ``` php // 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();

Add event to the timeline

``` php $profiler->start('foo', 'Bar'); $profiler->stop('foo'); $this->profiler->monitor('Foobar', function() { // very long process });, (*5)


### Logger ``` php $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');

Get last profils

``` php $profiles = $profiler->getDatasource()->all(0, 10);, (*6)


### Get Profil details ``` php $id = '1576efef8ea36c74b533238affc3eaec7f94561d'; $profile = $profiler->getProfile($id);

Clear all data

``` php $profile = $profiler->getDatasource()->clear();, (*7)


### Use monolog handler ``` php $profiler = ProfilerFactory::build([ // ... ProfilerFactory::LOGGER => Ndrx\Profiler\Components\Logs\Monolog::class ]); // $log is your instance of Monolog\Logger $log->pushHandler($profiler->getLogger();

Add new Collector

All data collector must implements one of those interfaces:, (*8)

  • Ndrx\Profiler\Collectors\Contracts\FinalCollectorInterface For data available only at the end of the process, like response data
  • Ndrx\Profiler\Collectors\Contracts\StartCollectorInterface For data available at the beginning of the process, like request data
  • Ndrx\Profiler\Collectors\Contracts\StreamCollectorInterface For data available during the process like logs, events, query...

Initial collector

``` php <?php, (*9)

namespace /Your/Namespace;, (*10)

use Ndrx\Profiler\Collectors\Collector; use Ndrx\Profiler\Collectors\Contracts\StartCollectorInterface;, (*11)

class Foo extends Collector implements StartCollectorInterface { /** * Fetch data * @return mixed */ public function resolve() { $this->data = 'bar'; }, (*12)

/**
 * 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';
}

}, (*13)


### Final collector ``` php <?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'; } }

Stream collector

Stream collector are a little bit more complex because you need to listen events and stream the event data to the datasource., (*14)

``` php <?php, (*15)

namespace Ndrx\Profiler\Collectors\Data;, (*16)

use Ndrx\Profiler\Collectors\StreamCollector; use Ndrx\Profiler\Events\Log as LogEvent; use Ndrx\Profiler\JsonPatch;, (*17)

class Log extends StreamCollector {, (*18)

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 = [];
}

}, (*19)



## Change log Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. ## Testing ``` bash $ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*20)

Security

If you discover any security related issues, please email arnaud.lahaxe[at]versusmind.eu instead of using the issue tracker., (*21)

Credits

License

The MIT License (MIT). Please see License File for more information., (*22)

The Versions