-
Notifications
You must be signed in to change notification settings - Fork 1
Quick Start
This page takes you from a working install to a useful measurement in under five minutes.
<?php
require __DIR__ . '/vendor/autoload.php';
use InitPHP\PerformanceMeter\PerformanceMeter;
PerformanceMeter::setPointer('start');
// the work you want to measure
usleep(50_000);
PerformanceMeter::setPointer('end');setPointer($name) captures three things at the moment of the call:
- The current wall-clock time, via
microtime(true) - The emalloc-tracked memory usage, via
memory_get_usage(false) - The system-allocated memory usage, via
memory_get_usage(true)
All three are stored in a single class-level registry, keyed by the lowercased name.
echo PerformanceMeter::elapsedTime('start', 'end'), " seconds\n";
echo PerformanceMeter::memoryUsage('start', 'end'), " memory delta\n";Output looks like:
0.0507 seconds
0.13KB memory delta
elapsedTime() and memoryUsage() both default their second argument to null. When it is null, the current moment is captured and used as the end of the interval:
PerformanceMeter::setPointer('boot');
// ... whole script runs ...
echo PerformanceMeter::elapsedTime('boot'); // seconds since 'boot'You only need an explicit end-checkpoint when you want to stop measuring before the script ends.
mark($name) is a one-to-one alias of setPointer($name). Same state, same effect:
PerformanceMeter::mark('before');
heavy_work();
PerformanceMeter::mark('after');
echo PerformanceMeter::elapsedTime('before', 'after');Every checkpoint lives in the same process-wide registry on the PerformanceMeter class. Two callers using the same name will overwrite each other:
PerformanceMeter::setPointer('x');
sleep(1);
PerformanceMeter::setPointer('x'); // overwrites — the first capture is goneWhen you run independent measurements one after another (e.g. a benchmark loop comparing two implementations), call reset() between them:
foreach ($candidates as $name => $fn) {
PerformanceMeter::reset();
PerformanceMeter::setPointer('s');
$fn();
PerformanceMeter::setPointer('e');
printf("%s: %ss\n", $name, PerformanceMeter::elapsedTime('s', 'e'));
}If you ask for a checkpoint that was never recorded, you get a PointerNotFoundException:
PerformanceMeter::elapsedTime('typo'); // throws PointerNotFoundExceptionThis is deliberate — see Concepts → Fail-fast on unknown names for the reasoning. If you legitimately want a "measure if present" pattern, gate the call with has():
if (PerformanceMeter::has('boot')) {
echo PerformanceMeter::elapsedTime('boot');
}That is the entire model. The remaining wiki pages drill into specific cases:
- Concepts — the mental model in one page (recommended next read).
- API Reference — every method's parameters, return value, throws.
- Recipes — practical patterns: CLI benchmarks, cron timing, A/B comparison, peak memory tracking.
- Use Cases & Comparison — when this package is and is not the right tool.
initphp/performance-meter · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Changelog · Contributing · Security Policy
Getting Started
Reference
Recipes
Migration & Help