PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/benchmarks/classes/ezbenchmarkrunner.php

https://github.com/aurelienRT1/ezpublish
PHP | 317 lines | 214 code | 34 blank | 69 comment | 26 complexity | 0a9db28b163e40dca47469f4f13a7496 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  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-2010 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",
  73. 'eZBenchmarkRunner::run' );
  74. }
  75. }
  76. function markEntryType( $benchmark, $entry )
  77. {
  78. if ( isset( $entry['method'] ) and
  79. isset( $entry['object'] ) )
  80. {
  81. return 'method';
  82. }
  83. else if ( isset( $entry['function'] ) )
  84. {
  85. return 'function';
  86. }
  87. return false;
  88. }
  89. function prepareMarkEntry( &$benchmark, $entry )
  90. {
  91. $this->setCurrentMarkName( $benchmark->name() . '::' . $entry['name'] );
  92. $this->resetCurrentResult();
  93. }
  94. function finalizeMarkEntry( &$benchmark, $entry, $display )
  95. {
  96. $currentResult = $this->addCurrentResult();
  97. $this->setCurrentMarkName( false );
  98. if ( $display )
  99. $this->display( $currentResult );
  100. }
  101. function runMarkEntry( &$benchmark, $entry )
  102. {
  103. switch ( $entry['type'] )
  104. {
  105. case 'method':
  106. {
  107. $object =& $entry['object'];
  108. $method =& $entry['method'];
  109. if ( method_exists( $object, $method ) )
  110. {
  111. if ( method_exists( $object, 'prime' ) )
  112. {
  113. $entry['prime_start'] = array( 'memory' => memory_get_usage(),
  114. 'time' => microtime() );
  115. $object->prime( $this );
  116. $entry['prime_end'] = array( 'memory' => memory_get_usage(),
  117. 'time' => microtime() );
  118. }
  119. $repeatCount = $this->DefaultRepeatCount;
  120. if ( isset( $entry['repeat_count'] ) )
  121. $repeatCount = $entry['repeat_count'];
  122. $entry['start'] = array( 'memory' => memory_get_usage(),
  123. 'time' => microtime() );
  124. for ( $i = 0; $i < $repeatCount; ++$i )
  125. {
  126. $object->$method( $this, $entry['parameter'] );
  127. }
  128. $entry['end'] = array( 'memory' => memory_get_usage(),
  129. 'time' => microtime() );
  130. if ( method_exists( $object, 'cleanup' ) )
  131. $object->cleanup( $this );
  132. $this->processRecording( $benchmark, $entry, $repeatCount );
  133. }
  134. else
  135. {
  136. $this->addToCurrentResult( $entry,
  137. "Method $method does not exist for mark object(" . get_class( $object ) . ")" );
  138. }
  139. } break;
  140. case 'function':
  141. {
  142. $function = $entry['function'];
  143. if ( function_exists( $function ) )
  144. {
  145. $repeatCount = $this->DefaultRepeatCount;
  146. if ( isset( $entry['repeat_count'] ) )
  147. $repeatCount = $entry['repeat_count'];
  148. $entry['start'] = array( 'memory' => memory_get_usage(),
  149. 'time' => microtime() );
  150. for ( $i = 0; $i < $repeatCount; ++$i )
  151. {
  152. $function( $this, $entry['parameter'] );
  153. }
  154. $entry['end'] = array( 'memory' => memory_get_usage(),
  155. 'time' => microtime() );
  156. $this->processRecording( $benchmark, $entry, $repeatCount );
  157. }
  158. else
  159. {
  160. $this->addToCurrentResult( $entry,
  161. "Function $function does not exist" );
  162. }
  163. } break;
  164. }
  165. }
  166. function processRecording( &$benchmark, &$entry, $repeatCount )
  167. {
  168. $memoryDiff = $entry['end']['memory'] - $entry['start']['memory'];
  169. $startTime = explode( " ", $entry['start']['time'] );
  170. preg_match( "@0\.([0-9]+)@", "" . $startTime[0], $t1 );
  171. $startTime = $startTime[1] . "." . $t1[1];
  172. $endTime = explode( " ", $entry['end']['time'] );
  173. preg_match( "@0\.([0-9]+)@", "" . $endTime[0], $t1 );
  174. $endTime = $endTime[1] . "." . $t1[1];
  175. $timeDiff = $endTime - $startTime;
  176. $entry['result'] = array( 'memory' => $memoryDiff,
  177. 'time' => $timeDiff );
  178. $entry['normalized'] = array( 'time' => $timeDiff / $repeatCount );
  179. $memoryDiff = $entry['prime_end']['memory'] - $entry['prime_start']['memory'];
  180. $startTime = explode( " ", $entry['prime_start']['time'] );
  181. preg_match( "@0\.([0-9]+)@", "" . $startTime[0], $t1 );
  182. $startTime = $startTime[1] . "." . $t1[1];
  183. $endTime = explode( " ", $entry['prime_end']['time'] );
  184. preg_match( "@0\.([0-9]+)@", "" . $endTime[0], $t1 );
  185. $endTime = $endTime[1] . "." . $t1[1];
  186. $timeDiff = $endTime - $startTime;
  187. $entry['prime'] = array( 'memory' => $memoryDiff,
  188. 'time' => $timeDiff );
  189. $this->addToCurrentResult( $entry );
  190. }
  191. /*!
  192. \virtual
  193. \protected
  194. Called whenever a test is run, can be overriden to print out the test result immediately.
  195. */
  196. function display( $result, $repeatCount )
  197. {
  198. }
  199. /*!
  200. \return an array with all the results from the last run.
  201. */
  202. function resultList()
  203. {
  204. return $this->Results;
  205. }
  206. /*!
  207. \protected
  208. Adds a result for mark \a $markName with optional message \a $message.
  209. */
  210. function addToCurrentResult( $entry, $message = false )
  211. {
  212. $markName = $entry['name'];
  213. if ( !is_array( $this->CurrentResult ) )
  214. {
  215. $this->CurrentResult = array( 'name' => $testName,
  216. 'start' => false,
  217. 'end' => false,
  218. 'result' => false,
  219. 'normalized' => false,
  220. 'prime' => false,
  221. 'messages' => array() );
  222. }
  223. $repeatCount = $this->DefaultRepeatCount;
  224. if ( isset( $entry['repeat_count'] ) )
  225. $repeatCount = $entry['repeat_count'];
  226. $this->CurrentResult['repeat_count'] = $repeatCount;
  227. if ( isset( $entry['start'] ) )
  228. $this->CurrentResult['start'] = $entry['start'];
  229. if ( isset( $entry['end'] ) )
  230. $this->CurrentResult['end'] = $entry['end'];
  231. if ( isset( $entry['result'] ) )
  232. $this->CurrentResult['result'] = $entry['result'];
  233. if ( isset( $entry['normalized'] ) )
  234. $this->CurrentResult['normalized'] = $entry['normalized'];
  235. if ( isset( $entry['prime'] ) )
  236. $this->CurrentResult['prime'] = $entry['prime'];
  237. if ( $message )
  238. $this->CurrentResult['messages'][] = array( 'text' => $message );
  239. }
  240. /*!
  241. \protected
  242. Adds the current result to the result list and resets the current result data.
  243. */
  244. function addCurrentResult()
  245. {
  246. if ( is_array( $this->CurrentResult ) )
  247. $this->Results[] = $this->CurrentResult;
  248. return $this->CurrentResult;
  249. }
  250. /*!
  251. \protected
  252. Resets the current result data.
  253. */
  254. function resetCurrentResult()
  255. {
  256. $this->CurrentResult = array( 'name' => $this->currentMarkName(),
  257. 'messages' => array() );
  258. }
  259. /*!
  260. \return the name of the currently running mark or \c false if no mark.
  261. */
  262. function currentMarkName()
  263. {
  264. return $this->CurrentMarkName;
  265. }
  266. /*!
  267. \protected
  268. Sets the name of the currently running mark to \a $name.
  269. */
  270. function setCurrentMarkName( $name )
  271. {
  272. $this->CurrentMarkName = $name;
  273. }
  274. /// \privatesection
  275. /// An array with test results.
  276. var $Results;
  277. /// The current result
  278. var $CurrentResult;
  279. /// The name of the currently running mark or \c false
  280. var $CurrentMarkName;
  281. }
  282. ?>