MediaWiki  master
ProfilerExcimer.php
Go to the documentation of this file.
1 <?php
26 class ProfilerExcimer extends Profiler {
28  private $cpuProf;
30  private $realProf;
31  private $period;
32 
42  public function __construct( array $params = [] ) {
43  parent::__construct( $params );
44 
45  $this->period = $params['period'] ?? 0.01;
46  $maxDepth = $params['maxDepth'] ?? 100;
47 
48  if ( isset( $params['cpuProfiler'] ) ) {
49  $this->cpuProf = $params['cpuProfiler'];
50  } else {
51  $this->cpuProf = new ExcimerProfiler;
52  $this->cpuProf->setEventType( EXCIMER_CPU );
53  $this->cpuProf->setPeriod( $this->period );
54  $this->cpuProf->setMaxDepth( $maxDepth );
55  $this->cpuProf->start();
56  }
57 
58  if ( isset( $params['realProfiler'] ) ) {
59  $this->realProf = $params['realProfiler'];
60  } else {
61  $this->realProf = new ExcimerProfiler;
62  $this->realProf->setEventType( EXCIMER_REAL );
63  $this->realProf->setPeriod( $this->period );
64  $this->realProf->setMaxDepth( $maxDepth );
65  $this->realProf->start();
66  }
67  }
68 
69  public function scopedProfileIn( $section ) {
70  }
71 
72  public function close() {
73  $this->cpuProf->stop();
74  $this->realProf->stop();
75  }
76 
77  public function getFunctionStats() {
78  $this->close();
79  $cpuStats = $this->cpuProf->getLog()->aggregateByFunction();
80  $realStats = $this->realProf->getLog()->aggregateByFunction();
81  $allNames = array_keys( $realStats + $cpuStats );
82  $cpuSamples = $this->cpuProf->getLog()->getEventCount();
83  $realSamples = $this->realProf->getLog()->getEventCount();
84 
85  $resultStats = [ [
86  'name' => '-total',
87  'calls' => 1,
88  'memory' => 0,
89  '%memory' => 0,
90  'min_real' => 0,
91  'max_real' => 0,
92  'cpu' => $cpuSamples * $this->period * 1000,
93  '%cpu' => 100,
94  'real' => $realSamples * $this->period * 1000,
95  '%real' => 100,
96  ] ];
97 
98  foreach ( $allNames as $funcName ) {
99  $cpuEntry = $cpuStats[$funcName] ?? false;
100  $realEntry = $realStats[$funcName] ?? false;
101  $resultEntry = [
102  'name' => $funcName,
103  'calls' => 0,
104  'memory' => 0,
105  '%memory' => 0,
106  'min_real' => 0,
107  'max_real' => 0,
108  ];
109 
110  if ( $cpuEntry ) {
111  $resultEntry['cpu'] = $cpuEntry['inclusive'] * $this->period * 1000;
112  $resultEntry['%cpu'] = $cpuEntry['inclusive'] / $cpuSamples * 100;
113  } else {
114  $resultEntry['cpu'] = 0;
115  $resultEntry['%cpu'] = 0;
116  }
117  if ( $realEntry ) {
118  $resultEntry['real'] = $realEntry['inclusive'] * $this->period * 1000;
119  $resultEntry['%real'] = $realEntry['inclusive'] / $realSamples * 100;
120  } else {
121  $resultEntry['real'] = 0;
122  $resultEntry['%real'] = 0;
123  }
124 
125  $resultStats[] = $resultEntry;
126  }
127  return $resultStats;
128  }
129 
130  public function getOutput() {
131  $this->close();
132  $cpuLog = $this->cpuProf->getLog();
133  $realLog = $this->realProf->getLog();
134  $cpuStats = $cpuLog->aggregateByFunction();
135  $realStats = $realLog->aggregateByFunction();
136  $allNames = array_keys( $cpuStats + $realStats );
137  $cpuSamples = $cpuLog->getEventCount();
138  $realSamples = $realLog->getEventCount();
139 
140  $result = '';
141 
142  $titleFormat = "%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n";
143  $statsFormat = "%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n";
144  $result .= sprintf( $titleFormat,
145  'Name',
146  'CPU incl', 'CPU incl%', 'CPU self', 'CPU self%',
147  'Real incl', 'Real incl%', 'Real self', 'Real self%'
148  );
149 
150  foreach ( $allNames as $funcName ) {
151  $realEntry = $realStats[$funcName] ?? false;
152  $cpuEntry = $cpuStats[$funcName] ?? false;
153  $realIncl = $realEntry ? $realEntry['inclusive'] : 0;
154  $realSelf = $realEntry ? $realEntry['self'] : 0;
155  $cpuIncl = $cpuEntry ? $cpuEntry['inclusive'] : 0;
156  $cpuSelf = $cpuEntry ? $cpuEntry['self'] : 0;
157  $result .= sprintf( $statsFormat,
158  $funcName,
159  $cpuIncl * $this->period * 1000,
160  $cpuIncl == 0 ? 0 : $cpuIncl / $cpuSamples * 100,
161  $cpuSelf * $this->period * 1000,
162  $cpuSelf == 0 ? 0 : $cpuSelf / $cpuSamples * 100,
163  $realIncl * $this->period * 1000,
164  $realIncl == 0 ? 0 : $realIncl / $realSamples * 100,
165  $realSelf * $this->period * 1000,
166  $realSelf == 0 ? 0 : $realSelf / $realSamples * 100
167  );
168  }
169 
170  return $result;
171  }
172 }
ProfilerExcimer\close
close()
Close opened profiling sections.
Definition: ProfilerExcimer.php:72
ProfilerExcimer\$realProf
ExcimerProfiler $realProf
Definition: ProfilerExcimer.php:30
ProfilerExcimer\$cpuProf
ExcimerProfiler $cpuProf
Definition: ProfilerExcimer.php:28
ProfilerExcimer\$period
$period
Definition: ProfilerExcimer.php:31
Profiler
Profiler base class that defines the interface and some shared functionality.
Definition: Profiler.php:36
ProfilerExcimer\getFunctionStats
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
Definition: ProfilerExcimer.php:77
ProfilerExcimer\__construct
__construct(array $params=[])
Definition: ProfilerExcimer.php:42
ProfilerExcimer
Definition: ProfilerExcimer.php:26
Profiler\$params
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:40
ProfilerExcimer\scopedProfileIn
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
Definition: ProfilerExcimer.php:69
ProfilerExcimer\getOutput
getOutput()
Returns a profiling output to be stored in debug file.
Definition: ProfilerExcimer.php:130