nullform/app-timer

Timer with nested intervals, execution time measurement

v2.1.0 2024-03-04 10:05 UTC

This package is auto-updated.

Last update: 2024-05-04 10:33:48 UTC


README

Timer for PHP apps with nested intervals.

Installation

composer require nullform/app-timer

Usage

Basic usage

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

Nested intervals

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

Additional information for timer/interval

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"]);

Report

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();

Methods

Timer

  • 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

  • Interval::extras(): Extras

Extras

  • Extras::add(string $key, string $value): int
  • Extras::remove(string $key): int
  • Extras::get(): array
  • Extras::getOne(string $key): ?string

Report

  • Report::longestInterval(): ?Interval
  • Report::toString(): string
  • Report::toJSON(): string
  • Report::extras(): Extras