annotate core/tests/Drupal/Tests/Component/Utility/TimerTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\Component\Utility;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Timer;
Chris@0 6 use PHPUnit\Framework\TestCase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests the Timer system.
Chris@0 10 *
Chris@0 11 * @group Utility
Chris@0 12 *
Chris@0 13 * @coversDefaultClass \Drupal\Component\Utility\Timer
Chris@0 14 */
Chris@0 15 class TimerTest extends TestCase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Tests Timer::read() time accumulation accuracy across multiple restarts.
Chris@0 19 *
Chris@0 20 * @covers ::start
Chris@0 21 * @covers ::stop
Chris@0 22 * @covers ::read
Chris@0 23 */
Chris@0 24 public function testTimer() {
Chris@0 25 Timer::start('test');
Chris@0 26 usleep(5000);
Chris@0 27 $value = Timer::read('test');
Chris@0 28 usleep(5000);
Chris@0 29 $value2 = Timer::read('test');
Chris@0 30 usleep(5000);
Chris@0 31 $value3 = Timer::read('test');
Chris@0 32 usleep(5000);
Chris@0 33 $value4 = Timer::read('test');
Chris@0 34
Chris@0 35 // Although we sleep for 5 milliseconds, we should test that at least 4 ms
Chris@0 36 // have past because usleep() is not reliable on Windows. See
Chris@0 37 // http://php.net/manual/function.usleep.php for more information. The
Chris@0 38 // purpose of the test to validate that the Timer class can measure elapsed
Chris@0 39 // time not the granularity of usleep() on a particular OS.
Chris@0 40 $this->assertGreaterThanOrEqual(4, $value, 'Timer failed to measure at least 4 milliseconds of sleeping while running.');
Chris@0 41
Chris@0 42 $this->assertGreaterThanOrEqual($value + 4, $value2, 'Timer failed to measure at least 8 milliseconds of sleeping while running.');
Chris@0 43
Chris@0 44 $this->assertGreaterThanOrEqual($value2 + 4, $value3, 'Timer failed to measure at least 12 milliseconds of sleeping while running.');
Chris@0 45
Chris@0 46 $this->assertGreaterThanOrEqual($value3 + 4, $value4, 'Timer failed to measure at least 16 milliseconds of sleeping while running.');
Chris@0 47
Chris@0 48 // Stop the timer.
Chris@0 49 $value5 = Timer::stop('test');
Chris@0 50 $this->assertGreaterThanOrEqual($value4, $value5['time'], 'Timer measured after stopping was not greater than last measurement.');
Chris@0 51
Chris@0 52 // Read again.
Chris@0 53 $value6 = Timer::read('test');
Chris@0 54 $this->assertEquals($value5['time'], $value6, 'Timer measured after stopping was not equal to the stopped time.');
Chris@0 55
Chris@0 56 // Restart.
Chris@0 57 Timer::start('test');
Chris@0 58 usleep(5000);
Chris@0 59 $value7 = Timer::read('test');
Chris@0 60 $this->assertGreaterThanOrEqual($value6 + 4, $value7, 'Timer failed to measure at least 16 milliseconds of sleeping while running.');
Chris@0 61
Chris@0 62 // Stop again.
Chris@0 63 $value8 = Timer::stop('test');
Chris@0 64 $value9 = Timer::read('test');
Chris@0 65 $this->assertEquals($value8['time'], $value9, 'Timer measured after stopping not equal to stop time.');
Chris@0 66 }
Chris@0 67
Chris@0 68 }