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