comparison core/tests/Drupal/Tests/Component/Utility/TimerTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Component\Utility\Timer;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9 * Tests the Timer system.
10 *
11 * @group Utility
12 *
13 * @coversDefaultClass \Drupal\Component\Utility\Timer
14 */
15 class TimerTest extends TestCase {
16
17 /**
18 * Tests Timer::read() time accumulation accuracy across multiple restarts.
19 *
20 * @covers ::start
21 * @covers ::stop
22 * @covers ::read
23 */
24 public function testTimer() {
25 Timer::start('test');
26 usleep(5000);
27 $value = Timer::read('test');
28 usleep(5000);
29 $value2 = Timer::read('test');
30 usleep(5000);
31 $value3 = Timer::read('test');
32 usleep(5000);
33 $value4 = Timer::read('test');
34
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.');
41
42 $this->assertGreaterThanOrEqual($value + 4, $value2, 'Timer failed to measure at least 8 milliseconds of sleeping while running.');
43
44 $this->assertGreaterThanOrEqual($value2 + 4, $value3, 'Timer failed to measure at least 12 milliseconds of sleeping while running.');
45
46 $this->assertGreaterThanOrEqual($value3 + 4, $value4, 'Timer failed to measure at least 16 milliseconds of sleeping while running.');
47
48 // Stop the timer.
49 $value5 = Timer::stop('test');
50 $this->assertGreaterThanOrEqual($value4, $value5['time'], 'Timer measured after stopping was not greater than last measurement.');
51
52 // Read again.
53 $value6 = Timer::read('test');
54 $this->assertEquals($value5['time'], $value6, 'Timer measured after stopping was not equal to the stopped time.');
55
56 // Restart.
57 Timer::start('test');
58 usleep(5000);
59 $value7 = Timer::read('test');
60 $this->assertGreaterThanOrEqual($value6 + 4, $value7, 'Timer failed to measure at least 16 milliseconds of sleeping while running.');
61
62 // Stop again.
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.');
66 }
67
68 }