8000 Include running periods in duration · symfony/symfony@d3d097d · GitHub
[go: up one dir, main page]

Skip to content

Commit d3d097d

Browse files
committed
Include running periods in duration
StopwatchEvent: - method getDuration() now includes periods that are not stopped yet StopwatchEventTest: - added testDurationBeforeStop()
1 parent bea1537 commit d3d097d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Symfony/Component/Stopwatch/StopwatchEvent.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,17 @@ public function getEndTime()
171171
*/
172172
public function getDuration()
173173
{
174+
$periods = $this->periods;
175+
$stopped = count($periods);
176+
$left = count($this->started) - $stopped;
177+
178+
for ($i = 0; $i < $left; $i++) {
179+
$index = $stopped + $i;
180+
$periods[] = new StopwatchPeriod($this->started[$index], $this->getNow());
181+
}
182+
174183
$total = 0;
175-
foreach ($this->periods as $period) {
184+
foreach ($periods as $period) {
176185
$total += $period->getDuration();
177186
}
178187

src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ public function testDuration()
8282
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
8383
}
8484

85+
public function testDurationBeforeStop()
86+
{
87+
$event = new StopwatchEvent(microtime(true) * 1000);
88+
$event->start();
89+
usleep(200000);
90+
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
91+
92+
$event = new StopwatchEvent(microtime(true) * 1000);
93+
$event->start();
94+
usleep(100000);
95+
$event->stop();
96+
$event->start();
97+
usleep(100000);
98+
$this->assertEquals(100, $event->getDuration(), null, self::DELTA);
99+
}
100+
85101
/**
86102
* @expectedException \LogicException
87103
*/

0 commit comments

Comments
 (0)
0