feature #10198 [Stopwatch] Allow getting duration of events without calling stop() (jochenvdv)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Stopwatch] Allow getting duration of events without calling stop() | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | [#10175](https://github.com/symfony/symfony/issues/10175) | License | MIT | Doc PR | [#3539](https://github.com/symfony/symfony-docs/pull/3539) Commits -------2efe461
Allow retrieving unstopped stopwatch eventsd3d097d
Include running periods in duration
This commit is contained in:
commit
6dfdb97f88
@ -125,6 +125,18 @@ class Stopwatch
|
||||
return end($this->activeSections)->stopEvent($name)->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a specific event by name
|
||||
*
|
||||
* @param string $name The event name
|
||||
*
|
||||
* @return StopwatchEvent A StopwatchEvent instance
|
||||
*/
|
||||
public function getEvent($name)
|
||||
{
|
||||
return end($this->activeSections)->getEvent($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all events for a given section.
|
||||
*
|
||||
@ -293,6 +305,24 @@ class Section
|
||||
return $this->stopEvent($name)->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a specific event by name
|
||||
*
|
||||
* @param string $name The event name
|
||||
*
|
||||
* @return StopwatchEvent The event
|
||||
*
|
||||
* @throws \LogicException When the event is not known
|
||||
*/
|
||||
public function getEvent($name)
|
||||
{
|
||||
if (!isset($this->events[$name])) {
|
||||
throw new \LogicException(sprintf('Event "%s" is not known.', $name));
|
||||
}
|
||||
|
||||
return $this->events[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the events from this section.
|
||||
*
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -29,6 +29,7 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event);
|
||||
$this->assertEquals('cat', $event->getCategory());
|
||||
$this->assertSame($event, $stopwatch->getEvent('foo'));
|
||||
}
|
||||
|
||||
public function testIsStarted()
|
||||
@ -92,6 +93,15 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
*/
|
||||
public function testUnknownEvent()
|
||||
{
|
||||
$stopwatch = new Stopwatch();
|
||||
$stopwatch->getEvent('foo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
*/
|
||||
|
Reference in New Issue
Block a user