MediaWiki  master
benchmarkEval.php
Go to the documentation of this file.
1 <?php
2 
3 require_once __DIR__ . '/../includes/Benchmarker.php';
4 
16 class BenchmarkEval extends Benchmarker {
17  public function __construct() {
18  parent::__construct();
19  $this->addOption( 'inner',
20  'Inner loop iterations', false, true );
21  $this->addOption( 'code',
22  'The code to run',
23  false, true, 'e' );
24  $this->addOption( 'setup',
25  'Code to run once before the first iteration',
26  false, true );
27  $this->addArg( 'input-file', 'Input file for measured code body', false );
28  }
29 
30  public function execute() {
31  if ( $this->hasOption( 'setup' ) ) {
32  $setupCode = $this->getOption( 'setup' ) . ';';
33  // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.eval
34  eval( $setupCode );
35  }
36 
37  if ( $this->hasOption( 'code' ) ) {
38  $code = $this->getOption( 'code' );
39  } elseif ( $this->hasArg( 0 ) ) {
40  $code = file_get_contents( $this->getArg( 0 ) );
41  if ( $code === false ) {
42  $this->fatalError( "Unable to read input file" );
43  }
44  } else {
45  fwrite( STDERR, "Reading from stdin...\n" );
46  $code = stream_get_contents( STDIN );
47  }
48  $code .= ';';
49  $inner = $this->getOption( 'inner', 1 );
50  if ( $inner > 1 ) {
51  $code = "for ( \$__i = 0; \$__i < $inner; \$__i++ ) { $code }";
52  }
53  $code = "function wfBenchmarkEvalBody () { $code }";
54  // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.eval
55  eval( $code );
56  $this->bench( [ 'eval' => [ 'function' => 'wfBenchmarkEvalBody' ] ] );
57  }
58 }
59 
60 $maintClass = BenchmarkEval::class;
61 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
Benchmark any provided code for ad-hoc benchmarks.
__construct()
Default constructor.
execute()
Do the actual work.
Base class for benchmark scripts.
Definition: Benchmarker.php:40
bench(array $benchs)
Definition: Benchmarker.php:49
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.