wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / profiler / ProfilerXhprof.php
blob5f7fc0028a0bd4e9bb51b87015c9b7b4f073a995
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 /**
22 * Profiler wrapper for XHProf extension.
24 * @code
25 * $wgProfiler['class'] = 'ProfilerXhprof';
26 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
27 * $wgProfiler['output'] = 'text';
28 * $wgProfiler['visible'] = true;
29 * @endcode
31 * @code
32 * $wgProfiler['class'] = 'ProfilerXhprof';
33 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
34 * $wgProfiler['output'] = 'udp';
35 * @endcode
37 * ProfilerXhprof profiles all functions using the XHProf PHP extenstion.
38 * For PHP5 users, this extension can be installed via PECL or your operating
39 * system's package manager. XHProf support is built into HHVM.
41 * To restrict the functions for which profiling data is collected, you can
42 * use either a whitelist ($wgProfiler['include']) or a blacklist
43 * ($wgProfiler['exclude']) containing an array of function names.
44 * Shell-style patterns are also accepted.
46 * @author Bryan Davis <bd808@wikimedia.org>
47 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
48 * @ingroup Profiler
49 * @see Xhprof
50 * @see https://php.net/xhprof
51 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
53 class ProfilerXhprof extends Profiler {
54 /**
55 * @var Xhprof $xhprof
57 protected $xhprof;
59 /**
60 * Profiler for explicit, arbitrary, frame labels
61 * @var SectionProfiler
63 protected $sprofiler;
65 /**
66 * @param array $params
67 * @see Xhprof::__construct()
69 public function __construct( array $params = array() ) {
70 parent::__construct( $params );
71 $this->xhprof = new Xhprof( $params );
72 $this->sprofiler = new SectionProfiler();
75 public function scopedProfileIn( $section ) {
76 $key = 'section.' . ltrim( $section, '.' );
77 return $this->sprofiler->scopedProfileIn( $key );
80 /**
81 * No-op for xhprof profiling.
83 public function close() {
86 /**
87 * Check if a function or section should be excluded from the output.
89 * @param string $name Function or section name.
90 * @return bool
92 private function shouldExclude( $name ) {
93 if ( $name === '-total' ) {
94 return true;
96 if ( !empty( $this->params['include'] ) ) {
97 foreach ( $this->params['include'] as $pattern ) {
98 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
99 return false;
102 return true;
104 if ( !empty( $this->params['exclude'] ) ) {
105 foreach ( $this->params['exclude'] as $pattern ) {
106 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
107 return true;
111 return false;
114 public function getFunctionStats() {
115 $metrics = $this->xhprof->getCompleteMetrics();
116 $profile = array();
118 $main = null; // units in ms
119 foreach ( $metrics as $fname => $stats ) {
120 if ( $this->shouldExclude( $fname ) ) {
121 continue;
123 // Convert elapsed times from μs to ms to match interface
124 $entry = array(
125 'name' => $fname,
126 'calls' => $stats['ct'],
127 'real' => $stats['wt']['total'] / 1000,
128 '%real' => $stats['wt']['percent'],
129 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
130 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
131 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
132 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
133 'min_real' => $stats['wt']['min'] / 1000,
134 'max_real' => $stats['wt']['max'] / 1000
136 $profile[] = $entry;
137 if ( $fname === 'main()' ) {
138 $main = $entry;
142 // Merge in all of the custom profile sections
143 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
144 if ( $this->shouldExclude( $stats['name'] ) ) {
145 continue;
148 // @note: getFunctionStats() values already in ms
149 $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
150 $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
151 $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
152 $profile[] = $stats; // assume no section names collide with $metrics
155 return $profile;
159 * Returns a profiling output to be stored in debug file
161 * @return string
163 public function getOutput() {
164 return $this->getFunctionReport();
168 * Get a report of profiled functions sorted by inclusive wall clock time
169 * in descending order.
171 * Each line of the report includes this data:
172 * - Function name
173 * - Number of times function was called
174 * - Total wall clock time spent in function in microseconds
175 * - Minimum wall clock time spent in function in microseconds
176 * - Average wall clock time spent in function in microseconds
177 * - Maximum wall clock time spent in function in microseconds
178 * - Percentage of total wall clock time spent in function
179 * - Total delta of memory usage from start to end of function in bytes
181 * @return string
183 protected function getFunctionReport() {
184 $data = $this->getFunctionStats();
185 usort( $data, function( $a, $b ) {
186 if ( $a['real'] === $b['real'] ) {
187 return 0;
189 return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
190 } );
192 $width = 140;
193 $nameWidth = $width - 65;
194 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
195 $out = array();
196 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
197 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
199 foreach ( $data as $stats ) {
200 $out[] = sprintf( $format,
201 $stats['name'],
202 $stats['calls'],
203 $stats['real'] * 1000,
204 $stats['min_real'] * 1000,
205 $stats['real'] / $stats['calls'] * 1000,
206 $stats['max_real'] * 1000,
207 $stats['%real'],
208 $stats['memory']
211 return implode( "\n", $out );
215 * Retrieve raw data from xhprof
216 * @return array
218 public function getRawData() {
219 return $this->xhprof->getRawData();