MediaWiki REL1_31
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 = isset( $params['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 if ( $a['real'] === $b['real'] ) {
205 return 0;
206 }
207 return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
208 } );
209
210 $width = 140;
211 $nameWidth = $width - 65;
212 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
213 $out = [];
214 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
215 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
216 );
217 foreach ( $data as $stats ) {
218 $out[] = sprintf( $format,
219 $stats['name'],
220 $stats['calls'],
221 $stats['real'] * 1000,
222 $stats['min_real'] * 1000,
223 $stats['real'] / $stats['calls'] * 1000,
224 $stats['max_real'] * 1000,
225 $stats['%real'],
226 $stats['memory']
227 );
228 }
229 return implode( "\n", $out );
230 }
231
236 public function getRawData() {
237 return $this->getXhprofData()->getRawData();
238 }
239}
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:112
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:39
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:71
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
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:864
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:3022