shawnlindstrom / laravel-timer
Simple high-resolution monotonic timer
Installs: 33
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/shawnlindstrom/laravel-timer
Requires
- php: ^8.3
- illuminate/support: ^12.0
Requires (Dev)
- laravel/pint: ^1.0
- mockery/mockery: ^1.6
- orchestra/testbench: ^10.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0
README
A modern, high-resolution monotonic timer for Laravel applications. Perfect for benchmarking, performance measurement, and precise timing operations.
Features
- 🚀 High-resolution timing using
hrtime() - ⏱️ Multiple time units (seconds, milliseconds, microseconds, nanoseconds)
- 🎯 Type-safe with PHP 8.3+ enums
- 📊 Laravel service provider & facade support
- ✅ 90%+ test coverage
- 🔒 Strict types throughout
Requirements
- PHP 8.3 or higher
- Laravel 12.x
Installation
Install via Composer:
composer require shawnlindstrom/laravel-timer
The package will automatically register itself via Laravel's package discovery.
Usage
Basic Usage
use shawnlindstrom\LaravelTimer\Timer; use shawnlindstrom\LaravelTimer\TimeUnit; $timer = new Timer; $timer->start(); // Your code to benchmark sleep(2); $timer->stop(); echo $timer->elapsed(); // Returns: 2 (default is seconds)
Different Time Units
$timer = new Timer; $timer->start(); // Your code here... $timer->stop(); // Get elapsed time in different units $seconds = $timer->elapsed(TimeUnit::SECOND); // 2 $microseconds = $timer->elapsed(TimeUnit::MICROSECOND); // 2000 $milliseconds = $timer->elapsed(TimeUnit::MILLISECOND); // 2000000 $nanoseconds = $timer->elapsed(TimeUnit::NANOSECOND); // 2000000000
Using the Facade
use shawnlindstrom\LaravelTimer\TimerFacade as Timer; use shawnlindstrom\LaravelTimer\TimeUnit; Timer::start(); // Your code to benchmark... Timer::stop(); echo Timer::elapsed(TimeUnit::MICROSECOND);
Alias Usage
If you prefer, you can use the pre-configured alias:
use Timer; use shawnlindstrom\LaravelTimer\TimeUnit; Timer::start(); // Your code... Timer::stop(); echo Timer::elapsed(TimeUnit::MILLISECOND);
Reusable Measurements
The timer can be reused for multiple measurements:
$timer = new Timer; // First measurement $timer->start(); performTask1(); $timer->stop(); $time1 = $timer->elapsed(); // Second measurement $timer->start(); performTask2(); $timer->stop(); $time2 = $timer->elapsed();
Testing
Run the test suite:
composer test
Generate code coverage report:
composer test:coverage
Run static analysis:
composer analyse
Format code:
composer format
Change Log
Please see CHANGELOG.md for more information on what has changed recently.
Contributing
Please see CONTRIBUTING.md for details.
Security
If you discover any security related issues, please email shawn@tenerant.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see LICENSE.md for more information.