| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * Profiler that only tracks explicit profiling sections |
| 23 | * |
| 24 | * @ingroup Profiler |
| 25 | * @since 1.25 |
| 26 | * @see $wgProfiler |
| 27 | */ |
| 28 | class ProfilerSectionOnly extends Profiler { |
| 29 | /** @var SectionProfiler */ |
| 30 | protected $sprofiler; |
| 31 | |
| 32 | public function __construct( array $params = [] ) { |
| 33 | parent::__construct( $params ); |
| 34 | $this->sprofiler = new SectionProfiler(); |
| 35 | } |
| 36 | |
| 37 | public function scopedProfileIn( $section ): ?SectionProfileCallback { |
| 38 | return $this->sprofiler->scopedProfileIn( $section ); |
| 39 | } |
| 40 | |
| 41 | public function close() { |
| 42 | } |
| 43 | |
| 44 | public function getFunctionStats() { |
| 45 | return $this->sprofiler->getFunctionStats(); |
| 46 | } |
| 47 | |
| 48 | public function getOutput() { |
| 49 | return $this->getFunctionReport(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get a report of profiled functions sorted by inclusive wall clock time |
| 54 | * in descending order. |
| 55 | * |
| 56 | * Each line of the report includes this data: |
| 57 | * - Function name |
| 58 | * - Number of times function was called |
| 59 | * - Total wall clock time spent in function in microseconds |
| 60 | * - Minimum wall clock time spent in function in microseconds |
| 61 | * - Average wall clock time spent in function in microseconds |
| 62 | * - Maximum wall clock time spent in function in microseconds |
| 63 | * - Percentage of total wall clock time spent in function |
| 64 | * - Total delta of memory usage from start to end of function in bytes |
| 65 | * |
| 66 | * @return string |
| 67 | */ |
| 68 | protected function getFunctionReport() { |
| 69 | $data = $this->getFunctionStats(); |
| 70 | usort( $data, static function ( $a, $b ) { |
| 71 | return $b['real'] <=> $a['real']; // descending |
| 72 | } ); |
| 73 | |
| 74 | $width = 140; |
| 75 | $nameWidth = $width - 65; |
| 76 | $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d"; |
| 77 | $out = []; |
| 78 | $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s", |
| 79 | 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem' |
| 80 | ); |
| 81 | foreach ( $data as $stats ) { |
| 82 | $out[] = sprintf( $format, |
| 83 | $stats['name'], |
| 84 | $stats['calls'], |
| 85 | $stats['real'] * 1000, |
| 86 | $stats['min_real'] * 1000, |
| 87 | $stats['real'] / $stats['calls'] * 1000, |
| 88 | $stats['max_real'] * 1000, |
| 89 | $stats['%real'], |
| 90 | $stats['memory'] |
| 91 | ); |
| 92 | } |
| 93 | return implode( "\n", $out ); |
| 94 | } |
| 95 | } |