From 9dfea19e7ba23cad97f31d49ab00265e7f4b877d Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 8 Jan 2015 02:10:02 -0800 Subject: [PATCH] Added ProfilerSectionOnly class Change-Id: I45d1036433a7cca13293de22adf1b63b916393fe --- autoload.php | 1 + includes/profiler/ProfilerSectionOnly.php | 104 ++++++++++++++++++++++ includes/profiler/ProfilerXhprof.php | 7 +- 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100755 includes/profiler/ProfilerSectionOnly.php diff --git a/autoload.php b/autoload.php index f4935b8c83..e23464e7c5 100644 --- a/autoload.php +++ b/autoload.php @@ -898,6 +898,7 @@ $wgAutoloadLocalClasses = array( 'ProfilerOutputDb' => __DIR__ . '/includes/profiler/output/ProfilerOutputDb.php', 'ProfilerOutputText' => __DIR__ . '/includes/profiler/output/ProfilerOutputText.php', 'ProfilerOutputUdp' => __DIR__ . '/includes/profiler/output/ProfilerOutputUdp.php', + 'ProfilerSectionOnly' => __DIR__ . '/includes/profiler/ProfilerSectionOnly.php', 'ProfilerStub' => __DIR__ . '/includes/profiler/ProfilerStub.php', 'ProfilerXhprof' => __DIR__ . '/includes/profiler/ProfilerXhprof.php', 'Protect' => __DIR__ . '/maintenance/protect.php', diff --git a/includes/profiler/ProfilerSectionOnly.php b/includes/profiler/ProfilerSectionOnly.php new file mode 100755 index 0000000000..1f8d33b192 --- /dev/null +++ b/includes/profiler/ProfilerSectionOnly.php @@ -0,0 +1,104 @@ +sprofiler = new SectionProfiler(); + } + + public function scopedProfileIn( $section ) { + return $this->sprofiler->scopedProfileIn( $section ); + } + + public function close() { + } + + public function getFunctionStats() { + return $this->sprofiler->getFunctionStats(); + } + + public function getOutput() { + return $this->getFunctionReport(); + } + + /** + * Get a report of profiled functions sorted by inclusive wall clock time + * in descending order. + * + * Each line of the report includes this data: + * - Function name + * - Number of times function was called + * - Total wall clock time spent in function in microseconds + * - Minimum wall clock time spent in function in microseconds + * - Average wall clock time spent in function in microseconds + * - Maximum wall clock time spent in function in microseconds + * - Percentage of total wall clock time spent in function + * - Total delta of memory usage from start to end of function in bytes + * + * @return string + */ + protected function getFunctionReport() { + $data = $this->getFunctionStats(); + usort( $data, function( $a, $b ) { + if ( $a['real'] === $b['real'] ) { + return 0; + } + return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending + } ); + + $width = 140; + $nameWidth = $width - 65; + $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d"; + $out = array(); + $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s", + 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem' + ); + foreach ( $data as $stats ) { + $out[] = sprintf( $format, + $stats['name'], + $stats['calls'], + $stats['real'] * 1000, + $stats['min_real'] * 1000, + $stats['real'] / $stats['calls'] * 1000, + $stats['max_real'] * 1000, + $stats['%real'], + $stats['memory'] + ); + } + return implode( "\n", $out ); + } +} diff --git a/includes/profiler/ProfilerXhprof.php b/includes/profiler/ProfilerXhprof.php index 64d2de71be..aea475e692 100644 --- a/includes/profiler/ProfilerXhprof.php +++ b/includes/profiler/ProfilerXhprof.php @@ -34,10 +34,9 @@ * $wgProfiler['output'] = 'udp'; * @endcode * - * Rather than obeying wfProfileIn() and wfProfileOut() calls placed in the - * application code, ProfilerXhprof profiles all functions using the XHProf - * PHP extenstion. For PHP5 users, this extension can be installed via PECL or - * your operating system's package manager. XHProf support is built into HHVM. + * ProfilerXhprof profiles all functions using the XHProf PHP extenstion. + * For PHP5 users, this extension can be installed via PECL or your operating + * system's package manager. XHProf support is built into HHVM. * * To restrict the functions for which profiling data is collected, you can * use either a whitelist ($wgProfiler['include']) or a blacklist -- 2.20.1