Include running periods in duration
StopwatchEvent: - method getDuration() now includes periods that are not stopped yet StopwatchEventTest: - added testDurationBeforeStop()
This commit is contained in:
parent
bea1537964
commit
d3d097d659
@ -171,8 +171,17 @@ class StopwatchEvent
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -82,6 +82,22 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase
|
||||
$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
|
||||
*/
|
||||
|
Reference in New Issue
Block a user