Search by

webservco / stopwatch

ariademur

A PHP component/library. Lap timer for measuring how long each part of a request or script takes.

Package info

github.com/webservco/stopwatch

Homepage

pkg:composer/webservco/stopwatch

Statistics

Installs: 206

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

v2.3.0 2026-10-11 16:34 UTC

This package is auto-updated.

Last update: 2026-10-11 16:43:07 UTC


README

A PHP component/library. Please see webservco/sandbox for an example project using these types of components.

Lap timer for measuring how long each part of a request or script takes.

  • Related: webservco/dependency-container (shares one timer across the application), webservco/application-default (logs the statistics at shutdown)

Installation

composer require webservco/stopwatch

Usage

Take the start time on the first line of the entry point, before autoloading, and pass it to start().

use WebServCo\Stopwatch\Factory\LapTimerFactory;

$startTime = (int) hrtime(true);

require 'vendor/autoload.php';

$lapTimerFactory = new LapTimerFactory();
// \WebServCo\Stopwatch\Contract\LapTimerInterface
$lapTimer = $lapTimerFactory->createLapTimer();
$lapTimer->start($startTime);
$lapTimer->lap('autoload');

// ... more work ...
$lapTimer->lap('initialization');

// ['laps' => ['autoload' => 1.2, 'initialization' => 3.4], 'totalLaps' => 2, 'totalTime' => 4.6]
$statistics = $lapTimer->getStatistics();

Contracts

Interface Implementation Purpose
Contract\LapTimerInterface Service\LapTimer Named laps and statistics.
Contract\LapTimerFactoryInterface Factory\LapTimerFactory Creates a lap timer.
Contract\StopwatchInterface Service\Stopwatch Start, stop and elapsed time. Used by the lap timer.

Namespaces are relative to WebServCo\Stopwatch\.

Notes

Times are measured with hrtime() in nanoseconds. getStatistics() converts them to milliseconds, while getLapTime() and getTotalTime() return nanoseconds.

Each lap() call measures the time since the previous one. Laps with the same name add up. Use one name inside a loop and you get the total for all iterations.

Development

# Start project.
ddev start
# Install dependencies.
ddev c u
# Run all validations.
ddev c c
# Run unit tests.
ddev c test:dox

License

MIT