No Description

TimerTest.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * PHP_Timer
  4. *
  5. * Copyright (c) 2010-2013, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHP
  38. * @subpackage Timer
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2010 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  42. * @link http://github.com/sebastianbergmann/php-timer
  43. * @since File available since Release 1.0.0
  44. */
  45. require_once dirname(dirname(__FILE__)) . '/PHP/Timer.php';
  46. /**
  47. * Tests for PHP_Timer.
  48. *
  49. * @package PHP
  50. * @subpackage Timer
  51. * @author Sebastian Bergmann <sebastian@phpunit.de>
  52. * @copyright 2010-2013 Sebastian Bergmann <sebastian@phpunit.de>
  53. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  54. * @version Release: @package_version@
  55. * @link http://github.com/sebastianbergmann/php-timer
  56. * @since Class available since Release 1.0.0
  57. */
  58. class PHP_TimerTest extends PHPUnit_Framework_TestCase
  59. {
  60. /**
  61. * @covers PHP_Timer::start
  62. * @covers PHP_Timer::stop
  63. */
  64. public function testStartStop()
  65. {
  66. $this->assertInternalType('float', PHP_Timer::stop());
  67. }
  68. /**
  69. * @covers PHP_Timer::secondsToTimeString
  70. * @dataProvider secondsProvider
  71. */
  72. public function testSecondsToTimeString($string, $seconds)
  73. {
  74. $this->assertEquals(
  75. $string, PHP_Timer::secondsToTimeString($seconds)
  76. );
  77. }
  78. /**
  79. * @covers PHP_Timer::timeSinceStartOfRequest
  80. */
  81. public function testTimeSinceStartOfRequest()
  82. {
  83. $this->assertStringMatchesFormat(
  84. '%f %s', PHP_Timer::timeSinceStartOfRequest()
  85. );
  86. }
  87. /**
  88. * @covers PHP_Timer::resourceUsage
  89. */
  90. public function testResourceUsage()
  91. {
  92. $this->assertStringMatchesFormat(
  93. 'Time: %s, Memory: %s', PHP_Timer::resourceUsage()
  94. );
  95. }
  96. public function secondsProvider()
  97. {
  98. return array(
  99. array('0 ms', 0),
  100. array('1 ms', .001),
  101. array('10 ms', .01),
  102. array('100 ms', .1),
  103. array('999 ms', .999),
  104. array('1 second', .9999),
  105. array('1 second', 1),
  106. array('2 seconds', 2),
  107. array('59.9 seconds', 59.9),
  108. array('59.99 seconds', 59.99),
  109. array('59.99 seconds', 59.999),
  110. array('1 minute', 59.9999),
  111. array('59 seconds', 59.001),
  112. array('59.01 seconds', 59.01),
  113. array('1 minute', 60),
  114. array('1.01 minutes', 61),
  115. array('2 minutes', 120),
  116. array('2.01 minutes', 121),
  117. array('59.99 minutes', 3599.9),
  118. array('59.99 minutes', 3599.99),
  119. array('59.99 minutes', 3599.999),
  120. array('1 hour', 3599.9999),
  121. array('59.98 minutes', 3599.001),
  122. array('59.98 minutes', 3599.01),
  123. array('1 hour', 3600),
  124. array('1 hour', 3601),
  125. array('1 hour', 3601.9),
  126. array('1 hour', 3601.99),
  127. array('1 hour', 3601.999),
  128. array('1 hour', 3601.9999),
  129. array('1.01 hours', 3659.9999),
  130. array('1.01 hours', 3659.001),
  131. array('1.01 hours', 3659.01),
  132. array('2 hours', 7199.9999),
  133. );
  134. }
  135. }