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

Skip to content

Commit 9162892

Browse files
committed
bug #34181 [Stopwatch] Fixed bug in getDuration when counting multiple ongoing periods (TimoBakx)
This PR was merged into the 3.4 branch. Discussion ---------- [Stopwatch] Fixed bug in getDuration when counting multiple ongoing periods | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #34087 | License | MIT | Doc PR | N/A When running multiple periods in StopwatchEvent (start multiple times and not stop them all), the getDuration() method would return unexpected values. This was because at every stop, the last entry in the `started` array was removed, while the `getDuration` method was still expecting all the started events to still be there. Now, when calling `getDuration`, the duration of all the finished periods are added together with the unfinished counts. Commits ------- af00d8d [Stopwatch] Fixed bug in getDuration when counting multiple ongoing periods
2 parents 767b265 + af00d8d commit 9162892

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
@@ -185,12 +185,10 @@ public function getEndTime()
185185
public function getDuration()
186186
{
187187
$periods = $this->periods;
188-
$stopped = \count($periods);
189-
$left = \count($this->started) - $stopped;
188+
$left = \count($this->started);
190189

191-
for ($i = 0; $i < $left; ++$i) {
192-
$index = $stopped + $i;
193-
$periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), $this->morePrecision);
190+
for ($i = $left - 1; $i >= 0; --$i) {
191+
$periods[] = new StopwatchPeriod($this->started[$i], $this->getNow(), $this->morePrecision);
194192
}
195193

196194
$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)