diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index b86b056bacad..e6739469de39 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -185,12 +185,10 @@ public function getEndTime() public function getDuration() { $periods = $this->periods; - $stopped = \count($periods); - $left = \count($this->started) - $stopped; + $left = \count($this->started); - for ($i = 0; $i < $left; ++$i) { - $index = $stopped + $i; - $periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), $this->morePrecision); + for ($i = $left - 1; $i >= 0; --$i) { + $periods[] = new StopwatchPeriod($this->started[$i], $this->getNow(), $this->morePrecision); } $total = 0; diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php index 16bfd69404bc..349334db7483 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php @@ -99,8 +99,25 @@ public function testDurationBeforeStop() $event->stop(); usleep(50000); $event->start(); - usleep(100000); $this->assertEqualsWithDelta(100, $event->getDuration(), self::DELTA); + usleep(100000); + $this->assertEqualsWithDelta(200, $event->getDuration(), self::DELTA); + } + + public function testDurationWithMultipleStarts() + { + $event = new StopwatchEvent(microtime(true) * 1000); + $event->start(); + usleep(100000); + $event->start(); + usleep(100000); + $this->assertEqualsWithDelta(300, $event->getDuration(), self::DELTA); + $event->stop(); + $this->assertEqualsWithDelta(300, $event->getDuration(), self::DELTA); + usleep(100000); + $this->assertEqualsWithDelta(400, $event->getDuration(), self::DELTA); + $event->stop(); + $this->assertEqualsWithDelta(400, $event->getDuration(), self::DELTA); } public function testStopWithoutStart()