| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
require_once __DIR__ . '/../includes/Benchmarker.php'; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class BenchmarkEval extends Benchmarker { |
| 19 |
public function __construct() { |
| 20 |
parent::__construct(); |
| 21 |
$this->addOption( 'inner', |
| 22 |
'Inner loop iterations', false, true ); |
| 23 |
$this->addOption( 'code', |
| 24 |
'The code to run', |
| 25 |
false, true, 'e' ); |
| 26 |
$this->addOption( 'setup', |
| 27 |
'Code to run once before the first iteration', |
| 28 |
false, true ); |
| 29 |
$this->addArg( 'input-file', 'Input file for measured code body', false ); |
| 30 |
} |
| 31 |
|
| 32 |
public function execute() { |
| 33 |
if ( $this->hasOption( 'setup' ) ) { |
| 34 |
$setupCode = $this->getOption( 'setup' ) . ';'; |
| 35 |
|
| 36 |
eval( $setupCode ); |
| 37 |
} |
| 38 |
|
| 39 |
if ( $this->hasOption( 'code' ) ) { |
| 40 |
$code = $this->getOption( 'code' ); |
| 41 |
} elseif ( $this->hasArg( 0 ) ) { |
| 42 |
$code = file_get_contents( $this->getArg( 0 ) ); |
| 43 |
if ( $code === false ) { |
| 44 |
$this->fatalError( "Unable to read input file" ); |
| 45 |
} |
| 46 |
} else { |
| 47 |
fwrite( STDERR, "Reading from stdin...\n" ); |
| 48 |
$code = stream_get_contents( STDIN ); |
| 49 |
} |
| 50 |
$code .= ';'; |
| 51 |
$inner = $this->getOption( 'inner', 1 ); |
| 52 |
if ( $inner > 1 ) { |
| 53 |
$code = "for ( \$__i = 0; \$__i < $inner; \$__i++ ) { $code }"; |
| 54 |
} |
| 55 |
$code = "function wfBenchmarkEvalBody () { $code }"; |
| 56 |
|
| 57 |
eval( $code ); |
| 58 |
$this->bench( [ 'eval' => [ 'function' => 'wfBenchmarkEvalBody' ] ] ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
$maintClass = BenchmarkEval::class; |
| 64 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 65 |
|