MediaWiki  1.29.2
ProfilerXhprof.php
Go to the documentation of this file.
1 <?php
58 class ProfilerXhprof extends Profiler {
62  protected $xhprofData;
63 
68  protected $sprofiler;
69 
74  public function __construct( array $params = [] ) {
75  parent::__construct( $params );
76 
77  $flags = isset( $params['flags'] ) ? $params['flags'] : 0;
78  $options = isset( $params['exclude'] )
79  ? [ 'ignored_functions' => $params['exclude'] ] : [];
81  $this->sprofiler = new SectionProfiler();
82  }
83 
87  public function getXhprofData() {
88  if ( !$this->xhprofData ) {
89  $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
90  }
91  return $this->xhprofData;
92  }
93 
94  public function scopedProfileIn( $section ) {
95  $key = 'section.' . ltrim( $section, '.' );
96  return $this->sprofiler->scopedProfileIn( $key );
97  }
98 
102  public function close() {
103  }
104 
111  private function shouldExclude( $name ) {
112  if ( $name === '-total' ) {
113  return true;
114  }
115  if ( !empty( $this->params['include'] ) ) {
116  foreach ( $this->params['include'] as $pattern ) {
117  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
118  return false;
119  }
120  }
121  return true;
122  }
123  if ( !empty( $this->params['exclude'] ) ) {
124  foreach ( $this->params['exclude'] as $pattern ) {
125  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
126  return true;
127  }
128  }
129  }
130  return false;
131  }
132 
133  public function getFunctionStats() {
134  $metrics = $this->getXhprofData()->getCompleteMetrics();
135  $profile = [];
136 
137  $main = null; // units in ms
138  foreach ( $metrics as $fname => $stats ) {
139  if ( $this->shouldExclude( $fname ) ) {
140  continue;
141  }
142  // Convert elapsed times from μs to ms to match interface
143  $entry = [
144  'name' => $fname,
145  'calls' => $stats['ct'],
146  'real' => $stats['wt']['total'] / 1000,
147  '%real' => $stats['wt']['percent'],
148  'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
149  '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
150  'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
151  '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
152  'min_real' => $stats['wt']['min'] / 1000,
153  'max_real' => $stats['wt']['max'] / 1000
154  ];
155  $profile[] = $entry;
156  if ( $fname === 'main()' ) {
157  $main = $entry;
158  }
159  }
160 
161  // Merge in all of the custom profile sections
162  foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
163  if ( $this->shouldExclude( $stats['name'] ) ) {
164  continue;
165  }
166 
167  // @note: getFunctionStats() values already in ms
168  $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
169  $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
170  $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
171  $profile[] = $stats; // assume no section names collide with $metrics
172  }
173 
174  return $profile;
175  }
176 
182  public function getOutput() {
183  return $this->getFunctionReport();
184  }
185 
202  protected function getFunctionReport() {
203  $data = $this->getFunctionStats();
204  usort( $data, function( $a, $b ) {
205  if ( $a['real'] === $b['real'] ) {
206  return 0;
207  }
208  return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
209  } );
210 
211  $width = 140;
212  $nameWidth = $width - 65;
213  $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
214  $out = [];
215  $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
216  'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
217  );
218  foreach ( $data as $stats ) {
219  $out[] = sprintf( $format,
220  $stats['name'],
221  $stats['calls'],
222  $stats['real'] * 1000,
223  $stats['min_real'] * 1000,
224  $stats['real'] / $stats['calls'] * 1000,
225  $stats['max_real'] * 1000,
226  $stats['%real'],
227  $stats['memory']
228  );
229  }
230  return implode( "\n", $out );
231  }
232 
237  public function getRawData() {
238  return $this->getXhprofData()->getRawData();
239  }
240 }
ProfilerXhprof\scopedProfileIn
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
Definition: ProfilerXhprof.php:94
ProfilerXhprof
Profiler wrapper for XHProf extension.
Definition: ProfilerXhprof.php:58
ProfilerXhprof\getOutput
getOutput()
Returns a profiling output to be stored in debug file.
Definition: ProfilerXhprof.php:182
Xhprof\enable
static enable( $flags=0, $options=[])
Start xhprof profiler.
Definition: Xhprof.php:48
$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:67
ProfilerXhprof\close
close()
No-op for xhprof profiling.
Definition: ProfilerXhprof.php:102
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
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:202
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:31
ProfilerXhprof\$sprofiler
SectionProfiler $sprofiler
Profiler for explicit, arbitrary, frame labels.
Definition: ProfilerXhprof.php:68
ProfilerXhprof\$xhprofData
$xhprofData
Definition: ProfilerXhprof.php:62
ProfilerXhprof\getXhprofData
getXhprofData()
Definition: ProfilerXhprof.php:87
ProfilerXhprof\getRawData
getRawData()
Retrieve raw data from xhprof.
Definition: ProfilerXhprof.php:237
ProfilerXhprof\getFunctionStats
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
Definition: ProfilerXhprof.php:133
XhprofData
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.
Definition: XhprofData.php:32
$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:2929
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
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
ProfilerXhprof\__construct
__construct(array $params=[])
Definition: ProfilerXhprof.php:74
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2749
ProfilerXhprof\shouldExclude
shouldExclude( $name)
Check if a function or section should be excluded from the output.
Definition: ProfilerXhprof.php:111
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:783