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  '@phan-var array $cpuStats';
81  $realStats = $this->realProf->getLog()->aggregateByFunction();
82  '@phan-var array $realStats';
83  $allNames = array_keys( $realStats + $cpuStats );
84  $cpuSamples = $this->cpuProf->getLog()->getEventCount();
85  $realSamples = $this->realProf->getLog()->getEventCount();
86 
87  $resultStats = [ [
88  'name' => '-total',
89  'calls' => 1,
90  'memory' => 0,
91  '%memory' => 0,
92  'min_real' => 0,
93  'max_real' => 0,
94  'cpu' => $cpuSamples * $this->period * 1000,
95  '%cpu' => 100,
96  'real' => $realSamples * $this->period * 1000,
97  '%real' => 100,
98  ] ];
99 
100  foreach ( $allNames as $funcName ) {
101  $cpuEntry = $cpuStats[$funcName] ?? false;
102  $realEntry = $realStats[$funcName] ?? false;
103  $resultEntry = [
104  'name' => $funcName,
105  'calls' => 0,
106  'memory' => 0,
107  '%memory' => 0,
108  'min_real' => 0,
109  'max_real' => 0,
110  ];
111 
112  if ( $cpuEntry ) {
113  $resultEntry['cpu'] = $cpuEntry['inclusive'] * $this->period * 1000;
114  $resultEntry['%cpu'] = $cpuEntry['inclusive'] / $cpuSamples * 100;
115  } else {
116  $resultEntry['cpu'] = 0;
117  $resultEntry['%cpu'] = 0;
118  }
119  if ( $realEntry ) {
120  $resultEntry['real'] = $realEntry['inclusive'] * $this->period * 1000;
121  $resultEntry['%real'] = $realEntry['inclusive'] / $realSamples * 100;
122  } else {
123  $resultEntry['real'] = 0;
124  $resultEntry['%real'] = 0;
125  }
126 
127  $resultStats[] = $resultEntry;
128  }
129  return $resultStats;
130  }
131 
132  public function getOutput() {
133  $this->close();
134  $cpuLog = $this->cpuProf->getLog();
135  $realLog = $this->realProf->getLog();
136  $cpuStats = $cpuLog->aggregateByFunction();
137  '@phan-var array $cpuStats';
138  $realStats = $realLog->aggregateByFunction();
139  '@phan-var array $realStats';
140  $allNames = array_keys( $cpuStats + $realStats );
141  $cpuSamples = $cpuLog->getEventCount();
142  $realSamples = $realLog->getEventCount();
143 
144  $result = '';
145 
146  $titleFormat = "%-70s %10s %11s %10s %11s %10s %11s %10s %11s\n";
147  $statsFormat = "%-70s %10d %10.1f%% %10d %10.1f%% %10d %10.1f%% %10d %10.1f%%\n";
148  $result .= sprintf( $titleFormat,
149  'Name',
150  'CPU incl', 'CPU incl%', 'CPU self', 'CPU self%',
151  'Real incl', 'Real incl%', 'Real self', 'Real self%'
152  );
153 
154  foreach ( $allNames as $funcName ) {
155  $realEntry = $realStats[$funcName] ?? false;
156  $cpuEntry = $cpuStats[$funcName] ?? false;
157  $realIncl = $realEntry ? $realEntry['inclusive'] : 0;
158  $realSelf = $realEntry ? $realEntry['self'] : 0;
159  $cpuIncl = $cpuEntry ? $cpuEntry['inclusive'] : 0;
160  $cpuSelf = $cpuEntry ? $cpuEntry['self'] : 0;
161  $result .= sprintf( $statsFormat,
162  $funcName,
163  $cpuIncl * $this->period * 1000,
164  $cpuIncl == 0 ? 0 : $cpuIncl / $cpuSamples * 100,
165  $cpuSelf * $this->period * 1000,
166  $cpuSelf == 0 ? 0 : $cpuSelf / $cpuSamples * 100,
167  $realIncl * $this->period * 1000,
168  $realIncl == 0 ? 0 : $realIncl / $realSamples * 100,
169  $realSelf * $this->period * 1000,
170  $realSelf == 0 ? 0 : $realSelf / $realSamples * 100
171  );
172  }
173 
174  return $result;
175  }
176 }
close()
Close opened profiling sections.
getOutput()
Returns a profiling output to be stored in debug file.
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
__construct(array $params=[])
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
Profiler base class that defines the interface and some shared functionality.
Definition: Profiler.php:37
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:41