PageRenderTime 77ms CodeModel.GetById 39ms RepoModel.GetById 9ms app.codeStats 0ms

/benchmarks/classes/ezbenchmarkrunner.php

https://github.com/StephanBoganskyXrow/ezpublish
PHP | 316 lines | 213 code | 34 blank | 69 comment | 26 complexity | 316d69d4be9143eeca67026dbc7e7397 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. //
  3. // Definition of eZBenchmarkrunner class
  4. //
  5. // Created on: <18-Feb-2004 11:56:59 >
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2011 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \file ezbenchmarkrunner.php
  31. */
  32. /*!
  33. \class eZBenchmarkrunner ezbenchmarkrunner.php
  34. \brief The class eZBenchmarkrunner does
  35. */
  36. class eZBenchmarkrunner
  37. {
  38. /*!
  39. Constructor
  40. */
  41. function eZBenchmarkrunner()
  42. {
  43. $this->Results = array();
  44. $this->CurrentResult = false;
  45. $this->IsSuccessful = false;
  46. $this->DefaultRepeatCount = 50;
  47. }
  48. function run( &$benchmark, $display = false )
  49. {
  50. $this->Results = array();
  51. $this->CurrentResult = false;
  52. if ( is_subclass_of( $benchmark, 'ezbenchmarkunit' ) )
  53. {
  54. $markList = $benchmark->markList();
  55. foreach ( $markList as $mark )
  56. {
  57. $type = $this->markEntryType( $benchmark, $mark );
  58. if ( $type )
  59. {
  60. $mark['type'] = $type;
  61. $this->prepareMarkEntry( $benchmark, $mark );
  62. $this->runMarkEntry( $benchmark, $mark );
  63. $this->finalizeMarkEntry( $benchmark, $mark, $display );
  64. }
  65. else
  66. $this->addToCurrentResult( $mark,
  67. "Unknown mark type for mark " . $benchmark->name() . '::' . $mark['name'] );
  68. }
  69. }
  70. else
  71. {
  72. eZDebug::writeWarning( "Tried to run test on an object which is not subclassed from eZBenchmarkCase", __METHOD__ );
  73. }
  74. }
  75. function markEntryType( $benchmark, $entry )
  76. {
  77. if ( isset( $entry['method'] ) and
  78. isset( $entry['object'] ) )
  79. {
  80. return 'method';
  81. }
  82. else if ( isset( $entry['function'] ) )
  83. {
  84. return 'function';
  85. }
  86. return false;
  87. }
  88. function prepareMarkEntry( &$benchmark, $entry )
  89. {
  90. $this->setCurrentMarkName( $benchmark->name() . '::' . $entry['name'] );
  91. $this->resetCurrentResult();
  92. }
  93. function finalizeMarkEntry( &$benchmark, $entry, $display )
  94. {
  95. $currentResult = $this->addCurrentResult();
  96. $this->setCurrentMarkName( false );
  97. if ( $display )
  98. $this->display( $currentResult );
  99. }
  100. function runMarkEntry( &$benchmark, $entry )
  101. {
  102. switch ( $entry['type'] )
  103. {
  104. case 'method':
  105. {
  106. $object =& $entry['object'];
  107. $method =& $entry['method'];
  108. if ( method_exists( $object, $method ) )
  109. {
  110. if ( method_exists( $object, 'prime' ) )
  111. {
  112. $entry['prime_start'] = array( 'memory' => memory_get_usage(),
  113. 'time' => microtime() );
  114. $object->prime( $this );
  115. $entry['prime_end'] = array( 'memory' => memory_get_usage(),
  116. 'time' => microtime() );
  117. }
  118. $repeatCount = $this->DefaultRepeatCount;
  119. if ( isset( $entry['repeat_count'] ) )
  120. $repeatCount = $entry['repeat_count'];
  121. $entry['start'] = array( 'memory' => memory_get_usage(),
  122. 'time' => microtime() );
  123. for ( $i = 0; $i < $repeatCount; ++$i )
  124. {
  125. $object->$method( $this, $entry['parameter'] );
  126. }
  127. $entry['end'] = array( 'memory' => memory_get_usage(),
  128. 'time' => microtime() );
  129. if ( method_exists( $object, 'cleanup' ) )
  130. $object->cleanup( $this );
  131. $this->processRecording( $benchmark, $entry, $repeatCount );
  132. }
  133. else
  134. {
  135. $this->addToCurrentResult( $entry,
  136. "Method $method does not exist for mark object(" . get_class( $object ) . ")" );
  137. }
  138. } break;
  139. case 'function':
  140. {
  141. $function = $entry['function'];
  142. if ( function_exists( $function ) )
  143. {
  144. $repeatCount = $this->DefaultRepeatCount;
  145. if ( isset( $entry['repeat_count'] ) )
  146. $repeatCount = $entry['repeat_count'];
  147. $entry['start'] = array( 'memory' => memory_get_usage(),
  148. 'time' => microtime() );
  149. for ( $i = 0; $i < $repeatCount; ++$i )
  150. {
  151. $function( $this, $entry['parameter'] );
  152. }
  153. $entry['end'] = array( 'memory' => memory_get_usage(),
  154. 'time' => microtime() );
  155. $this->processRecording( $benchmark, $entry, $repeatCount );
  156. }
  157. else
  158. {
  159. $this->addToCurrentResult( $entry,
  160. "Function $function does not exist" );
  161. }
  162. } break;
  163. }
  164. }
  165. function processRecording( &$benchmark, &$entry, $repeatCount )
  166. {
  167. $memoryDiff = $entry['end']['memory'] - $entry['start']['memory'];
  168. $startTime = explode( " ", $entry['start']['time'] );
  169. preg_match( "@0\.([0-9]+)@", "" . $startTime[0], $t1 );
  170. $startTime = $startTime[1] . "." . $t1[1];
  171. $endTime = explode( " ", $entry['end']['time'] );
  172. preg_match( "@0\.([0-9]+)@", "" . $endTime[0], $t1 );
  173. $endTime = $endTime[1] . "." . $t1[1];
  174. $timeDiff = $endTime - $startTime;
  175. $entry['result'] = array( 'memory' => $memoryDiff,
  176. 'time' => $timeDiff );
  177. $entry['normalized'] = array( 'time' => $timeDiff / $repeatCount );
  178. $memoryDiff = $entry['prime_end']['memory'] - $entry['prime_start']['memory'];
  179. $startTime = explode( " ", $entry['prime_start']['time'] );
  180. preg_match( "@0\.([0-9]+)@", "" . $startTime[0], $t1 );
  181. $startTime = $startTime[1] . "." . $t1[1];
  182. $endTime = explode( " ", $entry['prime_end']['time'] );
  183. preg_match( "@0\.([0-9]+)@", "" . $endTime[0], $t1 );
  184. $endTime = $endTime[1] . "." . $t1[1];
  185. $timeDiff = $endTime - $startTime;
  186. $entry['prime'] = array( 'memory' => $memoryDiff,
  187. 'time' => $timeDiff );
  188. $this->addToCurrentResult( $entry );
  189. }
  190. /*!
  191. \virtual
  192. \protected
  193. Called whenever a test is run, can be overriden to print out the test result immediately.
  194. */
  195. function display( $result, $repeatCount )
  196. {
  197. }
  198. /*!
  199. \return an array with all the results from the last run.
  200. */
  201. function resultList()
  202. {
  203. return $this->Results;
  204. }
  205. /*!
  206. \protected
  207. Adds a result for mark \a $markName with optional message \a $message.
  208. */
  209. function addToCurrentResult( $entry, $message = false )
  210. {
  211. $markName = $entry['name'];
  212. if ( !is_array( $this->CurrentResult ) )
  213. {
  214. $this->CurrentResult = array( 'name' => $testName,
  215. 'start' => false,
  216. 'end' => false,
  217. 'result' => false,
  218. 'normalized' => false,
  219. 'prime' => false,
  220. 'messages' => array() );
  221. }
  222. $repeatCount = $this->DefaultRepeatCount;
  223. if ( isset( $entry['repeat_count'] ) )
  224. $repeatCount = $entry['repeat_count'];
  225. $this->CurrentResult['repeat_count'] = $repeatCount;
  226. if ( isset( $entry['start'] ) )
  227. $this->CurrentResult['start'] = $entry['start'];
  228. if ( isset( $entry['end'] ) )
  229. $this->CurrentResult['end'] = $entry['end'];
  230. if ( isset( $entry['result'] ) )
  231. $this->CurrentResult['result'] = $entry['result'];
  232. if ( isset( $entry['normalized'] ) )
  233. $this->CurrentResult['normalized'] = $entry['normalized'];
  234. if ( isset( $entry['prime'] ) )
  235. $this->CurrentResult['prime'] = $entry['prime'];
  236. if ( $message )
  237. $this->CurrentResult['messages'][] = array( 'text' => $message );
  238. }
  239. /*!
  240. \protected
  241. Adds the current result to the result list and resets the current result data.
  242. */
  243. function addCurrentResult()
  244. {
  245. if ( is_array( $this->CurrentResult ) )
  246. $this->Results[] = $this->CurrentResult;
  247. return $this->CurrentResult;
  248. }
  249. /*!
  250. \protected
  251. Resets the current result data.
  252. */
  253. function resetCurrentResult()
  254. {
  255. $this->CurrentResult = array( 'name' => $this->currentMarkName(),
  256. 'messages' => array() );
  257. }
  258. /*!
  259. \return the name of the currently running mark or \c false if no mark.
  260. */
  261. function currentMarkName()
  262. {
  263. return $this->CurrentMarkName;
  264. }
  265. /*!
  266. \protected
  267. Sets the name of the currently running mark to \a $name.
  268. */
  269. function setCurrentMarkName( $name )
  270. {
  271. $this->CurrentMarkName = $name;
  272. }
  273. /// \privatesection
  274. /// An array with test results.
  275. var $Results;
  276. /// The current result
  277. var $CurrentResult;
  278. /// The name of the currently running mark or \c false
  279. var $CurrentMarkName;
  280. }
  281. ?>