/benchmarks/ezmark.php

https://github.com/GunioRobot/ezpublish · PHP · 301 lines · 267 code · 21 blank · 13 comment · 86 complexity · bbec350530f2ce9b8d72e7218efeb5d1 MD5 · raw file

  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  5. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  6. * @version //autogentag//
  7. * @package kernel
  8. */
  9. $cli = eZCLI::instance();
  10. $script = eZScript::instance( array( 'debug-message' => '',
  11. 'use-session' => true,
  12. 'use-modules' => true,
  13. 'use-extensions' => true ) );
  14. $script->startup();
  15. $endl = $cli->endlineString();
  16. $webOutput = $cli->isWebOutput();
  17. function help()
  18. {
  19. $argv = $_SERVER['argv'];
  20. $cli = eZCLI::instance();
  21. $cli->output( "Usage: " . $argv[0] . " [OPTION]... MARK [MARK]\n" .
  22. "Runs selected benchmarks.\n" .
  23. "e.g. " . $argv[0] . " eztemplate\n" .
  24. "\n" .
  25. "General options:\n" .
  26. " -h,--help display this help and exit \n" .
  27. " -q,--quiet do not give any output except when errors occur\n" .
  28. " -s,--siteaccess selected siteaccess for operations, if not specified default siteaccess is used\n" .
  29. " -d,--debug display debug output at end of execution\n" .
  30. " -c,--colors display output using ANSI colors (default)\n" .
  31. " --sql display sql queries\n" .
  32. " --logfiles create log files\n" .
  33. " --no-logfiles do not create log files (default)\n" .
  34. " --no-colors do not use ANSI coloring\n" );
  35. }
  36. function changeSiteAccessSetting( &$siteaccess, $optionData )
  37. {
  38. $cli = eZCLI::instance();
  39. if ( file_exists( 'settings/siteaccess/' . $optionData ) )
  40. {
  41. $siteaccess = $optionData;
  42. $cli->output( "Using siteaccess $siteaccess for cronjob" );
  43. }
  44. else
  45. {
  46. $cli->notice( "Siteaccess $optionData does not exist, using default siteaccess" );
  47. }
  48. }
  49. $siteaccess = false;
  50. $debugOutput = false;
  51. $allowedDebugLevels = false;
  52. $useDebugAccumulators = false;
  53. $useDebugTimingpoints = false;
  54. $useIncludeFiles = false;
  55. $useColors = true;
  56. $isQuiet = false;
  57. $useLogFiles = false;
  58. $showSQL = false;
  59. $markList = array();
  60. $optionsWithData = array( 's' );
  61. $longOptionsWithData = array( 'siteaccess' );
  62. $readOptions = true;
  63. for ( $i = 1; $i < count( $argv ); ++$i )
  64. {
  65. $arg = $argv[$i];
  66. if ( $readOptions and
  67. strlen( $arg ) > 0 and
  68. $arg[0] == '-' )
  69. {
  70. if ( strlen( $arg ) > 1 and
  71. $arg[1] == '-' )
  72. {
  73. $flag = substr( $arg, 2 );
  74. if ( in_array( $flag, $longOptionsWithData ) )
  75. {
  76. $optionData = $argv[$i+1];
  77. ++$i;
  78. }
  79. if ( $flag == 'help' )
  80. {
  81. help();
  82. exit( 1 );
  83. }
  84. else if ( $flag == 'siteaccess' )
  85. {
  86. changeSiteAccessSetting( $siteaccess, $optionData );
  87. }
  88. else if ( $flag == 'debug' )
  89. {
  90. $debugOutput = true;
  91. }
  92. else if ( $flag == 'quiet' )
  93. {
  94. $isQuiet = true;
  95. }
  96. else if ( $flag == 'colors' )
  97. {
  98. $useColors = true;
  99. }
  100. else if ( $flag == 'no-colors' )
  101. {
  102. $useColors = false;
  103. }
  104. else if ( $flag == 'no-logfiles' )
  105. {
  106. $useLogFiles = false;
  107. }
  108. else if ( $flag == 'logfiles' )
  109. {
  110. $useLogFiles = true;
  111. }
  112. else if ( $flag == 'sql' )
  113. {
  114. $showSQL = true;
  115. }
  116. }
  117. else
  118. {
  119. $flag = substr( $arg, 1, 1 );
  120. $optionData = false;
  121. if ( in_array( $flag, $optionsWithData ) )
  122. {
  123. if ( strlen( $arg ) > 2 )
  124. {
  125. $optionData = substr( $arg, 2 );
  126. }
  127. else
  128. {
  129. $optionData = $argv[$i+1];
  130. ++$i;
  131. }
  132. }
  133. if ( $flag == 'h' )
  134. {
  135. help();
  136. exit( 1 );
  137. }
  138. else if ( $flag == 'q' )
  139. {
  140. $isQuiet = true;
  141. }
  142. else if ( $flag == 'c' )
  143. {
  144. $useColors = true;
  145. }
  146. else if ( $flag == 'd' )
  147. {
  148. $debugOutput = true;
  149. if ( strlen( $arg ) > 2 )
  150. {
  151. $levels = explode( ',', substr( $arg, 2 ) );
  152. $allowedDebugLevels = array();
  153. foreach ( $levels as $level )
  154. {
  155. if ( $level == 'all' )
  156. {
  157. $useDebugAccumulators = true;
  158. $allowedDebugLevels = false;
  159. $useDebugTimingpoints = true;
  160. break;
  161. }
  162. if ( $level == 'accumulator' )
  163. {
  164. $useDebugAccumulators = true;
  165. continue;
  166. }
  167. if ( $level == 'timing' )
  168. {
  169. $useDebugTimingpoints = true;
  170. continue;
  171. }
  172. if ( $level == 'include' )
  173. {
  174. $useIncludeFiles = true;
  175. }
  176. if ( $level == 'error' )
  177. $level = eZDebug::LEVEL_ERROR;
  178. else if ( $level == 'warning' )
  179. $level = eZDebug::LEVEL_WARNING;
  180. else if ( $level == 'debug' )
  181. $level = eZDebug::LEVEL_DEBUG;
  182. else if ( $level == 'notice' )
  183. $level = eZDebug::LEVEL_NOTICE;
  184. else if ( $level == 'timing' )
  185. $level = eZDebug::EZ_LEVEL_TIMING;
  186. $allowedDebugLevels[] = $level;
  187. }
  188. }
  189. }
  190. else if ( $flag == 's' )
  191. {
  192. changeSiteAccessSetting( $siteaccess, $optionData );
  193. }
  194. }
  195. }
  196. else
  197. {
  198. $markList[] = $arg;
  199. }
  200. }
  201. if ( count( $markList ) == 0 )
  202. {
  203. help();
  204. $script->shutdown( 1 );
  205. }
  206. $script->setUseDebugOutput( $debugOutput );
  207. $script->setAllowedDebugLevels( $allowedDebugLevels );
  208. $script->setUseDebugAccumulators( $useDebugAccumulators );
  209. $script->setUseDebugTimingPoints( $useDebugTimingpoints );
  210. $script->setUseIncludeFiles( $useIncludeFiles );
  211. $script->setIsQuiet( $isQuiet );
  212. if ( $webOutput )
  213. $useColors = true;
  214. $cli->setUseStyles( $useColors );
  215. $script->setDebugMessage( "\n\n" . str_repeat( '#', 36 ) . $cli->style( 'emphasize' ) . " DEBUG " . $cli->style( 'emphasize-end' ) . str_repeat( '#', 36 ) . "\n" );
  216. $script->setUseSiteAccess( $siteaccess );
  217. $script->initialize();
  218. $success = true;
  219. foreach ( $markList as $markName )
  220. {
  221. $markPath = 'benchmarks/' . $markName;
  222. $markDefinitionPath = $markPath . '/benchmark.php';
  223. if ( file_exists( $markDefinitionPath ) )
  224. {
  225. unset( $MarkDefinition );
  226. include( $markDefinitionPath );
  227. if ( isset( $MarkDefinition ) )
  228. {
  229. $mark = new eZBenchmark( $MarkDefinition['name'] );
  230. foreach ( $MarkDefinition['marks'] as $markDefinition )
  231. {
  232. $markTestFile = $markDefinition['file'];
  233. $markTestPath = $markPath . '/' . $markTestFile;
  234. if ( file_exists( $markTestPath ) )
  235. {
  236. include_once( $markTestPath );
  237. $markTestClass = $markDefinition['class'];
  238. $markTestName = $markDefinition['name'];
  239. if ( class_exists( $markTestClass ) )
  240. {
  241. $markTest = new $markTestClass( $markTestName );
  242. $mark->addMark( $markTest );
  243. }
  244. else
  245. {
  246. $cli->warning( "Could not find test benchmark class '" . $cli->stylize( $markTestClass ) . "' for benchmark " .
  247. $cli->stylize( 'emphasize', $markTestName ) );
  248. }
  249. }
  250. else
  251. {
  252. $cli->warning( "Could not find a test benchmark file '" . $cli->stylize( $testUnitFile ) . "' for benchmark " .
  253. $cli->stylize( 'emphasize', $markName ) );
  254. }
  255. }
  256. $cli->output( "Benchmark results from mark " . $cli->stylize( 'emphasize', $markName ) );
  257. $runner = new eZBenchmarkCLIRunner();
  258. $runner->run( $mark, true );
  259. }
  260. else
  261. {
  262. $cli->warning( "Could not find a benchmark definition for benchmark " .
  263. $cli->stylize( 'emphasize', $markName ) . "\n" .
  264. $cli->stylize( 'emphasize', "\$MarkDefinition" ) . " is missing" );
  265. }
  266. }
  267. else
  268. {
  269. $cli->warning( "Could not find a benchmark definition for benchmark " . $cli->stylize( 'emphasize', $markName ) . "\nTried $markDefinitionPath" );
  270. }
  271. }
  272. $exitStatus = 0;
  273. // if ( !$success )
  274. // {
  275. // $cli->output();
  276. // $cli->output( "Some tests failed" );
  277. // $exitStatus = 1;
  278. // }
  279. $script->shutdown( $exitStatus );
  280. ?>