/tests/Propel/Tests/Runtime/Util/ProfilerTest.php

https://github.com/hhamon/Propel2 · PHP · 251 lines · 203 code · 23 blank · 25 comment · 0 complexity · 97068ba79f535e54472d1e392198996a MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. namespace Propel\Tests\Runtime\Util;
  10. use Propel\Tests\Helpers\BaseTestCase;
  11. use Propel\Runtime\Util\Profiler;
  12. class ProfilerTest extends BaseTestCase
  13. {
  14. public function testGetProfileBetweenAddsSlowTreshold()
  15. {
  16. $profiler = new Profiler();
  17. $profiler->setDetails(array());
  18. $profiler->setSlowTreshold(1000);
  19. $res = $profiler->getProfileBetween(array('microtime' => 1000), array('microtime' => 1200));
  20. $this->assertEquals(' ', $res);
  21. $res = $profiler->getProfileBetween(array('microtime' => 1000), array('microtime' => 2200));
  22. $this->assertEquals('SLOW ', $res);
  23. }
  24. public function testGetProfileBetweenDoesNotAddSlowTresholdWhenValueIsNull()
  25. {
  26. $profiler = new Profiler();
  27. $profiler->setDetails(array());
  28. $profiler->setSlowTreshold(0);
  29. $res = $profiler->getProfileBetween(array('microtime' => 1000), array('microtime' => 1200));
  30. $this->assertEquals('', $res);
  31. $res = $profiler->getProfileBetween(array('microtime' => 1000), array('microtime' => 2200));
  32. $this->assertEquals('', $res);
  33. }
  34. public function testGetProfileBetweenAddsTime()
  35. {
  36. $profiler = new Profiler();
  37. $profiler->setDetails(array('time' => array('name' => 'Time', 'precision' => 3, 'pad' => 3)));
  38. $profiler->setSlowTreshold(0);
  39. $res = $profiler->getProfileBetween(array('microtime' => 1.000), array('microtime' => 1.234));
  40. $this->assertEquals('Time: 234ms | ', $res);
  41. $res = $profiler->getProfileBetween(array('microtime' => 1.234), array('microtime' => 2.345));
  42. $this->assertEquals('Time: 1.11s | ', $res);
  43. }
  44. public function testGetProfileBetweenAddsMemoryUsage()
  45. {
  46. $profiler = new Profiler();
  47. $profiler->setDetails(array('mem' => array('name' => 'Memory', 'precision' => 3, 'pad' => 3)));
  48. $profiler->setSlowTreshold(0);
  49. $res = $profiler->getProfileBetween(array(), array('memoryUsage' => 343245));
  50. $this->assertEquals('Memory: 335kB | ', $res);
  51. $res = $profiler->getProfileBetween(array(), array('memoryUsage' => 73456345634));
  52. $this->assertEquals('Memory: 68.4GB | ', $res);
  53. }
  54. public function testGetProfileBetweenAddsMemoryDeltaUsage()
  55. {
  56. $profiler = new Profiler();
  57. $profiler->setDetails(array('memDelta' => array('name' => 'Delta', 'precision' => 3, 'pad' => 3)));
  58. $profiler->setSlowTreshold(0);
  59. $res = $profiler->getProfileBetween(array('memoryUsage' => 343245), array('memoryUsage' => 888064));
  60. $this->assertEquals('Delta: +532kB | ', $res);
  61. $res = $profiler->getProfileBetween(array('memoryUsage' => 234523523), array('memoryUsage' => 73456345634));
  62. $this->assertEquals('Delta: +68.2GB | ', $res);
  63. $res = $profiler->getProfileBetween(array('memoryUsage' => 73456345634), array('memoryUsage' => 234523523));
  64. $this->assertEquals('Delta: -68.2GB | ', $res);
  65. }
  66. public function testGetProfileBetweenAddsMemoryPeakUsage()
  67. {
  68. $profiler = new Profiler();
  69. $profiler->setDetails(array('memPeak' => array('name' => 'Peak', 'precision' => 3, 'pad' => 3)));
  70. $profiler->setSlowTreshold(0);
  71. $res = $profiler->getProfileBetween(array(), array('memoryPeakUsage' => 343245));
  72. $this->assertEquals('Peak: 335kB | ', $res);
  73. $res = $profiler->getProfileBetween(array(), array('memoryPeakUsage' => 73456345634));
  74. $this->assertEquals('Peak: 68.4GB | ', $res);
  75. }
  76. public function testGetProfileBetweenCombinesDetails()
  77. {
  78. $profiler = new Profiler();
  79. $profiler->setDetails(array(
  80. 'time' => array('name' => 'Time', 'precision' => 3, 'pad' => 3),
  81. 'mem' => array('name' => 'Memory', 'precision' => 3, 'pad' => 3),
  82. 'memDelta' => array('name' => 'Delta', 'precision' => 3, 'pad' => 3),
  83. 'memPeak' => array('name' => 'Peak', 'precision' => 3, 'pad' => 3),
  84. ));
  85. $res = $profiler->getProfileBetween(
  86. array('microtime' => 1.234, 'memoryUsage' => 343245, 'memoryPeakUsage' => 314357),
  87. array('microtime' => 2.345, 'memoryUsage' => 888064, 'memoryPeakUsage' => 343245)
  88. );
  89. $this->assertEquals('SLOW Time: 1.11s | Memory: 867kB | Delta: +532kB | Peak: 335kB | ', $res);
  90. $res = $profiler->getProfileBetween(
  91. array('microtime' => 1.000, 'memoryUsage' => 343245, 'memoryPeakUsage' => 314357),
  92. array('microtime' => 1.0345, 'memoryUsage' => 245643, 'memoryPeakUsage' => 343245)
  93. );
  94. $this->assertEquals(' Time: 34.5ms | Memory: 240kB | Delta: -95.3kB | Peak: 335kB | ', $res);
  95. }
  96. public function providerForTestFormatMemory()
  97. {
  98. return array(
  99. array(1234567890, number_format(1.15, 2) . 'GB'),
  100. array(123456789.0, number_format(118) . 'MB'),
  101. array(12345678.90, number_format(11.8, 1) . 'MB'),
  102. array(1234567.890, number_format(1.18, 2) . 'MB'),
  103. array(123456.7890, number_format(121) . 'kB'),
  104. array(12345.67890, number_format(12.1, 1) . 'kB'),
  105. array(1234.567890, number_format(1.21, 2) . 'kB'),
  106. array(123.4567890, number_format(123) . 'B'),
  107. array(12.34567890, number_format(12.3, 1) . 'B'),
  108. array(1.234567890, number_format(1.23, 2) . 'B'),
  109. );
  110. }
  111. /**
  112. * @dataProvider providerForTestFormatMemory
  113. */
  114. public function testFormatMemory($input, $output)
  115. {
  116. $this->assertEquals(Profiler::formatMemory($input), $output);
  117. }
  118. public function providerForTestFormatMemoryPrecision()
  119. {
  120. return array(
  121. array(1, number_format(10) . 'kB'),
  122. array(2, number_format(12) . 'kB'),
  123. array(3, number_format(12.1, 1) . 'kB'),
  124. array(4, number_format(12.06, 2) . 'kB'),
  125. array(5, number_format(12.056, 3) . 'kB'),
  126. array(6, number_format(12.0563, 4) . 'kB'),
  127. );
  128. }
  129. /**
  130. * @dataProvider providerForTestFormatMemoryPrecision
  131. */
  132. public function testFormatMemoryPrecision($input, $output)
  133. {
  134. $this->assertEquals(Profiler::formatMemory(12345.6789, $input), $output);
  135. }
  136. public function providerForTestFormatDuration()
  137. {
  138. return array(
  139. array(1234567890, number_format(1230000000) . 's '),
  140. array(123456789.0, number_format(123000000) . 's '),
  141. array(12345678.90, number_format(12300000) . 's '),
  142. array(1234567.890, number_format(1230000) . 's '),
  143. array(123456.7890, number_format(123000) . 's '),
  144. array(12345.67890, number_format(12300) . 's '),
  145. array(1234.567890, number_format(1230) . 's '),
  146. array(123.4567890, number_format(123) . 's '),
  147. array(12.34567890, number_format(12.3, 1) . 's '),
  148. array(1.234567890, number_format(1.23, 2) . 's '),
  149. array(0.123456789, number_format(123) . 'ms'),
  150. array(0.012345678, number_format(12.3, 1) . 'ms'),
  151. array(0.001234567, number_format(1.23, 2) . 'ms'),
  152. );
  153. }
  154. /**
  155. * @dataProvider providerForTestFormatDuration
  156. */
  157. public function testFormatDuration($input, $output)
  158. {
  159. $this->assertEquals(Profiler::formatDuration($input), $output);
  160. }
  161. public function providerForTestFormatDurationPrecision()
  162. {
  163. return array(
  164. array(1, number_format(100) . 's '),
  165. array(2, number_format(120) . 's '),
  166. array(3, number_format(123) . 's '),
  167. array(4, number_format(123.5, 1) . 's '),
  168. array(5, number_format(123.46, 2) . 's '),
  169. array(6, number_format(123.457, 3) . 's '),
  170. );
  171. }
  172. /**
  173. * @dataProvider providerForTestFormatDurationPrecision
  174. */
  175. public function testFormatDurationPrecision($input, $output)
  176. {
  177. $this->assertEquals(Profiler::formatDuration(123.456789, $input), $output);
  178. }
  179. public function providerForTestToPrecision()
  180. {
  181. return array(
  182. array(1234567890, number_format(1230000000)),
  183. array(123456789.0, number_format(123000000)),
  184. array(12345678.90, number_format(12300000)),
  185. array(1234567.890, number_format(1230000)),
  186. array(123456.7890, number_format(123000)),
  187. array(12345.67890, number_format(12300)),
  188. array(1234.567890, number_format(1230)),
  189. array(123.4567890, number_format(123)),
  190. array(12.34567890, number_format(12.3, 1)),
  191. array(1.234567890, number_format(1.23, 2)),
  192. array(0, 0),
  193. array(0.123456789, number_format(0.123, 3)),
  194. array(0.012345678, number_format(0.0123, 4)),
  195. array(0.001234567, number_format(0.00123, 5)),
  196. array(0.000123456, number_format(0.000123, 6)),
  197. array(0.000012345, number_format(0.0000123, 7)),
  198. array(0.000001234, number_format(0.00000123, 8)),
  199. array(-1234567.890, number_format(-1230000)),
  200. );
  201. }
  202. /**
  203. * @dataProvider providerForTestToPrecision
  204. */
  205. public function testToPrecision($input, $output)
  206. {
  207. $this->assertEquals(Profiler::toPrecision($input), $output);
  208. }
  209. public function providerForTestToPrecisionPrecision()
  210. {
  211. return array(
  212. array(0, 0),
  213. array(1, number_format(100)),
  214. array(2, number_format(120)),
  215. array(3, number_format(123)),
  216. array(4, number_format(123.5, 1)),
  217. array(5, number_format(123.46, 2)),
  218. array(6, number_format(123.457, 3)),
  219. );
  220. }
  221. /**
  222. * @dataProvider providerForTestToPrecisionPrecision
  223. */
  224. public function testToPrecisionPrecision($input, $output)
  225. {
  226. $this->assertEquals(Profiler::toPrecision(123.456789, $input), $output);
  227. }
  228. }