-
Notifications
You must be signed in to change notification settings - Fork 1
Recipes
Real-world patterns for using PerformanceMeter. Each recipe is self-contained and copy-pasteable into a fresh script.
| Recipe | What it shows |
|---|---|
| CLI Benchmark Script | A bench.php that prints elapsed / memory / peak in one summary line. |
| Cron Job Timing Log | Append one line per scheduled run to a log file for low-fi trend analysis. |
| A/B Comparison of Two Implementations | Reset-between-runs pattern for comparing candidate implementations. |
| Peak Memory Tracking | Tracking the high-water mark through distinct phases of a script. |
Each recipe page calls out the relevant caveats — microbenchmark noise, formatter caveats, why reset() is necessary, etc.
A few patterns that come up in more than one recipe.
The registry is global. If your code might run alongside another library that uses PerformanceMeter, prefix your names with a stable token so you do not collide:
PerformanceMeter::setPointer('mylib:request:start');
PerformanceMeter::setPointer('mylib:request:end');A two- or three-part dotted/colon-separated name is enough — there is no built-in namespacing, this is a convention.
If you want a single "total time" / "peak memory" line at the very end of a script, regardless of how many exit / die paths exist:
PerformanceMeter::setPointer('boot');
register_shutdown_function(static function (): void {
fwrite(STDERR, sprintf(
"elapsed=%ss peak=%s\n",
PerformanceMeter::elapsedTime('boot'),
PerformanceMeter::peakMemoryUsage(),
));
});The shutdown function runs after every code path, including fatal errors, so the measurement always lands.
If you are comparing two implementations and PHP's opcode cache (or your IDE's auto-formatter) reorders cold load behaviour, your first iteration of each will look slower than subsequent ones. The common fix is a warm-up loop you discard:
// warm-up — measurements thrown away
for ($i = 0; $i < 3; $i++) {
$fn();
}
// measured loop
PerformanceMeter::setPointer('s');
for ($i = 0; $i < 1_000; $i++) {
$fn();
}
PerformanceMeter::setPointer('e');For anything serious, use phpbench/phpbench — it handles warm-up, outlier rejection, and statistical confidence properly.
PerformanceMeter::setPointer('handler:start');
try {
$handler->run();
} finally {
PerformanceMeter::setPointer('handler:end');
log_metric(PerformanceMeter::elapsedTime('handler:start', 'handler:end'));
}The measurement records and the metric is logged even if $handler->run() throws.
initphp/performance-meter · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Changelog · Contributing · Security Policy
Getting Started
Reference
Recipes
Migration & Help