Merge "Use makeLink instead of makeKnownLink on Special:WhatLinksHere"
[lhc/web/wiklou.git] / includes / profiler / ProfilerSectionOnly.php
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 * @code
25 * $wgProfiler['class'] = ProfilerSectionOnly::class;
26 * $wgProfiler['output'] = 'text';
27 * $wgProfiler['visible'] = true;
28 * @endcode
29 *
30 * @ingroup Profiler
31 * @since 1.25
32 */
33 class ProfilerSectionOnly extends Profiler {
34 /** @var SectionProfiler */
35 protected $sprofiler;
36
37 public function __construct( array $params = [] ) {
38 parent::__construct( $params );
39 $this->sprofiler = new SectionProfiler();
40 }
41
42 public function scopedProfileIn( $section ) {
43 return $this->sprofiler->scopedProfileIn( $section );
44 }
45
46 public function close() {
47 }
48
49 public function getFunctionStats() {
50 return $this->sprofiler->getFunctionStats();
51 }
52
53 public function getOutput() {
54 return $this->getFunctionReport();
55 }
56
57 /**
58 * Get a report of profiled functions sorted by inclusive wall clock time
59 * in descending order.
60 *
61 * Each line of the report includes this data:
62 * - Function name
63 * - Number of times function was called
64 * - Total wall clock time spent in function in microseconds
65 * - Minimum wall clock time spent in function in microseconds
66 * - Average wall clock time spent in function in microseconds
67 * - Maximum wall clock time spent in function in microseconds
68 * - Percentage of total wall clock time spent in function
69 * - Total delta of memory usage from start to end of function in bytes
70 *
71 * @return string
72 */
73 protected function getFunctionReport() {
74 $data = $this->getFunctionStats();
75 usort( $data, function ( $a, $b ) {
76 return $b['real'] <=> $a['real']; // descending
77 } );
78
79 $width = 140;
80 $nameWidth = $width - 65;
81 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
82 $out = [];
83 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
84 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
85 );
86 foreach ( $data as $stats ) {
87 $out[] = sprintf( $format,
88 $stats['name'],
89 $stats['calls'],
90 $stats['real'] * 1000,
91 $stats['min_real'] * 1000,
92 $stats['real'] / $stats['calls'] * 1000,
93 $stats['max_real'] * 1000,
94 $stats['%real'],
95 $stats['memory']
96 );
97 }
98 return implode( "\n", $out );
99 }
100 }