MediaWiki REL1_35
ProfilerXhprof.php
Go to the documentation of this file.
1<?php
55class ProfilerXhprof extends Profiler {
59 protected $xhprofData;
60
65 protected $sprofiler;
66
71 public function __construct( array $params = [] ) {
72 parent::__construct( $params );
73
74 $flags = $params['flags'] ?? 0;
75 $options = isset( $params['exclude'] )
76 ? [ 'ignored_functions' => $params['exclude'] ] : [];
77 Xhprof::enable( $flags, $options );
78 $this->sprofiler = new SectionProfiler();
79 }
80
84 public function getXhprofData() {
85 if ( !$this->xhprofData ) {
86 $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
87 }
88 return $this->xhprofData;
89 }
90
91 public function scopedProfileIn( $section ) {
92 $key = 'section.' . ltrim( $section, '.' );
93 return $this->sprofiler->scopedProfileIn( $key );
94 }
95
99 public function close() {
100 }
101
108 private function shouldExclude( $name ) {
109 if ( $name === '-total' ) {
110 return true;
111 }
112 if ( !empty( $this->params['include'] ) ) {
113 foreach ( $this->params['include'] as $pattern ) {
114 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
115 return false;
116 }
117 }
118 return true;
119 }
120 if ( !empty( $this->params['exclude'] ) ) {
121 foreach ( $this->params['exclude'] as $pattern ) {
122 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
123 return true;
124 }
125 }
126 }
127 return false;
128 }
129
130 public function getFunctionStats() {
131 $metrics = $this->getXhprofData()->getCompleteMetrics();
132 $profile = [];
133
134 $main = null; // units in ms
135 foreach ( $metrics as $fname => $stats ) {
136 if ( $this->shouldExclude( $fname ) ) {
137 continue;
138 }
139 // Convert elapsed times from μs to ms to match interface
140 $entry = [
141 'name' => $fname,
142 'calls' => $stats['ct'],
143 'real' => $stats['wt']['total'] / 1000,
144 '%real' => $stats['wt']['percent'],
145 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
146 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
147 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
148 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
149 'min_real' => $stats['wt']['min'] / 1000,
150 'max_real' => $stats['wt']['max'] / 1000
151 ];
152 $profile[] = $entry;
153 if ( $fname === 'main()' ) {
154 $main = $entry;
155 }
156 }
157
158 // Merge in all of the custom profile sections
159 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
160 if ( $this->shouldExclude( $stats['name'] ) ) {
161 continue;
162 }
163
164 // @note: getFunctionStats() values already in ms
165 $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
166 $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
167 $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
168 $profile[] = $stats; // assume no section names collide with $metrics
169 }
170
171 return $profile;
172 }
173
179 public function getOutput() {
180 return $this->getFunctionReport();
181 }
182
199 protected function getFunctionReport() {
200 $data = $this->getFunctionStats();
201 usort( $data, function ( $a, $b ) {
202 return $b['real'] <=> $a['real']; // descending
203 } );
204
205 $width = 140;
206 $nameWidth = $width - 65;
207 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
208 $out = [];
209 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
210 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
211 );
212 foreach ( $data as $stats ) {
213 $out[] = sprintf( $format,
214 $stats['name'],
215 $stats['calls'],
216 $stats['real'] * 1000,
217 $stats['min_real'] * 1000,
218 $stats['real'] / $stats['calls'] * 1000,
219 $stats['max_real'] * 1000,
220 $stats['%real'],
221 $stats['memory']
222 );
223 }
224 return implode( "\n", $out );
225 }
226
231 public function getRawData() {
232 return $this->getXhprofData()->getRawData();
233 }
234}
Profiler wrapper for XHProf 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=[])
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 shared functionality.
Definition Profiler.php:33
array $params
All of the params passed from $wgProfiler.
Definition Profiler.php:37
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 xhprof profiler.
Definition Xhprof.php:50
static disable()
Stop xhprof profiler.
Definition Xhprof.php:76