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  eval( $setupCode );
34  }
35 
36  if ( $this->hasOption( 'code' ) ) {
37  $code = $this->getOption( 'code' );
38  } elseif ( $this->hasArg( 0 ) ) {
39  $code = file_get_contents( $this->getArg( 0 ) );
40  if ( $code === false ) {
41  $this->fatalError( "Unable to read input file" );
42  }
43  } else {
44  fwrite( STDERR, "Reading from stdin...\n" );
45  $code = stream_get_contents( STDIN );
46  }
47  $code .= ';';
48  $inner = $this->getOption( 'inner', 1 );
49  if ( $inner > 1 ) {
50  $code = "for ( \$__i = 0; \$__i < $inner; \$__i++ ) { $code }";
51  }
52  $code = "function wfBenchmarkEvalBody () { $code }";
53  eval( $code );
54  $this->bench( [ 'eval' => [ 'function' => 'wfBenchmarkEvalBody' ] ] );
55  }
56 }
57 
58 $maintClass = BenchmarkEval::class;
59 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.