Timer for PHP apps with nested intervals.
composer require nullform/app-timer
use Nullform\AppTimer;
$timer = new AppTimer\Timer("New timer");
$timer->start("First interval");
// Some code...
$interval = $timer->stop(); // Instance of Interval
$timer->start("Second interval");
// Some code...
$duration = $timer->stop()->duration; // float
$report = $timer->report(); // Instance of Report
You can create new (nested) intervals within others.
$timer->start("First interval"); // Parent interval
$timer->start("First nested interval");
// Some code...
$timet->stop(); // Stop first nested interval
$timer->start("Second nested interval");
// Some code...
$timet->stop(); // Stop second nested interval
$timer->stop(); // Stop parent interval
$report = $timer->report(); // Instance of Report
You can add additional information for the timer/interval to be reflected in the report.
$timer = new AppTimer\Timer("New timer", ['Size' => "XXL"]);
$timer->start("New interval", ['Color' => "Red"]);
The report can be generated as a human-readable string or in JSON format. You can save the report to a file.
// Create new timer
$timer = new AppTimer\Timer("New timer");
// Report file options
$timer->report_filename = "AppTimerReport.log";
$timer->report_dir = dirname(__FILE__);
$timer->report_file_append = true;
$timer->start("New interval");
// ...
$timer->stop();
// Create report
$report = $timer->report(); // Instance of Report
$report_json = $report->toJSON();
$report_string = $report->toString();
- Timer::__construct(string $description = "", array $extras = [])
- Timer::start(string $description, array $extras = []): Interval
- Timer::stop(array $extras = []): ?Interval
- Timer::stopAll(): void
- Timer::report(): Report
- Interval::extras(): Extras
- Extras::add(string $key, string $value): int
- Extras::remove(string $key): int
- Extras::get(): array
- Extras::getOne(string $key): ?string
- Report::longestInterval(): ?Interval
- Report::toString(): string
- Report::toJSON(): string
- Report::extras(): Extras