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.
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.
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
23 * Profiler wrapper for XHProf extension.
25 * Mimics the output of ProfilerStandard using data collected via the XHProf
29 * $wgProfiler['class'] = 'ProfilerXhprof';
30 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
31 * $wgProfiler['output'] = 'text';
32 * $wgProfiler['visible'] = true;
36 * $wgProfiler['class'] = 'ProfilerXhprof';
37 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
38 * $wgProfiler['output'] = 'udp';
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.
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.
55 * @author Bryan Davis <bd808@wikimedia.org>
56 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
59 * @see https://php.net/xhprof
60 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
62 class ProfilerXhprof
extends Profiler
{
70 * Type of report to send when logData() is called.
71 * @var string $logType
76 * Should profile report sent to in page content be visible?
82 * @param array $params
83 * @see Xhprof::__construct()
85 public function __construct( array $params = array() ) {
86 $params = array_merge(
93 parent
::__construct( $params );
94 $this->logType
= $params['log'];
95 $this->visible
= $params['visible'];
96 $this->xhprof
= new Xhprof( $params );
99 public function isStub() {
104 * No-op for xhprof profiling.
106 * Use the 'include' configuration key instead if you need to constrain
107 * the functions that are profiled.
109 * @param string $functionname
111 public function profileIn( $functionname ) {
112 global $wgDebugFunctionEntry;
113 if ( $wgDebugFunctionEntry ) {
114 $this->debug( "Entering {$functionname}" );
119 * No-op for xhprof profiling.
121 * Use the 'include' configuration key instead if you need to constrain
122 * the functions that are profiled.
124 * @param string $functionname
126 public function profileOut( $functionname ) {
127 global $wgDebugFunctionEntry;
128 if ( $wgDebugFunctionEntry ) {
129 $this->debug( "Exiting {$functionname}" );
134 * No-op for xhprof profiling.
136 public function close() {
139 public function getFunctionStats() {
140 $metrics = $this->xhprof
->getCompleteMetrics();
143 foreach ( $metrics as $fname => $stats ) {
144 // Convert elapsed times from μs to ms to match ProfilerStandard
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
163 * Returns a profiling output to be stored in debug file
167 public function getOutput() {
168 return $this->getFunctionReport();
172 * Get a report of profiled functions sorted by inclusive wall clock time
173 * in descending order.
175 * Each line of the report includes this data:
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
187 protected function getFunctionReport() {
188 $data = $this->xhprof
->getInclusiveMetrics();
189 uasort( $data, Xhprof
::makeSortFunction( 'wt', 'total' ) );
192 $nameWidth = $width - 65;
193 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
195 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
196 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
198 foreach ( $data as $func => $stats ) {
199 $out[] = sprintf( $format,
202 $stats['wt']['total'],
204 $stats['wt']['mean'],
206 $stats['wt']['percent'],
207 isset( $stats['mu'] ) ?
$stats['mu']['total'] : 0
210 return implode( "\n", $out );
214 * Get a brief report of profiled functions sorted by inclusive wall clock
215 * time in descending order.
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
223 * @param string $header Header text to prepend to report
224 * @param string $footer Footer text to append to report
227 protected function getSummaryReport( $header = '', $footer = '' ) {
228 $data = $this->xhprof
->getInclusiveMetrics();
229 uasort( $data, Xhprof
::makeSortFunction( 'wt', 'total' ) );
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
,
242 return implode( "\n", $out );