From d3d097d659ec50d2378d1e1384f750fec520e80d Mon Sep 17 00:00:00 2001 From: jochenvdv Date: Tue, 4 Feb 2014 00:11:49 +0100 Subject: [PATCH 1/2] Include running periods in duration StopwatchEvent: - method getDuration() now includes periods that are not stopped yet StopwatchEventTest: - added testDurationBeforeStop() --- .../Component/Stopwatch/StopwatchEvent.php | 11 ++++++++++- .../Stopwatch/Tests/StopwatchEventTest.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index 41d7279e3582..eec98793de6d 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -171,8 +171,17 @@ public function getEndTime() */ public function getDuration() { + $periods = $this->periods; + $stopped = count($periods); + $left = count($this->started) - $stopped; + + for ($i = 0; $i < $left; $i++) { + $index = $stopped + $i; + $periods[] = new StopwatchPeriod($this->started[$index], $this->getNow()); + } + $total = 0; - foreach ($this->periods as $period) { + foreach ($periods as $period) { $total += $period->getDuration(); } diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php index af63153382af..67a381ca2d1a 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php @@ -82,6 +82,22 @@ public function testDuration() $this->assertEquals(200, $event->getDuration(), null, self::DELTA); } + public function testDurationBeforeStop() + { + $event = new StopwatchEvent(microtime(true) * 1000); + $event->start(); + usleep(200000); + $this->assertEquals(200, $event->getDuration(), null, self::DELTA); + + $event = new StopwatchEvent(microtime(true) * 1000); + $event->start(); + usleep(100000); + $event->stop(); + $event->start(); + usleep(100000); + $this->assertEquals(100, $event->getDuration(), null, self::DELTA); + } + /** * @expectedException \LogicException */ From 2efe461a46dfcabc1daa7e51cc23b55d6e91e4ed Mon Sep 17 00:00:00 2001 From: jochenvdv Date: Tue, 4 Feb 2014 12:05:45 +0100 Subject: [PATCH 2/2] Allow retrieving unstopped stopwatch events Section: - added method getEvent() Stopwatch: - added method getEvent() StopwatchTest: - modified testStart() to test for getEvent() method - added testUnknownEvent() --- src/Symfony/Component/Stopwatch/Stopwatch.php | 30 +++++++++++++++++++ .../Stopwatch/Tests/StopwatchTest.php | 10 +++++++ 2 files changed, 40 insertions(+) diff --git a/src/Symfony/Component/Stopwatch/Stopwatch.php b/src/Symfony/Component/Stopwatch/Stopwatch.php index 8631cf89273b..db2395977727 100644 --- a/src/Symfony/Component/Stopwatch/Stopwatch.php +++ b/src/Symfony/Component/Stopwatch/Stopwatch.php @@ -125,6 +125,18 @@ public function lap($name) return end($this->activeSections)->stopEvent($name)->start(); } + /** + * Returns a specific event by name + * + * @param string $name The event name + * + * @return StopwatchEvent A StopwatchEvent instance + */ + public function getEvent($name) + { + return end($this->activeSections)->getEvent($name); + } + /** * Gets all events for a given section. * @@ -293,6 +305,24 @@ public function lap($name) return $this->stopEvent($name)->start(); } + /** + * Returns a specific event by name + * + * @param string $name The event name + * + * @return StopwatchEvent The event + * + * @throws \LogicException When the event is not known + */ + public function getEvent($name) + { + if (!isset($this->events[$name])) { + throw new \LogicException(sprintf('Event "%s" is not known.', $name)); + } + + return $this->events[$name]; + } + /** * Returns the events from this section. * diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php index a8c11424fa91..09be9d27956a 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php @@ -29,6 +29,7 @@ public function testStart() $this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event); $this->assertEquals('cat', $event->getCategory()); + $this->assertSame($event, $stopwatch->getEvent('foo')); } public function testIsStarted() @@ -92,6 +93,15 @@ public function testLap() $this->assertEquals(200, $event->getDuration(), null, self::DELTA); } + /** + * @expectedException \LogicException + */ + public function testUnknownEvent() + { + $stopwatch = new Stopwatch(); + $stopwatch->getEvent('foo'); + } + /** * @expectedException \LogicException */