From 32bbba88ce69a91a5a836ac115d5076915e6d6c1 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 3 Mar 2020 11:10:40 -0600 Subject: [PATCH] Add timing information to events This fixes #390 and expands on #392. Additionally, the change to referencing the Dispatcher contract rather than the implementation allows for greater flexibility (specifically I'm using it with October CMS, which has its own event dispatcher that implements the Dispatcher contract). While the timing information isn't 100% accurate, as the first event will be tracked if it took all of the time from when the collector was setup to when the event fired, and any future events are really just tracking the time between itself and the previous event, this is still valuable information that is close enough to reality to act upon. Additionally, this collector was setup from the start to track event timings, but was never actually implemented properly as it would record the start and end of the event as being the same time, always resulting in a timing of 0. --- src/DataCollector/EventCollector.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/DataCollector/EventCollector.php b/src/DataCollector/EventCollector.php index 572cab5..d3974a4 100644 --- a/src/DataCollector/EventCollector.php +++ b/src/DataCollector/EventCollector.php @@ -3,7 +3,7 @@ namespace Barryvdh\Debugbar\DataCollector; use Barryvdh\Debugbar\DataFormatter\SimpleFormatter; use DebugBar\DataCollector\TimeDataCollector; -use Illuminate\Events\Dispatcher; +use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Str; use Symfony\Component\VarDumper\Cloner\VarCloner; @@ -12,16 +12,20 @@ class EventCollector extends TimeDataCollector /** @var Dispatcher */ protected $events; + /** @var integer */ + protected $previousTime; + public function __construct($requestStartTime = null) { parent::__construct($requestStartTime); + $this->previousTime = microtime(true); $this->setDataFormatter(new SimpleFormatter()); } public function onWildcardEvent($name = null, $data = []) { $params = $this->prepareParams($data); - $time = microtime(true); + $currentTime = microtime(true); // Find all listeners for the current event foreach ($this->events->getListeners($name) as $i => $listener) { @@ -57,7 +61,8 @@ class EventCollector extends TimeDataCollector $params['listeners.' . $i] = $listener; } - $this->addMeasure($name, $time, $time, $params); + $this->addMeasure($name, $this->previousTime, $currentTime, $params); + $this->previousTime = $currentTime; } public function subscribe(Dispatcher $events)