MediaWiki  1.34.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 = $params['flags'] ?? 0;
77  $options = isset( $params['exclude'] )
78  ? [ 'ignored_functions' => $params['exclude'] ] : [];
79  Xhprof::enable( $flags, $options );
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  return $b['real'] <=> $a['real']; // descending
205  } );
206 
207  $width = 140;
208  $nameWidth = $width - 65;
209  $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
210  $out = [];
211  $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
212  'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
213  );
214  foreach ( $data as $stats ) {
215  $out[] = sprintf( $format,
216  $stats['name'],
217  $stats['calls'],
218  $stats['real'] * 1000,
219  $stats['min_real'] * 1000,
220  $stats['real'] / $stats['calls'] * 1000,
221  $stats['max_real'] * 1000,
222  $stats['%real'],
223  $stats['memory']
224  );
225  }
226  return implode( "\n", $out );
227  }
228 
233  public function getRawData() {
234  return $this->getXhprofData()->getRawData();
235  }
236 }
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
Xhprof\disable
static disable()
Stop xhprof profiler.
Definition: Xhprof.php:78
ProfilerXhprof\close
close()
No-op for xhprof profiling.
Definition: ProfilerXhprof.php:101
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:233
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
Profiler\$params
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:37
ProfilerXhprof\__construct
__construct(array $params=[])
Definition: ProfilerXhprof.php:73
ProfilerXhprof\shouldExclude
shouldExclude( $name)
Check if a function or section should be excluded from the output.
Definition: ProfilerXhprof.php:110