alecrabbit / php-simple-profiler
For functions benchmarking, contain counters and timers classes.
Fund package maintenance!
Patreon
Requires
- php: ^7.2
- alecrabbit/php-accessories: ^0.9
- alecrabbit/php-console-colour: ^0.5
- alecrabbit/php-console-spinner: ^0.14
- alecrabbit/php-counters: ^0.2
- alecrabbit/php-helpers: ^0.6
- alecrabbit/php-reports: ^0.2
- alecrabbit/php-timers: ^0.1
- alecrabbit/php-traits: ^0.3
- sebastian/exporter: ^3.1
Requires (Dev)
- krowinski/bcmath-extended: ^4.2 || ^5.0
- nunomaduro/collision: ^3.0
- php-mock/php-mock-phpunit: ^2.4
- phpunit/phpunit: ^8.0
- symfony/console: ^4.3
- symfony/phpunit-bridge: ^4.3
- symfony/var-dumper: ^4.3
Suggests
- symfony/console: To use symfony progress bar and colored output.
This package is auto-updated.
Last update: 2021-07-10 16:17:39 UTC
README
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