Commit 291dbd54 authored by alexpott's avatar alexpott

Issue #1935970 by msonnabaum, dawehner, RobLoach: Convert timer_* to a utility...

Issue #1935970 by msonnabaum, dawehner, RobLoach: Convert timer_* to a utility class and convert tests to phpunit.
parent c0eaf0f5
......@@ -2,6 +2,7 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Settings;
use Drupal\Component\Utility\Timer;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder;
......@@ -334,71 +335,27 @@
define('DRUPAL_ROOT', dirname(dirname(__DIR__)));
/**
* Starts the timer with the specified name.
*
* If you start and stop the same timer multiple times, the measured intervals
* will be accumulated.
*
* @param $name
* The name of the timer.
* @deprecated as of Drupal 8.0.
* @see \Drupal\Component\Utility\Timer::start
*/
function timer_start($name) {
global $timers;
$timers[$name]['start'] = microtime(TRUE);
$timers[$name]['count'] = isset($timers[$name]['count']) ? ++$timers[$name]['count'] : 1;
Timer::start($name);
}
/**
* Reads the current timer value without stopping the timer.
*
* @param $name
* The name of the timer.
*
* @return
* The current timer value in ms.
* @deprecated as of Drupal 8.0.
* @see \Drupal\Component\Utility\Timer::read
*/
function timer_read($name) {
global $timers;
if (isset($timers[$name]['start'])) {
$stop = microtime(TRUE);
$diff = round(($stop - $timers[$name]['start']) * 1000, 2);
if (isset($timers[$name]['time'])) {
$diff += $timers[$name]['time'];
}
return $diff;
}
return $timers[$name]['time'];
return Timer::read($name);
}
/**
* Stops the timer with the specified name.
*
* @param $name
* The name of the timer.
*
* @return
* A timer array. The array contains the number of times the timer has been
* started and stopped (count) and the accumulated timer value in ms (time).
* @deprecated as of Drupal 8.0.
* @see \Drupal\Component\Utility\Timer::stop
*/
function timer_stop($name) {
global $timers;
if (isset($timers[$name]['start'])) {
$stop = microtime(TRUE);
$diff = round(($stop - $timers[$name]['start']) * 1000, 2);
if (isset($timers[$name]['time'])) {
$timers[$name]['time'] += $diff;
}
else {
$timers[$name]['time'] = $diff;
}
unset($timers[$name]['start']);
}
return $timers[$name];
return Timer::stop($name);
}
/**
......@@ -2281,8 +2238,6 @@ function _drupal_exception_handler($exception) {
*/
function _drupal_bootstrap_configuration() {
drupal_environment_initialize();
// Start a page timer:
timer_start('page');
// Initialize the configuration, including variables from settings.php.
drupal_settings_initialize();
......@@ -2292,6 +2247,9 @@ function _drupal_bootstrap_configuration() {
// Activate the class loader.
drupal_classloader();
// Start a page timer:
Timer::start('page');
// Load the procedural configuration system helper functions.
require_once DRUPAL_ROOT . '/core/includes/config.inc';
......
<?php
/**
* @file
* Contains \Drupal\Component\Utility\Timer.
*/
namespace Drupal\Component\Utility;
/**
* Provides helpers to use timers throughout a request.
*/
class Timer {
static protected $timers = array();
/**
* Starts the timer with the specified name.
*
* If you start and stop the same timer multiple times, the measured intervals
* will be accumulated.
*
* @param $name
* The name of the timer.
*/
static public function start($name) {
static::$timers[$name]['start'] = microtime(TRUE);
static::$timers[$name]['count'] = isset(static::$timers[$name]['count']) ? ++static::$timers[$name]['count'] : 1;
}
/**
* Reads the current timer value without stopping the timer.
*
* @param string $name
* The name of the timer.
*
* @return int
* The current timer value in ms.
*/
static public function read($name) {
if (isset(static::$timers[$name]['start'])) {
$stop = microtime(TRUE);
$diff = round(($stop - static::$timers[$name]['start']) * 1000, 2);
if (isset(static::$timers[$name]['time'])) {
$diff += static::$timers[$name]['time'];
}
return $diff;
}
return static::$timers[$name]['time'];
}
/**
* Stops the timer with the specified name.
*
* @param string $name
* The name of the timer.
*
* @return array
* A timer array. The array contains the number of times the timer has been
* started and stopped (count) and the accumulated timer value in ms (time).
*/
static public function stop($name) {
if (isset(static::$timers[$name]['start'])) {
$stop = microtime(TRUE);
$diff = round(($stop - static::$timers[$name]['start']) * 1000, 2);
if (isset(static::$timers[$name]['time'])) {
static::$timers[$name]['time'] += $diff;
}
else {
static::$timers[$name]['time'] = $diff;
}
unset(static::$timers[$name]['start']);
}
return static::$timers[$name];
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Bootstrap\TimerUnitTest.
*/
namespace Drupal\system\Tests\Bootstrap;
use Drupal\simpletest\UnitTestBase;
/**
* Tests timer_read().
*/
class TimerUnitTest extends UnitTestBase {
public static function getInfo() {
return array(
'name' => 'Timer test',
'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
'group' => 'Bootstrap',
);
}
/**
* Tests timer_read() time accumulation accuracy across multiple restarts.
*/
function testTimer() {
timer_start('test');
sleep(1);
$this->assertTrue(timer_read('test') >= 1000, 'Timer measured 1 second of sleeping while running.');
sleep(1);
timer_stop('test');
$this->assertTrue(timer_read('test') >= 2000, 'Timer measured 2 seconds of sleeping after being stopped.');
timer_start('test');
sleep(1);
$this->assertTrue(timer_read('test') >= 3000, 'Timer measured 3 seconds of sleeping after being restarted.');
sleep(1);
$timer = timer_stop('test');
$this->assertTrue(timer_read('test') >= 4000, 'Timer measured 4 seconds of sleeping after being stopped for a second time.');
$this->assertEqual($timer['count'], 2, 'Timer counted 2 instances of being started.');
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Utility\TimerUnitTest.
*/
namespace Drupal\Tests\Component\Utility;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\Utility\Timer;
/**
* Tests the Timer system.
*
* @see \Drupal\Component\Utility\Timer
*/
class TimerUnitTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'Timer test',
'description' => 'Test that Timer::read() works both when a timer is running and when a timer is stopped.',
'group' => 'Bootstrap',
);
}
/**
* Tests Timer::read() time accumulation accuracy across multiple restarts.
*
* @see Drupal\Component\Utility\Timer::read()
*/
public function testTimer() {
Timer::start('test');
usleep(5000);
$value = Timer::read('test');
usleep(5000);
$value2 = Timer::read('test');
usleep(5000);
$value3 = Timer::read('test');
usleep(5000);
$value4 = Timer::read('test');
$this->assertGreaterThanOrEqual(5, $value, 'Timer measured 5 milliseconds of sleeping while running.');
$this->assertLessThan(10, $value, 'Timer measured 5 milliseconds of sleeping while running.');
$this->assertGreaterThanOrEqual(10, $value2, 'Timer measured 10 milliseconds of sleeping while running.');
$this->assertLessThan(15, $value2, 'Timer measured 10 milliseconds of sleeping while running.');
$this->assertGreaterThanOrEqual(15, $value3, 'Timer measured 15 milliseconds of sleeping while running.');
$this->assertLessThan(20, $value3, 'Timer measured 15 milliseconds of sleeping while running.');
$this->assertGreaterThanOrEqual(20, $value4, 'Timer measured 20 milliseconds of sleeping while running.');
$this->assertLessThan(25, $value4, 'Timer measured 20 milliseconds of sleeping while running.');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment