PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/benchmarks/classes/ezbenchmarkrunner.php

http://github.com/ezsystems/ezpublish
PHP | 293 lines | 213 code | 33 blank | 47 comment | 26 complexity | 5c1c3e1652f93451e412a68b87fdb5f9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZBenchmarkrunner class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZBenchmarkrunner ezbenchmarkrunner.php
  12. \brief The class eZBenchmarkrunner does
  13. */
  14. class eZBenchmarkrunner
  15. {
  16. /**
  17. * Constructor
  18. */
  19. public function __construct()
  20. {
  21. $this->Results = array();
  22. $this->CurrentResult = false;
  23. $this->IsSuccessful = false;
  24. $this->DefaultRepeatCount = 50;
  25. }
  26. function run( &$benchmark, $display = false )
  27. {
  28. $this->Results = array();
  29. $this->CurrentResult = false;
  30. if ( is_subclass_of( $benchmark, 'ezbenchmarkunit' ) )
  31. {
  32. $markList = $benchmark->markList();
  33. foreach ( $markList as $mark )
  34. {
  35. $type = $this->markEntryType( $benchmark, $mark );
  36. if ( $type )
  37. {
  38. $mark['type'] = $type;
  39. $this->prepareMarkEntry( $benchmark, $mark );
  40. $this->runMarkEntry( $benchmark, $mark );
  41. $this->finalizeMarkEntry( $benchmark, $mark, $display );
  42. }
  43. else
  44. $this->addToCurrentResult( $mark,
  45. "Unknown mark type for mark " . $benchmark->name() . '::' . $mark['name'] );
  46. }
  47. }
  48. else
  49. {
  50. eZDebug::writeWarning( "Tried to run test on an object which is not subclassed from eZBenchmarkCase", __METHOD__ );
  51. }
  52. }
  53. function markEntryType( $benchmark, $entry )
  54. {
  55. if ( isset( $entry['method'] ) and
  56. isset( $entry['object'] ) )
  57. {
  58. return 'method';
  59. }
  60. else if ( isset( $entry['function'] ) )
  61. {
  62. return 'function';
  63. }
  64. return false;
  65. }
  66. function prepareMarkEntry( &$benchmark, $entry )
  67. {
  68. $this->setCurrentMarkName( $benchmark->name() . '::' . $entry['name'] );
  69. $this->resetCurrentResult();
  70. }
  71. function finalizeMarkEntry( &$benchmark, $entry, $display )
  72. {
  73. $currentResult = $this->addCurrentResult();
  74. $this->setCurrentMarkName( false );
  75. if ( $display )
  76. $this->display( $currentResult );
  77. }
  78. function runMarkEntry( &$benchmark, $entry )
  79. {
  80. switch ( $entry['type'] )
  81. {
  82. case 'method':
  83. {
  84. $object =& $entry['object'];
  85. $method =& $entry['method'];
  86. if ( method_exists( $object, $method ) )
  87. {
  88. if ( method_exists( $object, 'prime' ) )
  89. {
  90. $entry['prime_start'] = array( 'memory' => memory_get_usage(),
  91. 'time' => microtime() );
  92. $object->prime( $this );
  93. $entry['prime_end'] = array( 'memory' => memory_get_usage(),
  94. 'time' => microtime() );
  95. }
  96. $repeatCount = $this->DefaultRepeatCount;
  97. if ( isset( $entry['repeat_count'] ) )
  98. $repeatCount = $entry['repeat_count'];
  99. $entry['start'] = array( 'memory' => memory_get_usage(),
  100. 'time' => microtime() );
  101. for ( $i = 0; $i < $repeatCount; ++$i )
  102. {
  103. $object->$method( $this, $entry['parameter'] );
  104. }
  105. $entry['end'] = array( 'memory' => memory_get_usage(),
  106. 'time' => microtime() );
  107. if ( method_exists( $object, 'cleanup' ) )
  108. $object->cleanup( $this );
  109. $this->processRecording( $benchmark, $entry, $repeatCount );
  110. }
  111. else
  112. {
  113. $this->addToCurrentResult( $entry,
  114. "Method $method does not exist for mark object(" . get_class( $object ) . ")" );
  115. }
  116. } break;
  117. case 'function':
  118. {
  119. $function = $entry['function'];
  120. if ( function_exists( $function ) )
  121. {
  122. $repeatCount = $this->DefaultRepeatCount;
  123. if ( isset( $entry['repeat_count'] ) )
  124. $repeatCount = $entry['repeat_count'];
  125. $entry['start'] = array( 'memory' => memory_get_usage(),
  126. 'time' => microtime() );
  127. for ( $i = 0; $i < $repeatCount; ++$i )
  128. {
  129. $function( $this, $entry['parameter'] );
  130. }
  131. $entry['end'] = array( 'memory' => memory_get_usage(),
  132. 'time' => microtime() );
  133. $this->processRecording( $benchmark, $entry, $repeatCount );
  134. }
  135. else
  136. {
  137. $this->addToCurrentResult( $entry,
  138. "Function $function does not exist" );
  139. }
  140. } break;
  141. }
  142. }
  143. function processRecording( &$benchmark, &$entry, $repeatCount )
  144. {
  145. $memoryDiff = $entry['end']['memory'] - $entry['start']['memory'];
  146. $startTime = explode( " ", $entry['start']['time'] );
  147. preg_match( "@0\.([0-9]+)@", "" . $startTime[0], $t1 );
  148. $startTime = $startTime[1] . "." . $t1[1];
  149. $endTime = explode( " ", $entry['end']['time'] );
  150. preg_match( "@0\.([0-9]+)@", "" . $endTime[0], $t1 );
  151. $endTime = $endTime[1] . "." . $t1[1];
  152. $timeDiff = $endTime - $startTime;
  153. $entry['result'] = array( 'memory' => $memoryDiff,
  154. 'time' => $timeDiff );
  155. $entry['normalized'] = array( 'time' => $timeDiff / $repeatCount );
  156. $memoryDiff = $entry['prime_end']['memory'] - $entry['prime_start']['memory'];
  157. $startTime = explode( " ", $entry['prime_start']['time'] );
  158. preg_match( "@0\.([0-9]+)@", "" . $startTime[0], $t1 );
  159. $startTime = $startTime[1] . "." . $t1[1];
  160. $endTime = explode( " ", $entry['prime_end']['time'] );
  161. preg_match( "@0\.([0-9]+)@", "" . $endTime[0], $t1 );
  162. $endTime = $endTime[1] . "." . $t1[1];
  163. $timeDiff = $endTime - $startTime;
  164. $entry['prime'] = array( 'memory' => $memoryDiff,
  165. 'time' => $timeDiff );
  166. $this->addToCurrentResult( $entry );
  167. }
  168. /*!
  169. \virtual
  170. \protected
  171. Called whenever a test is run, can be overriden to print out the test result immediately.
  172. */
  173. function display( $result, $repeatCount )
  174. {
  175. }
  176. /*!
  177. \return an array with all the results from the last run.
  178. */
  179. function resultList()
  180. {
  181. return $this->Results;
  182. }
  183. /*!
  184. \protected
  185. Adds a result for mark \a $markName with optional message \a $message.
  186. */
  187. function addToCurrentResult( $entry, $message = false )
  188. {
  189. $markName = $entry['name'];
  190. if ( !is_array( $this->CurrentResult ) )
  191. {
  192. $this->CurrentResult = array( 'name' => $markName,
  193. 'start' => false,
  194. 'end' => false,
  195. 'result' => false,
  196. 'normalized' => false,
  197. 'prime' => false,
  198. 'messages' => array() );
  199. }
  200. $repeatCount = $this->DefaultRepeatCount;
  201. if ( isset( $entry['repeat_count'] ) )
  202. $repeatCount = $entry['repeat_count'];
  203. $this->CurrentResult['repeat_count'] = $repeatCount;
  204. if ( isset( $entry['start'] ) )
  205. $this->CurrentResult['start'] = $entry['start'];
  206. if ( isset( $entry['end'] ) )
  207. $this->CurrentResult['end'] = $entry['end'];
  208. if ( isset( $entry['result'] ) )
  209. $this->CurrentResult['result'] = $entry['result'];
  210. if ( isset( $entry['normalized'] ) )
  211. $this->CurrentResult['normalized'] = $entry['normalized'];
  212. if ( isset( $entry['prime'] ) )
  213. $this->CurrentResult['prime'] = $entry['prime'];
  214. if ( $message )
  215. $this->CurrentResult['messages'][] = array( 'text' => $message );
  216. }
  217. /*!
  218. \protected
  219. Adds the current result to the result list and resets the current result data.
  220. */
  221. function addCurrentResult()
  222. {
  223. if ( is_array( $this->CurrentResult ) )
  224. $this->Results[] = $this->CurrentResult;
  225. return $this->CurrentResult;
  226. }
  227. /*!
  228. \protected
  229. Resets the current result data.
  230. */
  231. function resetCurrentResult()
  232. {
  233. $this->CurrentResult = array( 'name' => $this->currentMarkName(),
  234. 'messages' => array() );
  235. }
  236. /*!
  237. \return the name of the currently running mark or \c false if no mark.
  238. */
  239. function currentMarkName()
  240. {
  241. return $this->CurrentMarkName;
  242. }
  243. /*!
  244. \protected
  245. Sets the name of the currently running mark to \a $name.
  246. */
  247. function setCurrentMarkName( $name )
  248. {
  249. $this->CurrentMarkName = $name;
  250. }
  251. /// \privatesection
  252. /// An array with test results.
  253. var $Results;
  254. /// The current result
  255. var $CurrentResult;
  256. /// The name of the currently running mark or \c false
  257. var $CurrentMarkName;
  258. }
  259. ?>