Added some sanity warnings to TransactionProfiler
[lhc/web/wiklou.git] / includes / profiler / ProfilerSimpleText.php
1 <?php
2 /**
3 * Profiler showing output in page source.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 */
23
24 /**
25 * The least sophisticated profiler output class possible, view your source! :)
26 *
27 * Put the following 2 lines in StartProfiler.php:
28 *
29 * $wgProfiler['class'] = 'ProfilerSimpleText';
30 * $wgProfiler['visible'] = true;
31 *
32 * @ingroup Profiler
33 */
34 class ProfilerSimpleText extends ProfilerStandard {
35 public $visible = false; /* Show as <PRE> or <!-- ? */
36 static private $out;
37
38 public function __construct( $profileConfig ) {
39 if ( isset( $profileConfig['visible'] ) && $profileConfig['visible'] ) {
40 $this->visible = true;
41 }
42 parent::__construct( $profileConfig );
43 }
44
45 protected function collateOnly() {
46 return true;
47 }
48
49 public function logData() {
50 if ( $this->mTemplated ) {
51 $this->close();
52 $totalReal = isset( $this->mCollated['-total'] )
53 ? $this->mCollated['-total']['real']
54 : 0; // profiling mismatch error?
55 uasort( $this->mCollated, array( 'self', 'sort' ) );
56 array_walk( $this->mCollated, array( 'self', 'format' ), $totalReal );
57 if ( PHP_SAPI === 'cli' ) {
58 print "<!--\n" . self::$out . "\n-->\n";
59 } elseif ( $this->getContentType() === 'text/html' ) {
60 if ( $this->visible ) {
61 print '<pre>' . self::$out . '</pre>';
62 } else {
63 print "<!--\n" . self::$out . "\n-->\n";
64 }
65 } elseif ( $this->getContentType() === 'text/javascript' ) {
66 print "\n/*\n" . self::$out . "*/\n";
67 } elseif ( $this->getContentType() === 'text/css' ) {
68 print "\n/*\n" . self::$out . "*/\n";
69 }
70 }
71 }
72
73 static function sort( $a, $b ) {
74 return $a['real'] < $b['real']; /* sort descending by time elapsed */
75 }
76
77 static function format( $item, $key, $totalReal ) {
78 $perc = $totalReal ? $item['real'] / $totalReal * 100 : 0;
79 self::$out .= sprintf( "%6.2f%% %3.6f %6d - %s\n",
80 $perc, $item['real'], $item['count'], $key );
81 }
82 }