MediaWiki REL1_28
Benchmarker.php
Go to the documentation of this file.
1<?php
29require_once __DIR__ . '/../Maintenance.php';
30
36abstract class Benchmarker extends Maintenance {
37 private $results;
38
39 public function __construct() {
40 parent::__construct();
41 $this->addOption( 'count', "How many times to run a benchmark", false, true );
42 }
43
44 public function bench( array $benchs ) {
45 $bench_number = 0;
46 $count = $this->getOption( 'count', 100 );
47
48 foreach ( $benchs as $bench ) {
49 // handle empty args
50 if ( !array_key_exists( 'args', $bench ) ) {
51 $bench['args'] = [];
52 }
53
54 $bench_number++;
55 $start = microtime( true );
56 for ( $i = 0; $i < $count; $i++ ) {
57 call_user_func_array( $bench['function'], $bench['args'] );
58 }
59 $delta = microtime( true ) - $start;
60
61 // function passed as a callback
62 if ( is_array( $bench['function'] ) ) {
63 $ret = get_class( $bench['function'][0] ) . '->' . $bench['function'][1];
64 $bench['function'] = $ret;
65 }
66
67 $this->results[$bench_number] = [
68 'function' => $bench['function'],
69 'arguments' => $bench['args'],
70 'count' => $count,
71 'delta' => $delta,
72 'average' => $delta / $count,
73 ];
74 }
75 }
76
77 public function getFormattedResults() {
78 $ret = sprintf( "Running PHP version %s (%s) on %s %s %s\n\n",
79 phpversion(),
80 php_uname( 'm' ),
81 php_uname( 's' ),
82 php_uname( 'r' ),
83 php_uname( 'v' )
84 );
85 foreach ( $this->results as $res ) {
86 // show function with args
87 $ret .= sprintf( "%s times: function %s(%s) :\n",
88 $res['count'],
89 $res['function'],
90 implode( ', ', $res['arguments'] )
91 );
92 $ret .= sprintf( " %6.2fms (%6.2fms each)\n",
93 $res['delta'] * 1000,
94 $res['average'] * 1000
95 );
96 }
97
98 return $ret;
99 }
100}
Base class for benchmark scripts.
__construct()
Default constructor.
bench(array $benchs)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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.
$res
Definition database.txt:21
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:1949
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37