Skip to content

Commit

Permalink
[Stopwatch] Fixed bug in getDuration when counting multiple ongoing p…
Browse files Browse the repository at this point in the history
…eriods
  • Loading branch information
TimoBakx committed Oct 29, 2019
1 parent 2ecd793 commit af00d8d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/Symfony/Component/Stopwatch/StopwatchEvent.php
Expand Up @@ -177,12 +177,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;
Expand Down
19 changes: 18 additions & 1 deletion src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php
Expand Up @@ -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()
Expand Down

0 comments on commit af00d8d

Please sign in to comment.