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
-------
af00d8deab
[Stopwatch] Fixed bug in getDuration when counting multiple ongoing periods
This commit is contained in:
commit
9162892fd4
@ -185,12 +185,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