62 if ( empty(
$params[
'running'] ) ) {
64 if ( function_exists(
'xhprof_enable' ) ) {
65 $options = isset(
$params[
'exclude'] )
66 ? [
'ignored_functions' =>
$params[
'exclude'] ]
68 xhprof_enable( $flags, $options );
69 } elseif ( function_exists(
'tideways_xhprof_enable' ) ) {
70 if ( isset(
$params[
'exclude'] ) ) {
71 throw new RuntimeException(
'The exclude option is not supported in tideways_xhprof' );
73 tideways_xhprof_enable( $flags );
75 throw new RuntimeException(
'Neither xhprof nor tideways_xhprof is installed' );
86 if ( !$this->xhprofData ) {
87 if ( function_exists(
'xhprof_disable' ) ) {
88 $data = xhprof_disable();
89 } elseif ( function_exists(
'tideways_xhprof_disable' ) ) {
90 $data = tideways_xhprof_disable();
92 throw new RuntimeException(
'Neither xhprof nor tideways_xhprof is installed' );
94 $this->xhprofData =
new XhprofData( $data, $this->params );
102 $key =
'section.' . ltrim( $section,
'.' );
103 return $this->sprofiler->scopedProfileIn( $key );
118 private function shouldExclude( $name ) {
119 if ( $name ===
'-total' ) {
122 if ( !empty( $this->params[
'include'] ) ) {
123 foreach ( $this->params[
'include'] as $pattern ) {
124 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
130 if ( !empty( $this->params[
'exclude'] ) ) {
131 foreach ( $this->params[
'exclude'] as $pattern ) {
132 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
142 $metrics = $this->getXhprofData()->getCompleteMetrics();
146 foreach ( $metrics as $fname => $stats ) {
147 if ( $this->shouldExclude( $fname ) ) {
153 'calls' => $stats[
'ct'],
154 'real' => $stats[
'wt'][
'total'] / 1000,
155 '%real' => $stats[
'wt'][
'percent'],
156 'cpu' => ( $stats[
'cpu'][
'total'] ?? 0 ) / 1000,
157 '%cpu' => $stats[
'cpu'][
'percent'] ?? 0,
158 'memory' => $stats[
'mu'][
'total'] ?? 0,
159 '%memory' => $stats[
'mu'][
'percent'] ?? 0,
160 'min_real' => $stats[
'wt'][
'min'] / 1000,
161 'max_real' => $stats[
'wt'][
'max'] / 1000
164 if ( $fname ===
'main()' ) {
170 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
171 if ( $this->shouldExclude( $stats[
'name'] ) ) {
176 $stats[
'%real'] = $main[
'real'] ? $stats[
'real'] / $main[
'real'] * 100 : 0;
177 $stats[
'%cpu'] = $main[
'cpu'] ? $stats[
'cpu'] / $main[
'cpu'] * 100 : 0;
178 $stats[
'%memory'] = $main[
'memory'] ? $stats[
'memory'] / $main[
'memory'] * 100 : 0;
191 return $this->getFunctionReport();
211 $data = $this->getFunctionStats();
212 usort( $data,
static function ( $a, $b ) {
213 return $b[
'real'] <=> $a[
'real'];
217 $nameWidth = $width - 65;
218 $format =
"%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
220 $out[] = sprintf(
"%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
221 'Name',
'Calls',
'Total',
'Min',
'Each',
'Max',
'%',
'Mem'
223 foreach ( $data as $stats ) {
224 $out[] = sprintf( $format,
227 $stats[
'real'] * 1000,
228 $stats[
'min_real'] * 1000,
229 $stats[
'real'] / $stats[
'calls'] * 1000,
230 $stats[
'max_real'] * 1000,
235 return implode(
"\n", $out );
243 return $this->getXhprofData()->getRawData();
248class_alias( ProfilerXhprof::class,
'ProfilerXhprof' );