MediaWiki REL1_39
ProfilerXhprof.php
Go to the documentation of this file.
1<?php
36class ProfilerXhprof extends Profiler {
40 protected $xhprofData;
41
46 protected $sprofiler;
47
69 public function __construct( array $params = [] ) {
70 parent::__construct( $params );
71
72 // See T180183 and T247332 for why we need the 'running' option.
73 if ( empty( $params['running'] ) ) {
74 $flags = $params['flags'] ?? 0;
75 $options = isset( $params['exclude'] )
76 ? [ 'ignored_functions' => $params['exclude'] ]
77 : [];
78
79 Xhprof::enable( $flags, $options );
80 }
81
82 $this->sprofiler = new SectionProfiler();
83 }
84
88 public function getXhprofData() {
89 if ( !$this->xhprofData ) {
90 $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
91 }
92 return $this->xhprofData;
93 }
94
95 public function scopedProfileIn( $section ) {
96 $key = 'section.' . ltrim( $section, '.' );
97 return $this->sprofiler->scopedProfileIn( $key );
98 }
99
103 public function close() {
104 }
105
112 private function shouldExclude( $name ) {
113 if ( $name === '-total' ) {
114 return true;
115 }
116 if ( !empty( $this->params['include'] ) ) {
117 foreach ( $this->params['include'] as $pattern ) {
118 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
119 return false;
120 }
121 }
122 return true;
123 }
124 if ( !empty( $this->params['exclude'] ) ) {
125 foreach ( $this->params['exclude'] as $pattern ) {
126 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
127 return true;
128 }
129 }
130 }
131 return false;
132 }
133
134 public function getFunctionStats() {
135 $metrics = $this->getXhprofData()->getCompleteMetrics();
136 $profile = [];
137
138 $main = null; // units in ms
139 foreach ( $metrics as $fname => $stats ) {
140 if ( $this->shouldExclude( $fname ) ) {
141 continue;
142 }
143 // Convert elapsed times from μs to ms to match interface
144 $entry = [
145 'name' => $fname,
146 'calls' => $stats['ct'],
147 'real' => $stats['wt']['total'] / 1000,
148 '%real' => $stats['wt']['percent'],
149 'cpu' => ( $stats['cpu']['total'] ?? 0 ) / 1000,
150 '%cpu' => $stats['cpu']['percent'] ?? 0,
151 'memory' => $stats['mu']['total'] ?? 0,
152 '%memory' => $stats['mu']['percent'] ?? 0,
153 'min_real' => $stats['wt']['min'] / 1000,
154 'max_real' => $stats['wt']['max'] / 1000
155 ];
156 $profile[] = $entry;
157 if ( $fname === 'main()' ) {
158 $main = $entry;
159 }
160 }
161
162 // Merge in all of the custom profile sections
163 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
164 if ( $this->shouldExclude( $stats['name'] ) ) {
165 continue;
166 }
167
168 // @note: getFunctionStats() values already in ms
169 $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
170 $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
171 $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
172 $profile[] = $stats; // assume no section names collide with $metrics
173 }
174
175 return $profile;
176 }
177
183 public function getOutput() {
184 return $this->getFunctionReport();
185 }
186
203 protected function getFunctionReport() {
204 $data = $this->getFunctionStats();
205 usort( $data, static function ( $a, $b ) {
206 return $b['real'] <=> $a['real']; // descending
207 } );
208
209 $width = 140;
210 $nameWidth = $width - 65;
211 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
212 $out = [];
213 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
214 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
215 );
216 foreach ( $data as $stats ) {
217 $out[] = sprintf( $format,
218 $stats['name'],
219 $stats['calls'],
220 $stats['real'] * 1000,
221 $stats['min_real'] * 1000,
222 $stats['real'] / $stats['calls'] * 1000,
223 $stats['max_real'] * 1000,
224 $stats['%real'],
225 $stats['memory']
226 );
227 }
228 return implode( "\n", $out );
229 }
230
235 public function getRawData() {
236 return $this->getXhprofData()->getRawData();
237 }
238}
Profiler that captures all function calls from the XHProf PHP extension.
XhprofData null $xhprofData
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=[])
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 shared functionality.
Definition Profiler.php:36
array $params
All of the params passed from $wgProfiler.
Definition Profiler.php:40
Arbitrary section name based PHP profiling.
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.
static enable( $flags=0, $options=[])
Start profiler.
Definition Xhprof.php:39
static disable()
Stop profiler.
Definition Xhprof.php:61