Skip to content

[Stopwatch] StopwatchEvent->getDuration() incorrect #34087

Closed
@sirian

Description

@sirian

Symfony version(s) affected: ^4.3

How to reproduce

$event = new \Symfony\Component\Stopwatch\StopwatchEvent(0, null, true);

$event->start();
usleep(100000);
$event->stop();
var_dump($event->getDuration());
$event->start();
$event->start();
$event->start();
usleep(100000);
var_dump($event->getDuration());
$event->stop();
var_dump($event->getDuration());
$event->stop();
var_dump($event->getDuration());

Output:

float(104.5)
float(306.80004882812)
float(205.69995117188) // WTF?
float(308.80004882812)

Possible Solution
Bug lives here: https://github.com/symfony/stopwatch/blob/9e2c41817c3e3d506e5db1d052cbfbbc6f685199/StopwatchEvent.php#L184

 for ($i = 0; $i < $left; ++$i) {
            $index = $stopped + $i;
            $periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), $this->morePrecision);
        }

$this->started contains only active sessions. So $index calculation is wrong here

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions