MediaWiki  1.30.0
ProfilerXhprof.php
Go to the documentation of this file.
1 <?php
57 class ProfilerXhprof extends Profiler {
61  protected $xhprofData;
62 
67  protected $sprofiler;
68 
73  public function __construct( array $params = [] ) {
74  parent::__construct( $params );
75 
76  $flags = isset( $params['flags'] ) ? $params['flags'] : 0;
77  $options = isset( $params['exclude'] )
78  ? [ 'ignored_functions' => $params['exclude'] ] : [];
80  $this->sprofiler = new SectionProfiler();
81  }
82 
86  public function getXhprofData() {
87  if ( !$this->xhprofData ) {
88  $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
89  }
90  return $this->xhprofData;
91  }
92 
93  public function scopedProfileIn( $section ) {
94  $key = 'section.' . ltrim( $section, '.' );
95  return $this->sprofiler->scopedProfileIn( $key );
96  }
97 
101  public function close() {
102  }
103 
110  private function shouldExclude( $name ) {
111  if ( $name === '-total' ) {
112  return true;
113  }
114  if ( !empty( $this->params['include'] ) ) {
115  foreach ( $this->params['include'] as $pattern ) {
116  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
117  return false;
118  }
119  }
120  return true;
121  }
122  if ( !empty( $this->params['exclude'] ) ) {
123  foreach ( $this->params['exclude'] as $pattern ) {
124  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
125  return true;
126  }
127  }
128  }
129  return false;
130  }
131 
132  public function getFunctionStats() {
133  $metrics = $this->getXhprofData()->getCompleteMetrics();
134  $profile = [];
135 
136  $main = null; // units in ms
137  foreach ( $metrics as $fname => $stats ) {
138  if ( $this->shouldExclude( $fname ) ) {
139  continue;
140  }
141  // Convert elapsed times from μs to ms to match interface
142  $entry = [
143  'name' => $fname,
144  'calls' => $stats['ct'],
145  'real' => $stats['wt']['total'] / 1000,
146  '%real' => $stats['wt']['percent'],
147  'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
148  '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
149  'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
150  '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
151  'min_real' => $stats['wt']['min'] / 1000,
152  'max_real' => $stats['wt']['max'] / 1000
153  ];
154  $profile[] = $entry;
155  if ( $fname === 'main()' ) {
156  $main = $entry;
157  }
158  }
159 
160  // Merge in all of the custom profile sections
161  foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
162  if ( $this->shouldExclude( $stats['name'] ) ) {
163  continue;
164  }
165 
166  // @note: getFunctionStats() values already in ms
167  $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
168  $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
169  $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
170  $profile[] = $stats; // assume no section names collide with $metrics
171  }
172 
173  return $profile;
174  }
175 
181  public function getOutput() {
182  return $this->getFunctionReport();
183  }
184 
201  protected function getFunctionReport() {
202  $data = $this->getFunctionStats();
203  usort( $data, function ( $a, $b ) {
204  if ( $a['real'] === $b['real'] ) {
205  return 0;
206  }
207  return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
208  } );
209 
210  $width = 140;
211  $nameWidth = $width - 65;
212  $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
213  $out = [];
214  $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
215  'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
216  );
217  foreach ( $data as $stats ) {
218  $out[] = sprintf( $format,
219  $stats['name'],
220  $stats['calls'],
221  $stats['real'] * 1000,
222  $stats['min_real'] * 1000,
223  $stats['real'] / $stats['calls'] * 1000,
224  $stats['max_real'] * 1000,
225  $stats['%real'],
226  $stats['memory']
227  );
228  }
229  return implode( "\n", $out );
230  }
231 
236  public function getRawData() {
237  return $this->getXhprofData()->getRawData();
238  }
239 }
ProfilerXhprof\scopedProfileIn
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
Definition: ProfilerXhprof.php:93
ProfilerXhprof
Profiler wrapper for XHProf extension.
Definition: ProfilerXhprof.php:57
ProfilerXhprof\getOutput
getOutput()
Returns a profiling output to be stored in debug file.
Definition: ProfilerXhprof.php:181
Xhprof\enable
static enable( $flags=0, $options=[])
Start xhprof profiler.
Definition: Xhprof.php:52
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:36
Xhprof\disable
static disable()
Stop xhprof profiler.
Definition: Xhprof.php:71
ProfilerXhprof\close
close()
No-op for xhprof profiling.
Definition: ProfilerXhprof.php:101
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
php
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:35
ProfilerXhprof\getFunctionReport
getFunctionReport()
Get a report of profiled functions sorted by inclusive wall clock time in descending order.
Definition: ProfilerXhprof.php:201
Profiler
Profiler base class that defines the interface and some trivial functionality.
Definition: Profiler.php:33
SectionProfiler
Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle.
Definition: SectionProfiler.php:30
ProfilerXhprof\$sprofiler
SectionProfiler $sprofiler
Profiler for explicit, arbitrary, frame labels.
Definition: ProfilerXhprof.php:67
ProfilerXhprof\$xhprofData
$xhprofData
Definition: ProfilerXhprof.php:61
ProfilerXhprof\getXhprofData
getXhprofData()
Definition: ProfilerXhprof.php:86
ProfilerXhprof\getRawData
getRawData()
Retrieve raw data from xhprof.
Definition: ProfilerXhprof.php:236
ProfilerXhprof\getFunctionStats
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
Definition: ProfilerXhprof.php:132
XhprofData
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.
Definition: XhprofData.php:31
$options
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 & $options
Definition: hooks.txt:1965
$section
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition: hooks.txt:2981
as
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
Definition: distributors.txt:9
Profiler\$params
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:39
ProfilerXhprof\__construct
__construct(array $params=[])
Definition: ProfilerXhprof.php:73
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2801
ProfilerXhprof\shouldExclude
shouldExclude( $name)
Check if a function or section should be excluded from the output.
Definition: ProfilerXhprof.php:110
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:781