71 if ( empty(
$params[
'running'] ) ) {
73 if ( function_exists(
'xhprof_enable' ) ) {
74 $options = isset(
$params[
'exclude'] )
75 ? [
'ignored_functions' =>
$params[
'exclude'] ]
77 xhprof_enable( $flags, $options );
78 } elseif ( function_exists(
'tideways_xhprof_enable' ) ) {
79 if ( isset(
$params[
'exclude'] ) ) {
80 throw new RuntimeException(
'The exclude option is not supported in tideways_xhprof' );
82 tideways_xhprof_enable( $flags );
84 throw new RuntimeException(
'Neither xhprof nor tideways_xhprof is installed' );
95 if ( !$this->xhprofData ) {
96 if ( function_exists(
'xhprof_disable' ) ) {
97 $data = xhprof_disable();
98 } elseif ( function_exists(
'tideways_xhprof_disable' ) ) {
99 $data = tideways_xhprof_disable();
101 throw new RuntimeException(
'Neither xhprof nor tideways_xhprof is installed' );
103 $this->xhprofData =
new XhprofData( $data, $this->params );
109 $key =
'section.' . ltrim( $section,
'.' );
110 return $this->sprofiler->scopedProfileIn( $key );
125 private function shouldExclude( $name ) {
126 if ( $name ===
'-total' ) {
129 if ( !empty( $this->params[
'include'] ) ) {
130 foreach ( $this->params[
'include'] as $pattern ) {
131 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
137 if ( !empty( $this->params[
'exclude'] ) ) {
138 foreach ( $this->params[
'exclude'] as $pattern ) {
139 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
152 foreach ( $metrics as $fname => $stats ) {
153 if ( $this->shouldExclude( $fname ) ) {
159 'calls' => $stats[
'ct'],
160 'real' => $stats[
'wt'][
'total'] / 1000,
161 '%real' => $stats[
'wt'][
'percent'],
162 'cpu' => ( $stats[
'cpu'][
'total'] ?? 0 ) / 1000,
163 '%cpu' => $stats[
'cpu'][
'percent'] ?? 0,
164 'memory' => $stats[
'mu'][
'total'] ?? 0,
165 '%memory' => $stats[
'mu'][
'percent'] ?? 0,
166 'min_real' => $stats[
'wt'][
'min'] / 1000,
167 'max_real' => $stats[
'wt'][
'max'] / 1000
170 if ( $fname ===
'main()' ) {
176 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
177 if ( $this->shouldExclude( $stats[
'name'] ) ) {
182 $stats[
'%real'] = $main[
'real'] ? $stats[
'real'] / $main[
'real'] * 100 : 0;
183 $stats[
'%cpu'] = $main[
'cpu'] ? $stats[
'cpu'] / $main[
'cpu'] * 100 : 0;
184 $stats[
'%memory'] = $main[
'memory'] ? $stats[
'memory'] / $main[
'memory'] * 100 : 0;
218 usort( $data,
static function ( $a, $b ) {
219 return $b[
'real'] <=> $a[
'real'];
223 $nameWidth = $width - 65;
224 $format =
"%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
226 $out[] = sprintf(
"%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
227 'Name',
'Calls',
'Total',
'Min',
'Each',
'Max',
'%',
'Mem'
229 foreach ( $data as $stats ) {
230 $out[] = sprintf( $format,
233 $stats[
'real'] * 1000,
234 $stats[
'min_real'] * 1000,
235 $stats[
'real'] / $stats[
'calls'] * 1000,
236 $stats[
'max_real'] * 1000,
241 return implode(
"\n", $out );
Profiler that captures all function calls from the XHProf PHP 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=[])
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.
array $params
All of the params passed from $wgProfiler.
Arbitrary section name based PHP profiling.
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.