Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af00d8d

Browse files
committed
[Stopwatch] Fixed bug in getDuration when counting multiple ongoing periods
1 parent 2ecd793 commit af00d8d

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/Symfony/Component/Stopwatch/StopwatchEvent.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,10 @@ public function getEndTime()
177177
public function getDuration()
178178
{
179179
$periods = $this->periods;
180-
$stopped = \count($periods);
181-
$left = \count($this->started) - $stopped;
180+
$left = \count($this->started);
182181

183-
for ($i = 0; $i < $left; ++$i) {
184-
$index = $stopped + $i;
185-
$periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), $this->morePrecision);
182+
for ($i = $left - 1; $i >= 0; --$i) {
183+
$periods[] = new StopwatchPeriod($this->started[$i], $this->getNow(), $this->morePrecision);
186184
}
187185

188186
$total = 0;

src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,25 @@ public function testDurationBeforeStop()
9999
$event->stop();
100100
usleep(50000);
101101
$event->start();
102-
usleep(100000);
103102
$this->assertEqualsWithDelta(100, $event->getDuration(), self::DELTA);
103+
usleep(100000);
104+
$this->assertEqualsWithDelta(200, $event->getDuration(), self::DELTA);
105+
}
106+
107+
public function testDurationWithMultipleStarts()
108+
{
109+
$event = new StopwatchEvent(microtime(true) * 1000);
110+
$event->start();
111+
usleep(100000);
112+
$event->start();
113+
usleep(100000);
114+
$this->assertEqualsWithDelta(300, $event->getDuration(), self::DELTA);
115+
$event->stop();
116+
$this->assertEqualsWithDelta(300, $event->getDuration(), self::DELTA);
117+
usleep(100000);
118+
$this->assertEqualsWithDelta(400, $event->getDuration(), self::DELTA);
119+
$event->stop();
120+
$this->assertEqualsWithDelta(400, $event->getDuration(), self::DELTA);
104121
}
105122

106123
public function testStopWithoutStart()

0 commit comments

Comments
 (0)