10use Psr\Log\LoggerInterface;
46 $this->logger = LoggerFactory::getInstance(
'profiler' );
87 if ( is_array( $this->start ) && is_array( $this->end ) ) {
88 $totalCpu = max( $this->end[
'cpu'] - $this->start[
'cpu'], 0 );
89 $totalReal = max( $this->end[
'real'] - $this->start[
'real'], 0 );
90 $totalMem = max( $this->end[
'memory'] - $this->start[
'memory'], 0 );
98 foreach ( $this->collated as $fname => $data ) {
101 'calls' => $data[
'count'],
102 'real' => $data[
'real'] * 1000,
103 '%real' => $totalReal ? 100 * $data[
'real'] / $totalReal : 0,
104 'cpu' => $data[
'cpu'] * 1000,
105 '%cpu' => $totalCpu ? 100 * $data[
'cpu'] / $totalCpu : 0,
106 'memory' => $data[
'memory'],
107 '%memory' => $totalMem ? 100 * $data[
'memory'] / $totalMem : 0,
108 'min_real' => 1000 * $data[
'min_real'],
109 'max_real' => 1000 * $data[
'max_real']
116 'real' => 1000 * $totalReal,
118 'cpu' => 1000 * $totalCpu,
120 'memory' => $totalMem,
122 'min_real' => 1000 * $totalReal,
123 'max_real' => 1000 * $totalReal
136 $this->workStack = [];
137 $this->collated = [];
138 $this->collateDone =
false;
159 $entry = $this->getZeroEntry();
172 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
173 $entry =& $this->collated[$name];
174 if ( !is_array( $entry ) ) {
175 $entry = $this->getZeroEntry();
176 $this->collated[$name] =& $entry;
178 $entry[
'cpu'] += $elapsedCpu;
179 $entry[
'real'] += $elapsedReal;
180 $entry[
'memory'] += $memChange > 0 ? $memChange : 0;
182 $entry[
'min_real'] = min( $entry[
'min_real'], $elapsedReal );
183 $entry[
'max_real'] = max( $entry[
'max_real'], $elapsedReal );
195 $this->collateDone =
false;
197 $cpu = $this->getTime(
'cpu' );
198 $real = $this->getTime(
'wall' );
199 $memory = memory_get_usage();
201 if ( $this->start ===
null ) {
202 $this->start = [
'cpu' => $cpu,
'real' => $real,
'memory' => $memory ];
205 $this->workStack[] = [
207 count( $this->workStack ),
220 $item = array_pop( $this->workStack );
221 if ( $item ===
null ) {
222 $this->logger->error(
"Profiling error: $functionname" );
225 [ $ofname, , $ortime, $octime, $omem ] = $item;
227 if ( $functionname ===
'close' ) {
228 $message =
"Profile section ended by close(): {$ofname}";
229 $this->logger->error( $message );
230 $this->collated[$message] = $this->errorEntry;
231 $functionname = $ofname;
232 } elseif ( $ofname !== $functionname ) {
233 $message =
"Profiling error: in({$ofname}), out($functionname)";
234 $this->logger->error( $message );
235 $this->collated[$message] = $this->errorEntry;
238 $realTime = $this->getTime(
'wall' );
239 $cpuTime = $this->getTime(
'cpu' );
240 $memUsage = memory_get_usage();
242 $elapsedcpu = $cpuTime - $octime;
243 $elapsedreal = $realTime - $ortime;
244 $memchange = $memUsage - $omem;
245 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
250 'memory' => $memUsage
258 if ( $this->collateDone ) {
261 $this->collateDone =
true;
263 while ( count( $this->workStack ) ) {
264 $this->profileOutInternal(
'close' );
278 protected function getTime( $metric =
'wall' ) {
279 if ( $metric ===
'cpu' || $metric ===
'user' ) {
280 $ru = getrusage( 0 );
281 $time = $ru[
'ru_utime.tv_sec'] + $ru[
'ru_utime.tv_usec'] / 1e6;
282 if ( $metric ===
'cpu' ) {
283 # This is the time of system calls, added to the user time
284 # it gives the total CPU time
285 $time += $ru[
'ru_stime.tv_sec'] + $ru[
'ru_stime.tv_usec'] / 1e6;
289 return microtime(
true );
295class_alias( SectionProfiler::class,
'SectionProfiler' );