Base for a static organization website

DebugTimerTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * DebugTimer Test Case
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since debug_kit 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('DebugTimer', 'DebugKit.Lib');
  19. /**
  20. * Class DebugTimerTest
  21. *
  22. * @since debug_kit 2.0
  23. */
  24. class DebugTimerTest extends CakeTestCase {
  25. /**
  26. * tearDown method
  27. *
  28. * @return void
  29. */
  30. public function tearDown() {
  31. DebugTimer::clear();
  32. }
  33. /**
  34. * Start Timer test
  35. *
  36. * @return void
  37. */
  38. public function testTimers() {
  39. $this->assertTrue(DebugTimer::start('test1', 'this is my first test'));
  40. usleep(5000);
  41. $this->assertTrue(DebugTimer::stop('test1'));
  42. $elapsed = DebugTimer::elapsedTime('test1');
  43. $this->assertTrue($elapsed > 0.0050);
  44. $this->assertTrue(DebugTimer::start('test2', 'this is my second test'));
  45. sleep(1);
  46. $this->assertTrue(DebugTimer::stop('test2'));
  47. $elapsed = DebugTimer::elapsedTime('test2');
  48. $expected = stripos(PHP_OS, 'win') === false ? 0.999: 0.95; // Windows timer's precision is bad
  49. $this->assertTrue($elapsed >= $expected);
  50. DebugTimer::start('test3');
  51. $this->assertIdentical(DebugTimer::elapsedTime('test3'), 0);
  52. $this->assertFalse(DebugTimer::stop('wrong'));
  53. }
  54. /**
  55. * test timers with no names.
  56. *
  57. * @return void
  58. */
  59. public function testAnonymousTimers() {
  60. $this->assertTrue(DebugTimer::start());
  61. usleep(2000);
  62. $this->assertTrue(DebugTimer::stop());
  63. $timers = DebugTimer::getAll();
  64. $this->assertEquals(2, count($timers));
  65. end($timers);
  66. $key = key($timers);
  67. $lineNo = __LINE__ - 8;
  68. $file = Debugger::trimPath(__FILE__);
  69. $expected = $file . ' line ' . $lineNo;
  70. $this->assertEquals($expected, $key);
  71. $timer = $timers[$expected];
  72. $this->assertTrue($timer['time'] > 0.0020);
  73. $this->assertEquals($expected, $timers[$expected]['message']);
  74. }
  75. /**
  76. * Assert that nested anonymous timers don't get mixed up.
  77. *
  78. * @return void
  79. */
  80. public function testNestedAnonymousTimers() {
  81. $this->assertTrue(DebugTimer::start());
  82. usleep(100);
  83. $this->assertTrue(DebugTimer::start());
  84. usleep(100);
  85. $this->assertTrue(DebugTimer::stop());
  86. $this->assertTrue(DebugTimer::stop());
  87. $timers = DebugTimer::getAll();
  88. $this->assertEquals(3, count($timers), 'incorrect number of timers %s');
  89. $firstTimerLine = __LINE__ - 9;
  90. $secondTimerLine = __LINE__ - 8;
  91. $file = Debugger::trimPath(__FILE__);
  92. $this->assertTrue(isset($timers[$file . ' line ' . $firstTimerLine]), 'first timer is not set %s');
  93. $this->assertTrue(isset($timers[$file . ' line ' . $secondTimerLine]), 'second timer is not set %s');
  94. $firstTimer = $timers[$file . ' line ' . $firstTimerLine];
  95. $secondTimer = $timers[$file . ' line ' . $secondTimerLine];
  96. $this->assertTrue($firstTimer['time'] > $secondTimer['time']);
  97. }
  98. /**
  99. * test that calling start with the same name does not overwrite previous timers
  100. * and instead adds new ones.
  101. *
  102. * @return void
  103. */
  104. public function testRepeatTimers() {
  105. DebugTimer::start('my timer', 'This is the first call');
  106. usleep(100);
  107. DebugTimer::start('my timer', 'This is the second call');
  108. usleep(100);
  109. DebugTimer::stop('my timer');
  110. DebugTimer::stop('my timer');
  111. $timers = DebugTimer::getAll();
  112. $this->assertEquals(3, count($timers), 'wrong timer count %s');
  113. $this->assertTrue(isset($timers['my timer']));
  114. $this->assertTrue(isset($timers['my timer #2']));
  115. $this->assertTrue($timers['my timer']['time'] > $timers['my timer #2']['time'], 'timer 2 is longer? %s');
  116. $this->assertEquals('This is the first call', $timers['my timer']['message']);
  117. $this->assertEquals('This is the second call #2', $timers['my timer #2']['message']);
  118. }
  119. /**
  120. * testRequestTime
  121. *
  122. * @return void
  123. */
  124. public function testRequestTime() {
  125. $result1 = DebugTimer::requestTime();
  126. usleep(50);
  127. $result2 = DebugTimer::requestTime();
  128. $this->assertTrue($result1 < $result2);
  129. }
  130. /**
  131. * test getting all the set timers.
  132. *
  133. * @return void
  134. */
  135. public function testGetTimers() {
  136. DebugTimer::start('test1', 'this is my first test');
  137. DebugTimer::stop('test1');
  138. usleep(50);
  139. DebugTimer::start('test2');
  140. DebugTimer::stop('test2');
  141. $timers = DebugTimer::getAll();
  142. $this->assertEquals(3, count($timers));
  143. $this->assertTrue(is_float($timers['test1']['time']));
  144. $this->assertTrue(isset($timers['test1']['message']));
  145. $this->assertTrue(isset($timers['test2']['message']));
  146. }
  147. }