MediaWiki REL1_34
ProfilerXhprof.php
Go to the documentation of this file.
1<?php
57class 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}
Profiler wrapper for XHProf extension.
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
getRawData()
Retrieve raw data from xhprof.
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
close()
No-op for xhprof profiling.
__construct(array $params=[])
shouldExclude( $name)
Check if a function or section should be excluded from the output.
getFunctionReport()
Get a report of profiled functions sorted by inclusive wall clock time in descending order.
getOutput()
Returns a profiling output to be stored in debug file.
SectionProfiler $sprofiler
Profiler for explicit, arbitrary, frame labels.
Profiler base class that defines the interface and some trivial functionality.
Definition Profiler.php:33
array $params
All of the params passed from $wgProfiler.
Definition Profiler.php:37
Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle.
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.
static enable( $flags=0, $options=[])
Start xhprof profiler.
Definition Xhprof.php:52
static disable()
Stop xhprof profiler.
Definition Xhprof.php:78