`$a <=> $b` returns `-1` if `$a` is lesser, `1` if `$b` is lesser, and `0` if they are equal, which are exactly the values 'sort()' callbacks are supposed to return. It also enables the neat idiom `$a[x] <=> $b[x] ?: $a[y] <=> $b[y]` to sort arrays of objects first by 'x', and by 'y' if they are equal. * Replace a common pattern like `return $a < $b ? -1 : 1` with the new operator (and similar patterns with the variables, the numbers or the comparison inverted). Some of the uses were previously not correctly handling the variables being equal; this is now automatically fixed. * Also replace `return $a - $b`, which is equivalent to `return $a <=> $b` if both variables are integers but less intuitive. * (Do not replace `return strcmp( $a, $b )`. It is also equivalent when both variables are strings, but if any of the variables is not, 'strcmp()' converts it to a string before comparison, which could give different results than '<=>', so changing this would require careful review and isn't worth it.) * Also replace `return $a > $b`, which presumably sort of works most of the time (returns `1` if `$b` is lesser, and `0` if they are equal or `$a` is lesser) but is erroneous. Change-Id: I19a3d2fc8fcdb208c10330bd7a42c4e05d7f5cf3
100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
/**
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
*
|
|
* @file
|
|
*/
|
|
|
|
/**
|
|
* Profiler that only tracks explicit profiling sections
|
|
*
|
|
* @code
|
|
* $wgProfiler['class'] = ProfilerSectionOnly::class;
|
|
* $wgProfiler['output'] = 'text';
|
|
* $wgProfiler['visible'] = true;
|
|
* @endcode
|
|
*
|
|
* @ingroup Profiler
|
|
* @since 1.25
|
|
*/
|
|
class ProfilerSectionOnly extends Profiler {
|
|
/** @var SectionProfiler */
|
|
protected $sprofiler;
|
|
|
|
public function __construct( array $params = [] ) {
|
|
parent::__construct( $params );
|
|
$this->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 ) {
|
|
return $b['real'] <=> $a['real']; // descending
|
|
} );
|
|
|
|
$width = 140;
|
|
$nameWidth = $width - 65;
|
|
$format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
|
|
$out = [];
|
|
$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 );
|
|
}
|
|
}
|