desarrolla2/timer
Lightweight PHP timer utility for measuring execution time. Start/stop laps, track multiple timers, and get elapsed time for profiling and benchmarking small code sections. Suitable for quick performance checks during development.
Installation:
composer require desarrolla2/timer
No service provider or facade registration is required—just autoload the Timer class:
use Desarrolla2\Timer\Timer;
First Use Case: Start a timer in a controller/middleware:
$timer = new Timer('user_action');
$timer->start();
Later, stop and log the duration:
$timer->stop();
$duration = $timer->getDuration(); // Returns seconds as float
Where to Look First:
Timer class methods: start(), stop(), pause(), resume(), getDuration().Request Timing:
// Middleware: Track request processing time
public function handle($request, Closure $next) {
$timer = new Timer('request_' . $request->ip());
$response = $next($request);
$timer->stop();
Log::info("Request took {$timer->getDuration()}s");
return $response;
}
Task Segmentation:
$timer = new Timer('data_processing');
$timer->start();
// ... business logic ...
$timer->pause(); // Pause before I/O operations
sleep(2); // Simulate delay
$timer->resume();
$timer->stop();
Benchmarking:
$timers = [];
$timers['db_query'] = new Timer('db_query');
$timers['db_query']->start();
DB::query()->get(...);
$timers['db_query']->stop();
$total = $timers['db_query']->getDuration();
Laravel Logging:
Use Log::debug() with timer data for structured logs:
Log::debug('Timer', [
'name' => $timer->getName(),
'duration' => $timer->getDuration(),
'status' => $timer->isRunning() ? 'running' : 'stopped',
]);
Artisan Commands: Profile CLI tasks:
$timer = new Timer('artisan_command');
$timer->start();
// ... command logic ...
$timer->stop();
echo "Executed in {$timer->getDuration()}s";
Queue Jobs:
Track job execution time in handle():
public function handle() {
$timer = new Timer('job_' . $this->job->id);
$timer->start();
// ... job logic ...
$timer->stop();
$this->logDuration($timer);
}
Precision:
$timer = new Timer('high_precision', microtime(true));
Static State:
$app->singleton('timers', function() {
return collect();
});
Thread Safety:
Memory Leaks:
unset() or use dependency injection for short-lived timers.Check Running Timers:
if ($timer->isRunning()) {
echo "Timer paused at {$timer->getPausedAt()}s";
}
Log All Timers:
$timer->log(); // Outputs to storage/logs/laravel.log by default
Custom Storage:
Override storeDuration() to save to a database:
$timer->storeDuration(function($duration) {
DB::table('timer_logs')->insert([
'name' => $timer->getName(),
'duration' => $duration,
'created_at' => now(),
]);
});
Event Triggers:
Extend to dispatch events on start/stop:
$timer = new Timer('event_timer');
event(new TimerStarted($timer));
$timer->start();
Time Units: Add custom getters for readability:
public function getDurationInMinutes() {
return $this->getDuration() / 60;
}
$timer = new Timer('custom', null, storage_path('logs/timers.log'));
How can I help you explore Laravel packages today?