MediaWiki  master
ProfilerOutputText.php
Go to the documentation of this file.
1 <?php
31  private $thresholdMs;
32 
34  private $visible;
35 
36  public function __construct( Profiler $collector, array $params ) {
37  parent::__construct( $collector, $params );
38  $this->thresholdMs = $params['thresholdMs'] ?? 1.0;
39  $this->visible = $params['visible'] ?? false;
40  }
41 
42  public function logsToOutput() {
43  return true;
44  }
45 
46  public function log( array $stats ) {
47  $out = '';
48 
49  // Filter out really tiny entries
50  $min = $this->thresholdMs;
51  $stats = array_filter( $stats, function ( $a ) use ( $min ) {
52  return $a['real'] > $min;
53  } );
54  // Sort descending by time elapsed
55  usort( $stats, function ( $a, $b ) {
56  return $b['real'] <=> $a['real'];
57  } );
58 
59  array_walk( $stats,
60  function ( $item ) use ( &$out ) {
61  $out .= sprintf( "%6.2f%% %3.3f %6d - %s\n",
62  $item['%real'], $item['real'], $item['calls'], $item['name'] );
63  }
64  );
65 
66  $contentType = $this->collector->getContentType();
67  if ( wfIsCLI() ) {
68  print "<!--\n{$out}\n-->\n";
69  } elseif ( $contentType === 'text/html' ) {
70  if ( $this->visible ) {
71  print "<pre>{$out}</pre>";
72  } else {
73  print "<!--\n{$out}\n-->\n";
74  }
75  } elseif ( $contentType === 'text/javascript' || $contentType === 'text/css' ) {
76  print "\n/*\n{$out}*/\n";
77  }
78  }
79 }
ProfilerOutputText\$thresholdMs
float $thresholdMs
Min real time display threshold.
Definition: ProfilerOutputText.php:31
ProfilerOutputText
Adds profiler output to the HTTP response.
Definition: ProfilerOutputText.php:29
ProfilerOutput\$collector
Profiler $collector
Definition: ProfilerOutput.php:29
ProfilerOutputText\__construct
__construct(Profiler $collector, array $params)
Definition: ProfilerOutputText.php:36
ProfilerOutput\$params
array $params
Configuration of $wgProfiler.
Definition: ProfilerOutput.php:31
ProfilerOutputText\log
log(array $stats)
Log MediaWiki-style profiling data.
Definition: ProfilerOutputText.php:46
Profiler
Profiler base class that defines the interface and some shared functionality.
Definition: Profiler.php:33
ProfilerOutputText\logsToOutput
logsToOutput()
May the log() try to write to standard output?
Definition: ProfilerOutputText.php:42
wfIsCLI
wfIsCLI()
Check if we are running from the commandline.
Definition: GlobalFunctions.php:1858
print
print
Definition: opensearch_desc.php:53
ProfilerOutput
Base class for profiling output.
Definition: ProfilerOutput.php:27
ProfilerOutputText\$visible
bool $visible
Whether to use visible text or a comment (for HTML responses)
Definition: ProfilerOutputText.php:34