MediaWiki  1.28.1
ProfilerXhprof.php
Go to the documentation of this file.
1 <?php
53 class ProfilerXhprof extends Profiler {
57  protected $xhprofData;
58 
63  protected $sprofiler;
64 
69  public function __construct( array $params = [] ) {
70  parent::__construct( $params );
71 
72  $flags = isset( $params['flags'] ) ? $params['flags'] : 0;
73  $options = isset( $params['exclude'] )
74  ? [ 'ignored_functions' => $params['exclude'] ] : [];
76  $this->sprofiler = new SectionProfiler();
77  }
78 
82  public function getXhprofData() {
83  if ( !$this->xhprofData ) {
84  $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
85  }
86  return $this->xhprofData;
87  }
88 
89  public function scopedProfileIn( $section ) {
90  $key = 'section.' . ltrim( $section, '.' );
91  return $this->sprofiler->scopedProfileIn( $key );
92  }
93 
97  public function close() {
98  }
99 
106  private function shouldExclude( $name ) {
107  if ( $name === '-total' ) {
108  return true;
109  }
110  if ( !empty( $this->params['include'] ) ) {
111  foreach ( $this->params['include'] as $pattern ) {
112  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
113  return false;
114  }
115  }
116  return true;
117  }
118  if ( !empty( $this->params['exclude'] ) ) {
119  foreach ( $this->params['exclude'] as $pattern ) {
120  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
121  return true;
122  }
123  }
124  }
125  return false;
126  }
127 
128  public function getFunctionStats() {
129  $metrics = $this->getXhprofData()->getCompleteMetrics();
130  $profile = [];
131 
132  $main = null; // units in ms
133  foreach ( $metrics as $fname => $stats ) {
134  if ( $this->shouldExclude( $fname ) ) {
135  continue;
136  }
137  // Convert elapsed times from μs to ms to match interface
138  $entry = [
139  'name' => $fname,
140  'calls' => $stats['ct'],
141  'real' => $stats['wt']['total'] / 1000,
142  '%real' => $stats['wt']['percent'],
143  'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
144  '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
145  'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
146  '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
147  'min_real' => $stats['wt']['min'] / 1000,
148  'max_real' => $stats['wt']['max'] / 1000
149  ];
150  $profile[] = $entry;
151  if ( $fname === 'main()' ) {
152  $main = $entry;
153  }
154  }
155 
156  // Merge in all of the custom profile sections
157  foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
158  if ( $this->shouldExclude( $stats['name'] ) ) {
159  continue;
160  }
161 
162  // @note: getFunctionStats() values already in ms
163  $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
164  $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
165  $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
166  $profile[] = $stats; // assume no section names collide with $metrics
167  }
168 
169  return $profile;
170  }
171 
177  public function getOutput() {
178  return $this->getFunctionReport();
179  }
180 
197  protected function getFunctionReport() {
198  $data = $this->getFunctionStats();
199  usort( $data, function( $a, $b ) {
200  if ( $a['real'] === $b['real'] ) {
201  return 0;
202  }
203  return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
204  } );
205 
206  $width = 140;
207  $nameWidth = $width - 65;
208  $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
209  $out = [];
210  $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
211  'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
212  );
213  foreach ( $data as $stats ) {
214  $out[] = sprintf( $format,
215  $stats['name'],
216  $stats['calls'],
217  $stats['real'] * 1000,
218  $stats['min_real'] * 1000,
219  $stats['real'] / $stats['calls'] * 1000,
220  $stats['max_real'] * 1000,
221  $stats['%real'],
222  $stats['memory']
223  );
224  }
225  return implode( "\n", $out );
226  }
227 
232  public function getRawData() {
233  return $this->getXhprofData()->getRawData();
234  }
235 }
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:802
the array() calling protocol came about after MediaWiki 1.4rc1.
static disable()
Stop xhprof profiler.
Definition: Xhprof.php:57
static enable($flags=0, $options=[])
Start xhprof profiler.
Definition: Xhprof.php:44
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:38
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2703
__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.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1046
Profiler wrapper for XHProf extension.
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.
Definition: XhprofData.php:32
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:2889
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:36
getRawData()
Retrieve raw data from xhprof.
Profiler base class that defines the interface and some trivial functionality.
Definition: Profiler.php:32
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300