This tool allows you to monitor performance and detect memory leaks as well as inconsistent performance behavior of your application over time.
For basic profiling you can use a profiling helper.
The Profiling
will allow you to profile between start
and finish
methods calls.
namespace PetrKnap\Profiler;
$profiling = Profiling::start();
// do something
$profile = $profiling->finish();
printf('It took %.1f s to do something.', $profile->getDuration());
The Profiling
is simple and cannot be turned on and off easily.
So a profiler was created for the purpose of hard-coded more complex profiling.
Request a profiler as a dependency and call a profile
method on it.
namespace PetrKnap\Profiler;
function doSomething(ProfilerInterface $profiler): string {
return $profiler->profile(function (): string {
return 'something';
})->process(fn (ProfileInterface $profile) => printf(
'It took %.1f s to do something.',
$profile->getDuration(),
));
}
It can be easily enabled, or disabled through the DI, which provides either the Profiler
or the NullProfiler
.
namespace PetrKnap\Profiler;
echo doSomething(new Profiler());
echo doSomething(new NullProfiler());
If you need to measure the current values, just call the takeSnapshot
method on the Profiling
, or a profiler.
namespace PetrKnap\Profiler;
$profiling = Profiling::start();
// do something
$profiling->takeSnapshot();
// do something more
$profile = $profiling->finish();
printf('There are %d memory usage records.', count($profile->getMemoryUsages()));
If you want to automate it then take snapshot on tick. Or you can use a more practical cascade profiling.
For greater precision, you can take snapshot on each N
tick.
declare(ticks=2); // this declaration is important (N=2)
namespace PetrKnap\Profiler;
$profiling = Profiling::start(takeSnapshotOnTick: true);
(fn () => 'something')();
$profile = $profiling->finish();
printf('There are %d memory usage records.', count($profile->getMemoryUsages()));
This will result in very detailed code tracking, which can degrade the performance of the monitored application.
The profile
method provides you a nested profiler that you can use for more detailed cascade profiling.
namespace PetrKnap\Profiler;
$profile = (new Profiler())->profile(function (ProfilerInterface $profiler): void {
// do something
$profiler->profile(function (): void {
// do something more
});
});
printf('There are %d memory usage records.', count($profile->getMemoryUsages()));
Run composer require petrknap/profiler
to install it.
You can support this project via donation.
The project is licensed under the terms of the LGPL-3.0-or-later
.