Add Timer class

This commit is contained in:
Kijin Sung 2016-03-17 16:04:36 +09:00
parent a9d6cf598d
commit 63006e025a
2 changed files with 172 additions and 0 deletions

109
common/framework/timer.php Normal file
View file

@ -0,0 +1,109 @@
<?php
namespace Rhymix\Framework;
/**
* The timer class.
*/
class Timer
{
/**
* Timestamps are stored here.
*/
protected static $_timestamps = [];
/**
* Start a timer.
*
* This method returns the current microtime.
*
* @param string $name (optional)
* @return float
*/
public static function start($name = null)
{
$timestamp = microtime(true);
if ($name === null)
{
$name = 'anon-timer-' . $timestamp;
}
self::$_timestamps[$name] = $timestamp;
return $timestamp;
}
/**
* Stop a timer and return the elapsed time.
*
* If the name is not given, the most recently started timer will be stopped.
* If no timer has been started, this method returns false.
*
* @param string $name (optional)
* @return float|false
*/
public static function stop($name = null)
{
$timestamp = microtime(true);
$started_timestamp = 0;
if ($name === null)
{
if (count(self::$_timestamps))
{
$started_timestamp = array_pop(self::$_timestamps);
}
else
{
return false;
}
}
elseif (array_key_exists($name, self::$_timestamps))
{
$started_timestamp = self::$_timestamps[$name];
unset(self::$_timestamps[$name]);
}
else
{
return false;
}
return $timestamp - $started_timestamp;
}
/**
* Stop a timer and return the elapsed time in a human-readable format.
*
* If the name is not given, the most recently started timer will be stopped.
* If no timer has been started, this method returns false.
*
* @param string $name (optional)
* @return string|false
*/
public static function stopFormat($name = null)
{
$result = self::stop($name);
if ($result === false) return $result;
return number_format($result * 1000, 1, '.', ',') . 'ms';
}
/**
* This method returns how much time has elapsed since Rhymix startup.
*
* @return float
*/
public static function sinceStartup()
{
return microtime(true) - \RX_MICROTIME;
}
/**
* This method returns how much time has elapsed since startup in a human-readable format.
*
* @return string
*/
public static function sinceStartupFormat()
{
return number_format((microtime(true) - \RX_MICROTIME) * 1000, 1, '.', ',') . 'ms';
}
}

View file

@ -0,0 +1,63 @@
<?php
class TimerTest extends \Codeception\TestCase\Test
{
function testStartStop()
{
$t1 = microtime(true);
$started = Rhymix\Framework\Timer::start();
$t2 = microtime(true);
$elapsed = Rhymix\Framework\Timer::stop();
$t3 = microtime(true);
$this->assertGreaterThanOrEqual($t1, $started);
$this->assertLessThanOrEqual($t2, $started);
$this->assertGreaterThanOrEqual($t2 - $started, $elapsed);
$this->assertLessThanOrEqual($t3 - $t1, $elapsed);
$this->assertGreaterThan(0, $elapsed);
}
function testNestedTimers()
{
$t1 = Rhymix\Framework\Timer::start();
$t2 = Rhymix\Framework\Timer::start();
$t3 = Rhymix\Framework\Timer::stop();
$t4 = Rhymix\Framework\Timer::stop();
$this->assertGreaterThanOrEqual($t1, $t2);
$this->assertGreaterThan($t4, $t3);
}
function testMultipleTimers()
{
$t1 = Rhymix\Framework\Timer::start('timer1');
usleep(5000);
$t2 = Rhymix\Framework\Timer::start('timer2');
$t3 = Rhymix\Framework\Timer::stop('timer1');
usleep(2000);
$t4 = Rhymix\Framework\Timer::stop('timer2');
$this->assertGreaterThanOrEqual($t1, $t2);
$this->assertGreaterThanOrEqual($t4, $t3);
}
function testTimerFormat()
{
$t1 = Rhymix\Framework\Timer::start();
$t2 = Rhymix\Framework\Timer::stopFormat();
$this->assertRegexp('/^[0-9\.]+ms$/', $t2);
}
function testTimerSinceStartup()
{
$t1 = Rhymix\Framework\Timer::sinceStartup();
$t2 = Rhymix\Framework\Timer::sinceStartup();
$this->assertGreaterThanOrEqual($t1, $t2);
$t3 = Rhymix\Framework\Timer::sinceStartupFormat();
$this->assertRegexp('/^[0-9\.]+ms$/', $t3);
}
}