[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/benchmarks/ -> Benchmarker.php (source)

   1  <?php
   2  /**
   3   * @defgroup Benchmark Benchmark
   4   * @ingroup  Maintenance
   5   */
   6  
   7  /**
   8   * Base code for benchmark scripts.
   9   *
  10   * This program is free software; you can redistribute it and/or modify
  11   * it under the terms of the GNU General Public License as published by
  12   * the Free Software Foundation; either version 2 of the License, or
  13   * (at your option) any later version.
  14   *
  15   * This program is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License along
  21   * with this program; if not, write to the Free Software Foundation, Inc.,
  22   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23   * http://www.gnu.org/copyleft/gpl.html
  24   *
  25   * @todo Report PHP version, OS ..
  26   * @file
  27   * @ingroup Benchmark
  28   */
  29  
  30  require_once  __DIR__ . '/../Maintenance.php';
  31  
  32  /**
  33   * Base class for benchmark scripts.
  34   *
  35   * @ingroup Benchmark
  36   */
  37  abstract class Benchmarker extends Maintenance {
  38      private $results;
  39  
  40  	public function __construct() {
  41          parent::__construct();
  42          $this->addOption( 'count', "How many time to run a benchmark", false, true );
  43      }
  44  
  45  	public function bench( array $benchs ) {
  46          $bench_number = 0;
  47          $count = $this->getOption( 'count', 100 );
  48  
  49          foreach ( $benchs as $bench ) {
  50              // handle empty args
  51              if ( !array_key_exists( 'args', $bench ) ) {
  52                  $bench['args'] = array();
  53              }
  54  
  55              $bench_number++;
  56              $start = microtime( true );
  57              for ( $i = 0; $i < $count; $i++ ) {
  58                  call_user_func_array( $bench['function'], $bench['args'] );
  59              }
  60              $delta = microtime( true ) - $start;
  61  
  62              // function passed as a callback
  63              if ( is_array( $bench['function'] ) ) {
  64                  $ret = get_class( $bench['function'][0] ) . '->' . $bench['function'][1];
  65                  $bench['function'] = $ret;
  66              }
  67  
  68              $this->results[$bench_number] = array(
  69                  'function' => $bench['function'],
  70                  'arguments' => $bench['args'],
  71                  'count' => $count,
  72                  'delta' => $delta,
  73                  'average' => $delta / $count,
  74              );
  75          }
  76      }
  77  
  78  	public function getFormattedResults() {
  79          $ret = '';
  80          foreach ( $this->results as $res ) {
  81              // show function with args
  82              $ret .= sprintf( "%s times: function %s(%s) :\n",
  83                  $res['count'],
  84                  $res['function'],
  85                  join( ', ', $res['arguments'] )
  86              );
  87              $ret .= sprintf( "   %6.2fms (%6.2fms each)\n",
  88                  $res['delta'] * 1000,
  89                  $res['average'] * 1000
  90              );
  91          }
  92  
  93          return $ret;
  94      }
  95  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1