2011-10-17 09:25:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-31 22:00:32 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-10-17 09:25:52 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
2012-03-31 22:00:32 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-10-17 09:25:52 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\HttpKernel\Debug;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents an Event managed by Stopwatch.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
|
|
|
class StopwatchEvent
|
|
|
|
{
|
|
|
|
private $periods;
|
|
|
|
private $origin;
|
|
|
|
private $category;
|
|
|
|
private $started;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param float $origin The origin time in milliseconds
|
|
|
|
* @param string $category The event category
|
2012-01-25 13:27:59 +00:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When the raw time is not valid
|
2011-10-17 09:25:52 +01:00
|
|
|
*/
|
|
|
|
public function __construct($origin, $category = null)
|
|
|
|
{
|
2012-01-25 13:27:59 +00:00
|
|
|
$this->origin = $this->formatTime($origin);
|
2012-01-10 21:23:16 +00:00
|
|
|
$this->category = is_string($category) ? $category : 'default';
|
2011-10-17 09:25:52 +01:00
|
|
|
$this->started = array();
|
|
|
|
$this->periods = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the category.
|
|
|
|
*
|
|
|
|
* @return string The category
|
|
|
|
*/
|
|
|
|
public function getCategory()
|
|
|
|
{
|
|
|
|
return $this->category;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the origin.
|
|
|
|
*
|
|
|
|
* @return integer The origin in milliseconds
|
|
|
|
*/
|
|
|
|
public function getOrigin()
|
|
|
|
{
|
|
|
|
return $this->origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a new event period.
|
2012-01-10 21:23:16 +00:00
|
|
|
*
|
2012-01-30 15:14:38 +00:00
|
|
|
* @return StopwatchEvent The event
|
2011-10-17 09:25:52 +01:00
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
$this->started[] = $this->getNow();
|
2012-01-10 21:23:16 +00:00
|
|
|
|
|
|
|
return $this;
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the last started event period.
|
2012-01-10 21:23:16 +00:00
|
|
|
*
|
2012-01-30 15:14:38 +00:00
|
|
|
* @return StopwatchEvent The event
|
2011-10-17 09:25:52 +01:00
|
|
|
*/
|
|
|
|
public function stop()
|
|
|
|
{
|
|
|
|
if (!count($this->started)) {
|
|
|
|
throw new \LogicException('stop() called but start() has not been called before.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->periods[] = array(array_pop($this->started), $this->getNow());
|
2012-01-10 21:23:16 +00:00
|
|
|
|
|
|
|
return $this;
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the current period and then starts a new one.
|
2012-01-10 21:23:16 +00:00
|
|
|
*
|
2012-01-30 15:14:38 +00:00
|
|
|
* @return StopwatchEvent The event
|
2011-10-17 09:25:52 +01:00
|
|
|
*/
|
|
|
|
public function lap()
|
|
|
|
{
|
2012-01-10 21:23:16 +00:00
|
|
|
return $this->stop()->start();
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops all non already stopped periods.
|
|
|
|
*/
|
|
|
|
public function ensureStopped()
|
|
|
|
{
|
|
|
|
while (count($this->started)) {
|
|
|
|
$this->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all event periods.
|
|
|
|
*
|
|
|
|
* @return array An array of periods
|
|
|
|
*/
|
|
|
|
public function getPeriods()
|
|
|
|
{
|
|
|
|
return $this->periods;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the relative time of the start of the first period.
|
|
|
|
*
|
|
|
|
* @return integer The time (in milliseconds)
|
|
|
|
*/
|
|
|
|
public function getStartTime()
|
|
|
|
{
|
|
|
|
return isset($this->periods[0]) ? $this->periods[0][0] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the relative time of the end of the last period.
|
|
|
|
*
|
|
|
|
* @return integer The time (in milliseconds)
|
|
|
|
*/
|
|
|
|
public function getEndTime()
|
|
|
|
{
|
2012-01-25 13:27:59 +00:00
|
|
|
return ($count = count($this->periods)) ? $this->periods[$count - 1][1] : 0;
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the total time of all periods.
|
|
|
|
*
|
|
|
|
* @return integer The time (in milliseconds)
|
|
|
|
*/
|
|
|
|
public function getTotalTime()
|
|
|
|
{
|
|
|
|
$total = 0;
|
|
|
|
foreach ($this->periods as $period) {
|
|
|
|
$total += $period[1] - $period[0];
|
|
|
|
}
|
|
|
|
|
2012-01-25 13:27:59 +00:00
|
|
|
return $this->formatTime($total);
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 15:14:38 +00:00
|
|
|
/**
|
|
|
|
* Return the current time relative to origin.
|
|
|
|
*
|
|
|
|
* @return float Time in ms
|
|
|
|
*/
|
|
|
|
protected function getNow()
|
2011-10-17 09:25:52 +01:00
|
|
|
{
|
2012-01-25 13:27:59 +00:00
|
|
|
return $this->formatTime(microtime(true) * 1000 - $this->origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a time.
|
|
|
|
*
|
|
|
|
* @param numerical $time A raw time
|
|
|
|
*
|
|
|
|
* @return float The formatted time
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When the raw time is not valid
|
|
|
|
*/
|
|
|
|
private function formatTime($time)
|
|
|
|
{
|
|
|
|
if (!is_numeric($time)) {
|
|
|
|
throw new \InvalidArgumentException('The time must be a numerical value');
|
|
|
|
}
|
|
|
|
|
|
|
|
return round($time, 1);
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
}
|