csa/xhprof-profiler

A small abstraction in front of the xhprof extension, with storage support

v1.1.0 2014-05-25 15:27 UTC

README

Latest Stable Version Latest Unstable Version SensioLabs Insight Scrutinizer Quality Score Code Coverage Build Status

Installation

composer require csa/xhprof-profiler:dev-master

Usage

Create any type of PHP web or console application, then use the following in your code:

<?php

require_once __DIR__ . '/vendor/autoload.php';

use XhProf\Profiler;

$profiler = new Profiler();

$profiler->start();

// Your code

// You may either use $profiler->stop() at the end of the code you wish to do something with the trace,
// or let xhprof-profiler manage it, as it registers a shutdown function automatically.
$trace = $profiler->stop();

You can easily store a trace using any storage class implementing StorageInterface:

<?php

require_once __DIR__ . '/vendor/autoload.php';

use XhProf\Profiler;
use XhProf\Storage\MemoryStorage;

$profiler = new Profiler();
$profiler->start();
$trace = $profiler->stop();
$storage = new MemoryStorage();
$storage->store($trace);

You may also override the profiler's shutdown function:

<?php

require_once __DIR__ . '/vendor/autoload.php';

use XhProf\Storage\FileStorage;
use XhProf\Profiler;
use XhProf\Trace;

$profiler = new Profiler();

$callback = function (Trace $trace) {
    $storage = new FileStorage();
    $storage->store($trace);

    // Do whatever you want with the trace
};

$profiler->setShutdownFunction($callback);
$profiler->start();

If you are using the file storage implementation, you may fetch a trace simply by using the fetch method. You may also fetch the list of available tokens:

<?php

require_once __DIR__ . '/vendor/autoload.php';

use XhProf\Storage\FileStorage;

$storage = new FileStorage();
echo implode(PHP_EOL, $storage->getTokens());

Todo

  • Improve context support (for both Cli or request contexts).

License

This library is under the MIT license. For the full copyright and license information, please view the LICENSE file that was distributed with this source code.

Bitdeli Badge