MediaWiki  1.27.2
ProfilerXhprof.php
Go to the documentation of this file.
1 <?php
53 class ProfilerXhprof extends Profiler {
57  protected $xhprof;
58 
63  protected $sprofiler;
64 
69  public function __construct( array $params = [] ) {
70  parent::__construct( $params );
71  $this->xhprof = new Xhprof( $params );
72  $this->sprofiler = new SectionProfiler();
73  }
74 
75  public function scopedProfileIn( $section ) {
76  $key = 'section.' . ltrim( $section, '.' );
77  return $this->sprofiler->scopedProfileIn( $key );
78  }
79 
83  public function close() {
84  }
85 
92  private function shouldExclude( $name ) {
93  if ( $name === '-total' ) {
94  return true;
95  }
96  if ( !empty( $this->params['include'] ) ) {
97  foreach ( $this->params['include'] as $pattern ) {
98  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
99  return false;
100  }
101  }
102  return true;
103  }
104  if ( !empty( $this->params['exclude'] ) ) {
105  foreach ( $this->params['exclude'] as $pattern ) {
106  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
107  return true;
108  }
109  }
110  }
111  return false;
112  }
113 
114  public function getFunctionStats() {
115  $metrics = $this->xhprof->getCompleteMetrics();
116  $profile = [];
117 
118  $main = null; // units in ms
119  foreach ( $metrics as $fname => $stats ) {
120  if ( $this->shouldExclude( $fname ) ) {
121  continue;
122  }
123  // Convert elapsed times from μs to ms to match interface
124  $entry = [
125  'name' => $fname,
126  'calls' => $stats['ct'],
127  'real' => $stats['wt']['total'] / 1000,
128  '%real' => $stats['wt']['percent'],
129  'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
130  '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
131  'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
132  '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
133  'min_real' => $stats['wt']['min'] / 1000,
134  'max_real' => $stats['wt']['max'] / 1000
135  ];
136  $profile[] = $entry;
137  if ( $fname === 'main()' ) {
138  $main = $entry;
139  }
140  }
141 
142  // Merge in all of the custom profile sections
143  foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
144  if ( $this->shouldExclude( $stats['name'] ) ) {
145  continue;
146  }
147 
148  // @note: getFunctionStats() values already in ms
149  $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
150  $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
151  $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
152  $profile[] = $stats; // assume no section names collide with $metrics
153  }
154 
155  return $profile;
156  }
157 
163  public function getOutput() {
164  return $this->getFunctionReport();
165  }
166 
183  protected function getFunctionReport() {
184  $data = $this->getFunctionStats();
185  usort( $data, function( $a, $b ) {
186  if ( $a['real'] === $b['real'] ) {
187  return 0;
188  }
189  return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
190  } );
191 
192  $width = 140;
193  $nameWidth = $width - 65;
194  $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
195  $out = [];
196  $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
197  'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
198  );
199  foreach ( $data as $stats ) {
200  $out[] = sprintf( $format,
201  $stats['name'],
202  $stats['calls'],
203  $stats['real'] * 1000,
204  $stats['min_real'] * 1000,
205  $stats['real'] / $stats['calls'] * 1000,
206  $stats['max_real'] * 1000,
207  $stats['%real'],
208  $stats['memory']
209  );
210  }
211  return implode( "\n", $out );
212  }
213 
218  public function getRawData() {
219  return $this->xhprof->getRawData();
220  }
221 }
Convenience class for working with XHProf https://github.com/phacility/xhprof.
Definition: Xhprof.php:32
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:762
the array() calling protocol came about after MediaWiki 1.4rc1.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
scopedProfileIn($section)
getOutput()
Returns a profiling output to be stored in debug file.
close()
No-op for xhprof profiling.
SectionProfiler $sprofiler
Profiler for explicit, arbitrary, frame labels.
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:37
__construct(array $params=[])
getFunctionReport()
Get a report of profiled functions sorted by inclusive wall clock time in descending order...
Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle.
shouldExclude($name)
Check if a function or section should be excluded from the output.
Profiler wrapper for XHProf extension.
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
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:2715
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
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined...
Definition: Setup.php:35
getRawData()
Retrieve raw data from xhprof.
Profiler base class that defines the interface and some trivial functionality.
Definition: Profiler.php:31
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310