Refactor profiling output from profiling
[lhc/web/wiklou.git] / includes / profiler / ProfilerXhprof.php
1 <?php
2 /**
3 * @section LICENSE
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 /**
23 * Profiler wrapper for XHProf extension.
24 *
25 * Mimics the output of ProfilerStandard using data collected via the XHProf
26 * PHP extension.
27 *
28 * @code
29 * $wgProfiler['class'] = 'ProfilerXhprof';
30 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
31 * $wgProfiler['output'] = 'text';
32 * $wgProfiler['visible'] = true;
33 * @endcode
34 *
35 * @code
36 * $wgProfiler['class'] = 'ProfilerXhprof';
37 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
38 * $wgProfiler['output'] = 'udp';
39 * @endcode
40 *
41 * Rather than obeying wfProfileIn() and wfProfileOut() calls placed in the
42 * application code, ProfilerXhprof profiles all functions using the XHProf
43 * PHP extenstion. For PHP5 users, this extension can be installed via PECL or
44 * your operating system's package manager. XHProf support is built into HHVM.
45 *
46 * To restrict the functions for which profiling data is collected, you can
47 * use either a whitelist ($wgProfiler['include']) or a blacklist
48 * ($wgProfiler['exclude']) containing an array of function names. The
49 * blacklist functionality is built into HHVM and will completely exclude the
50 * named functions from profiling collection. The whitelist is implemented by
51 * Xhprof class which will filter the data collected by XHProf before reporting.
52 * See documentation for the Xhprof class and the XHProf extension for
53 * additional information.
54 *
55 * @author Bryan Davis <bd808@wikimedia.org>
56 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
57 * @ingroup Profiler
58 * @see Xhprof
59 * @see https://php.net/xhprof
60 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
61 */
62 class ProfilerXhprof extends Profiler {
63
64 /**
65 * @var Xhprof $xhprof
66 */
67 protected $xhprof;
68
69 /**
70 * Type of report to send when logData() is called.
71 * @var string $logType
72 */
73 protected $logType;
74
75 /**
76 * Should profile report sent to in page content be visible?
77 * @var bool $visible
78 */
79 protected $visible;
80
81 /**
82 * @param array $params
83 * @see Xhprof::__construct()
84 */
85 public function __construct( array $params = array() ) {
86 $params = array_merge(
87 array(
88 'log' => 'text',
89 'visible' => false
90 ),
91 $params
92 );
93 parent::__construct( $params );
94 $this->logType = $params['log'];
95 $this->visible = $params['visible'];
96 $this->xhprof = new Xhprof( $params );
97 }
98
99 public function isStub() {
100 return false;
101 }
102
103 /**
104 * No-op for xhprof profiling.
105 *
106 * Use the 'include' configuration key instead if you need to constrain
107 * the functions that are profiled.
108 *
109 * @param string $functionname
110 */
111 public function profileIn( $functionname ) {
112 global $wgDebugFunctionEntry;
113 if ( $wgDebugFunctionEntry ) {
114 $this->debug( "Entering {$functionname}" );
115 }
116 }
117
118 /**
119 * No-op for xhprof profiling.
120 *
121 * Use the 'include' configuration key instead if you need to constrain
122 * the functions that are profiled.
123 *
124 * @param string $functionname
125 */
126 public function profileOut( $functionname ) {
127 global $wgDebugFunctionEntry;
128 if ( $wgDebugFunctionEntry ) {
129 $this->debug( "Exiting {$functionname}" );
130 }
131 }
132
133 /**
134 * No-op for xhprof profiling.
135 */
136 public function close() {
137 }
138
139 public function getFunctionStats() {
140 $metrics = $this->xhprof->getCompleteMetrics();
141 $profile = array();
142
143 foreach ( $metrics as $fname => $stats ) {
144 // Convert elapsed times from μs to ms to match ProfilerStandard
145 $profile[] = array(
146 'name' => $fname,
147 'calls' => $stats['ct'],
148 'real' => $stats['wt']['total'] / 1000,
149 '%real' => $stats['wt']['percent'],
150 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
151 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
152 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
153 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
154 'min' => $stats['wt']['min'] / 1000,
155 'max' => $stats['wt']['max'] / 1000
156 );
157 }
158
159 return $profile;
160 }
161
162 /**
163 * Returns a profiling output to be stored in debug file
164 *
165 * @return string
166 */
167 public function getOutput() {
168 return $this->getFunctionReport();
169 }
170
171 /**
172 * Get a report of profiled functions sorted by inclusive wall clock time
173 * in descending order.
174 *
175 * Each line of the report includes this data:
176 * - Function name
177 * - Number of times function was called
178 * - Total wall clock time spent in function in microseconds
179 * - Minimum wall clock time spent in function in microseconds
180 * - Average wall clock time spent in function in microseconds
181 * - Maximum wall clock time spent in function in microseconds
182 * - Percentage of total wall clock time spent in function
183 * - Total delta of memory usage from start to end of function in bytes
184 *
185 * @return string
186 */
187 protected function getFunctionReport() {
188 $data = $this->xhprof->getInclusiveMetrics();
189 uasort( $data, Xhprof::makeSortFunction( 'wt', 'total' ) );
190
191 $width = 140;
192 $nameWidth = $width - 65;
193 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
194 $out = array();
195 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
196 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
197 );
198 foreach ( $data as $func => $stats ) {
199 $out[] = sprintf( $format,
200 $func,
201 $stats['ct'],
202 $stats['wt']['total'],
203 $stats['wt']['min'],
204 $stats['wt']['mean'],
205 $stats['wt']['max'],
206 $stats['wt']['percent'],
207 isset( $stats['mu'] ) ? $stats['mu']['total'] : 0
208 );
209 }
210 return implode( "\n", $out );
211 }
212
213 /**
214 * Get a brief report of profiled functions sorted by inclusive wall clock
215 * time in descending order.
216 *
217 * Each line of the report includes this data:
218 * - Percentage of total wall clock time spent in function
219 * - Total wall clock time spent in function in seconds
220 * - Number of times function was called
221 * - Function name
222 *
223 * @param string $header Header text to prepend to report
224 * @param string $footer Footer text to append to report
225 * @return string
226 */
227 protected function getSummaryReport( $header = '', $footer = '' ) {
228 $data = $this->xhprof->getInclusiveMetrics();
229 uasort( $data, Xhprof::makeSortFunction( 'wt', 'total' ) );
230
231 $format = '%6.2f%% %3.6f %6d - %s';
232 $out = array( $header );
233 foreach ( $data as $func => $stats ) {
234 $out[] = sprintf( $format,
235 $stats['wt']['percent'],
236 $stats['wt']['total'] / 1e6,
237 $stats['ct'],
238 $func
239 );
240 }
241 $out[] = $footer;
242 return implode( "\n", $out );
243 }
244 }