Giter VIP home page Giter VIP logo

php-simple-profiler's Introduction

PHP Simple profiler

PHP Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads

Latest Stable Version Latest Version Latest Unstable Version

License Average time to resolve an issue Percentage of issues still open

Demos

See demos

Quickstart

Benchmark
use AlecRabbit\Tools\BenchmarkSymfonyProgressBar;

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

$benchmark = new BenchmarkSymfonyProgressBar(900000);
$benchmark
    ->addFunction('hrtime', true); 
$benchmark
    ->addFunction('microtime', true);
echo $benchmark->run()->noReturns()->report() . PHP_EOL;
echo $benchmark->stat() . PHP_EOL;

Output will be something like:

Results:
Benchmark:
1.  150.1ns (  0.00%) ⟨1⟩ hrtime(boolean) 
2.  157.7ns (  5.07%) ⟨2⟩ microtime(boolean) 
Benchmarked: 2 
Memory: 0.88MB(0.92MB) Real: 2.00MB(2.00MB)

Done in: 2.0s

For more details see examples

Note: Some examples could be not up to date... WIP

WIP

Installation

For now this package is suggested to be used in dev process

composer require --dev alecrabbit/php-simple-profiler

or if you wish

composer require alecrabbit/php-simple-profiler

Benchmark classes

There are moments when you have to choose between two or more different approaches. Benchmark classes is to help you choose which is faster :)

  • Benchmark::class (no default progress bar, silent measurements)
  • BenchmarkSymfonyPB::class (with Symfony progress bar)
 26% [████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░]  1 sec/4 secs
  • BenchmarkSimplePB::class (with star progress bar)
******************************
Example

Let's say you want to know which is faster call_user_func($func) or $func(). First you need to create an instance of Benchmark class

$b = new BenchmarkSymfonyPB(900000) // with Symfony Progress bar, 900000 measurments

Note: Xdebug extension slowing things down a lot! Disable it (I'm using two different images w/o Xdebug and with Xdebug)

Then you have to add functions to test. But first let's add a closure:

$func = function (array $a) {
    return array_sum($a);
};

Now we are ready to add functions:

$a = [1, 2, 3];

$b->addFunction('call_user_func', $func, $a);

$b->addFunction($func, $a);

And now you can run the benchmarking

$b->run();

Getting results

$report = $b->report(); // you can get report object and use data from it 
echo $report . PHP_EOL; // or you can print it by default formatter
echo $b->stat() . PHP_EOL;

Results will be something like that

Results:
Benchmark:
1.  219.3ns (  0.00%) $func(array) $func(...$args)
2.  287.4ns ( 31.09%) call_user_func(array) \call_user_func($func, ...$args)
All returns are equal: 
integer(6) 
Benchmarked: 2 
Memory: 0.87MB(0.91MB) Real: 2.00MB(2.00MB)

Done in: 1.1s

Profiler::class

Profiler is a kinda wrapper for Counter and Timer in case if you need them both.

$profiler = new Profiler();

for ($i = 0; $i < 100; $i++) {
    $profiler->counter()->bump();
    someOperation();
    $profiler->timer()->check();
}

echo $profiler->report() . PHP_EOL;

Counter::class

// todo

Timer::class

// todo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.