Debian Codegraph @

maintenance/benchmarks/benchmarkEval.php

raw symbols SCIP
1 <?php
2
3 // @codeCoverageIgnoreStart
4 require_once __DIR__ . '/../includes/Benchmarker.php';
5 // @codeCoverageIgnoreEnd
6
7 /**
8 * Benchmark any provided code for ad-hoc benchmarks.
9 *
10 * Usage:
11 *
12 * $ php benchmarkEval.php --code "Html::openElement( 'a', [ 'class' => 'foo' ] );"
13 * $ echo "Html::openElement( 'a', [ 'class' => 'foo' ] );" | php benchmarkEval.php
14 * $ php benchmarkEval.php input.txt
15 *
16 * @ingroup Benchmark
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 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.eval
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 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.eval
57 eval( $code );
58 $this->bench( [ 'eval' => [ 'function' => 'wfBenchmarkEvalBody' ] ] );
59 }
60 }
61
62 // @codeCoverageIgnoreStart
63 $maintClass = BenchmarkEval::class;
64 require_once RUN_MAINTENANCE_IF_MAIN;
65 // @codeCoverageIgnoreEnd