3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\Timer;
6 use PHPUnit\Framework\TestCase;
9 * Tests the Timer system.
13 * @coversDefaultClass \Drupal\Component\Utility\Timer
15 class TimerTest extends TestCase {
18 * Tests Timer::read() time accumulation accuracy across multiple restarts.
24 public function testTimer() {
27 $value = Timer::read('test');
29 $value2 = Timer::read('test');
31 $value3 = Timer::read('test');
33 $value4 = Timer::read('test');
35 // Although we sleep for 5 milliseconds, we should test that at least 4 ms
36 // have past because usleep() is not reliable on Windows. See
37 // http://php.net/manual/function.usleep.php for more information. The
38 // purpose of the test to validate that the Timer class can measure elapsed
39 // time not the granularity of usleep() on a particular OS.
40 $this->assertGreaterThanOrEqual(4, $value, 'Timer failed to measure at least 4 milliseconds of sleeping while running.');
42 $this->assertGreaterThanOrEqual($value + 4, $value2, 'Timer failed to measure at least 8 milliseconds of sleeping while running.');
44 $this->assertGreaterThanOrEqual($value2 + 4, $value3, 'Timer failed to measure at least 12 milliseconds of sleeping while running.');
46 $this->assertGreaterThanOrEqual($value3 + 4, $value4, 'Timer failed to measure at least 16 milliseconds of sleeping while running.');
49 $value5 = Timer::stop('test');
50 $this->assertGreaterThanOrEqual($value4, $value5['time'], 'Timer measured after stopping was not greater than last measurement.');
53 $value6 = Timer::read('test');
54 $this->assertEquals($value5['time'], $value6, 'Timer measured after stopping was not equal to the stopped time.');
59 $value7 = Timer::read('test');
60 $this->assertGreaterThanOrEqual($value6 + 4, $value7, 'Timer failed to measure at least 16 milliseconds of sleeping while running.');
63 $value8 = Timer::stop('test');
64 $value9 = Timer::read('test');
65 $this->assertEquals($value8['time'], $value9, 'Timer measured after stopping not equal to stop time.');