[Stopwatch] Fixed bug in getDuration when counting multiple ongoing periods
This commit is contained in:
parent
2ecd7936b1
commit
af00d8deab
@ -177,12 +177,10 @@ class StopwatchEvent
|
|||||||
public function getDuration()
|
public function getDuration()
|
||||||
{
|
{
|
||||||
$periods = $this->periods;
|
$periods = $this->periods;
|
||||||
$stopped = \count($periods);
|
$left = \count($this->started);
|
||||||
$left = \count($this->started) - $stopped;
|
|
||||||
|
|
||||||
for ($i = 0; $i < $left; ++$i) {
|
for ($i = $left - 1; $i >= 0; --$i) {
|
||||||
$index = $stopped + $i;
|
$periods[] = new StopwatchPeriod($this->started[$i], $this->getNow(), $this->morePrecision);
|
||||||
$periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), $this->morePrecision);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$total = 0;
|
$total = 0;
|
||||||
|
@ -99,8 +99,25 @@ class StopwatchEventTest extends TestCase
|
|||||||
$event->stop();
|
$event->stop();
|
||||||
usleep(50000);
|
usleep(50000);
|
||||||
$event->start();
|
$event->start();
|
||||||
usleep(100000);
|
|
||||||
$this->assertEqualsWithDelta(100, $event->getDuration(), self::DELTA);
|
$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()
|
public function testStopWithoutStart()
|
||||||
|
Reference in New Issue
Block a user