From f6d65e533c62f6deb21342d4901ece24497b433e Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Thu, 4 Jun 2015 07:31:04 +0200 Subject: Update to MediaWiki 1.25.1 --- includes/profiler/ProfilerXhprof.php | 194 +++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 includes/profiler/ProfilerXhprof.php (limited to 'includes/profiler/ProfilerXhprof.php') diff --git a/includes/profiler/ProfilerXhprof.php b/includes/profiler/ProfilerXhprof.php new file mode 100644 index 00000000..f36cdc1a --- /dev/null +++ b/includes/profiler/ProfilerXhprof.php @@ -0,0 +1,194 @@ + + * @copyright © 2014 Bryan Davis and Wikimedia Foundation. + * @ingroup Profiler + * @see Xhprof + * @see https://php.net/xhprof + * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md + */ +class ProfilerXhprof extends Profiler { + /** + * @var Xhprof $xhprof + */ + protected $xhprof; + + /** + * Profiler for explicit, arbitrary, frame labels + * @var SectionProfiler + */ + protected $sprofiler; + + /** + * @param array $params + * @see Xhprof::__construct() + */ + public function __construct( array $params = array() ) { + parent::__construct( $params ); + $this->xhprof = new Xhprof( $params ); + $this->sprofiler = new SectionProfiler(); + } + + public function scopedProfileIn( $section ) { + return $this->sprofiler->scopedProfileIn( $section ); + } + + /** + * No-op for xhprof profiling. + */ + public function close() { + } + + public function getFunctionStats() { + $metrics = $this->xhprof->getCompleteMetrics(); + $profile = array(); + + $main = null; // units in ms + foreach ( $metrics as $fname => $stats ) { + // Convert elapsed times from μs to ms to match interface + $entry = array( + 'name' => $fname, + 'calls' => $stats['ct'], + 'real' => $stats['wt']['total'] / 1000, + '%real' => $stats['wt']['percent'], + 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0, + '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0, + 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0, + '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0, + 'min_real' => $stats['wt']['min'] / 1000, + 'max_real' => $stats['wt']['max'] / 1000 + ); + $profile[] = $entry; + if ( $fname === 'main()' ) { + $main = $entry; + } + } + + // Merge in all of the custom profile sections + foreach ( $this->sprofiler->getFunctionStats() as $stats ) { + if ( $stats['name'] === '-total' ) { + // Discard section profiler running totals + continue; + } + + // @note: getFunctionStats() values already in ms + $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0; + $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0; + $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0; + $profile[] = $stats; // assume no section names collide with $metrics + } + + return $profile; + } + + /** + * Returns a profiling output to be stored in debug file + * + * @return string + */ + 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 ); + } + + /** + * Retrieve raw data from xhprof + * @return array + */ + public function getRawData() { + return $this->xhprof->getRawData(); + } +} -- cgit v1.2.3-54-g00ecf