Skip to content

Commit

Permalink
Include running periods in duration
Browse files Browse the repository at this point in the history
StopwatchEvent:
  - method getDuration() now includes periods that are not stopped yet

StopwatchEventTest:
  - added testDurationBeforeStop()
  • Loading branch information
jochenvdv committed Feb 4, 2014
1 parent bea1537 commit d3d097d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Symfony/Component/Stopwatch/StopwatchEvent.php
Expand Up @@ -171,8 +171,17 @@ public function getEndTime()
*/
public function getDuration()
{
$periods = $this->periods;
$stopped = count($periods);
$left = count($this->started) - $stopped;

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

$total = 0;
foreach ($this->periods as $period) {
foreach ($periods as $period) {
$total += $period->getDuration();
}

Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php
Expand Up @@ -82,6 +82,22 @@ public function testDuration()
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
}

public function testDurationBeforeStop()
{
$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(200000);
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);

$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(100000);
$event->stop();
$event->start();
usleep(100000);
$this->assertEquals(100, $event->getDuration(), null, self::DELTA);
}

/**
* @expectedException \LogicException
*/
Expand Down

0 comments on commit d3d097d

Please sign in to comment.