webservco / stopwatch
A PHP component/library. Lap timer for measuring how long each part of a request or script takes.
Requires
- php: ^8.4
Requires (Dev)
- pds/skeleton: ^1
- phan/phan: ^6
- php-parallel-lint/php-console-highlighter: ^1
- php-parallel-lint/php-parallel-lint: ^1
- phpmd/phpmd: ^3
- phpstan/phpstan: ^2
- phpstan/phpstan-deprecation-rules: ^2
- phpstan/phpstan-phpunit: ^2
- phpstan/phpstan-strict-rules: ^2
- phpunit/phpunit: ^13
- slevomat/coding-standard: ^8
- squizlabs/php_codesniffer: ^4
- vimeo/psalm: ^6
- webservco/coding-standards: ^2
- webservco/component-common: ^2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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