Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
SectionProfiler.php
Go to the documentation of this file.
1<?php
21useMediaWiki\Logger\LoggerFactory;
22use Psr\Log\LoggerInterface;
23use Wikimedia\ScopedCallback;
24
35classSectionProfiler {
37protected$start;
39protected$end;
41protected$stack = [];
43protected$workStack = [];
45protected$collated = [];
47protected$collateDone =false;
48
50protected$errorEntry;
52protected$logger;
53
57publicfunction__construct( array $params = [] ) {
58 $this->errorEntry = $this->getErrorEntry();
59 $this->logger = LoggerFactory::getInstance('profiler' );
60 }
61
66publicfunctionscopedProfileIn( $section ) {
67 $this->profileInInternal( $section );
68
69returnnewSectionProfileCallback( $this, $section );
70 }
71
72publicfunctionscopedProfileOut( ScopedCallback &$section ) {
73 $section =null;
74 }
75
97publicfunctiongetFunctionStats() {
98 $this->collateData();
99
100if ( is_array( $this->start ) && is_array( $this->end ) ) {
101 $totalCpu = max( $this->end['cpu'] - $this->start['cpu'], 0 );
102 $totalReal = max( $this->end['real'] - $this->start['real'], 0 );
103 $totalMem = max( $this->end['memory'] - $this->start['memory'], 0 );
104 }else {
105 $totalCpu = 0;
106 $totalReal = 0;
107 $totalMem = 0;
108 }
109
110 $profile = [];
111foreach ( $this->collated as $fname => $data ) {
112 $profile[] = [
113'name' => $fname,
114'calls' => $data['count'],
115'real' => $data['real'] * 1000,
116'%real' => $totalReal ? 100 * $data['real'] / $totalReal : 0,
117'cpu' => $data['cpu'] * 1000,
118'%cpu' => $totalCpu ? 100 * $data['cpu'] / $totalCpu : 0,
119'memory' => $data['memory'],
120'%memory' => $totalMem ? 100 * $data['memory'] / $totalMem : 0,
121'min_real' => 1000 * $data['min_real'],
122'max_real' => 1000 * $data['max_real']
123 ];
124 }
125
126 $profile[] = [
127'name' =>'-total',
128'calls' => 1,
129'real' => 1000 * $totalReal,
130'%real' => 100,
131'cpu' => 1000 * $totalCpu,
132'%cpu' => 100,
133'memory' => $totalMem,
134'%memory' => 100,
135'min_real' => 1000 * $totalReal,
136'max_real' => 1000 * $totalReal
137 ];
138
139return $profile;
140 }
141
145publicfunctionreset() {
146 $this->start =null;
147 $this->end =null;
148 $this->stack = [];
149 $this->workStack = [];
150 $this->collated = [];
151 $this->collateDone =false;
152 }
153
157protectedfunctiongetZeroEntry() {
158return [
159'cpu' => 0.0,
160'real' => 0.0,
161'memory' => 0,
162'count' => 0,
163'min_real' => 0.0,
164'max_real' => 0.0
165 ];
166 }
167
171protectedfunctiongetErrorEntry() {
172 $entry = $this->getZeroEntry();
173 $entry['count'] = 1;
174return $entry;
175 }
176
185protectedfunctionupdateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
186 $entry =& $this->collated[$name];
187if ( !is_array( $entry ) ) {
188 $entry = $this->getZeroEntry();
189 $this->collated[$name] =& $entry;
190 }
191 $entry['cpu'] += $elapsedCpu;
192 $entry['real'] += $elapsedReal;
193 $entry['memory'] += $memChange > 0 ? $memChange : 0;
194 $entry['count']++;
195 $entry['min_real'] = min( $entry['min_real'], $elapsedReal );
196 $entry['max_real'] = max( $entry['max_real'], $elapsedReal );
197 }
198
204publicfunctionprofileInInternal( $functionname ) {
205// Once the data is collated for reports, any future calls
206// should clear the collation cache so the next report will
207// reflect them. This matters when trace mode is used.
208 $this->collateDone =false;
209
210 $cpu = $this->getTime('cpu' );
211 $real = $this->getTime('wall' );
212 $memory = memory_get_usage();
213
214if ( $this->start ===null ) {
215 $this->start = ['cpu' => $cpu,'real' => $real,'memory' => $memory ];
216 }
217
218 $this->workStack[] = [
219 $functionname,
220 count( $this->workStack ),
221 $real,
222 $cpu,
223 $memory
224 ];
225 }
226
232publicfunctionprofileOutInternal( $functionname ) {
233 $item = array_pop( $this->workStack );
234if ( $item ===null ) {
235 $this->logger->error("Profiling error: $functionname" );
236return;
237 }
238 [ $ofname,/* $ocount */, $ortime, $octime, $omem ] = $item;
239
240if ( $functionname ==='close' ) {
241 $message ="Profile section ended by close(): {$ofname}";
242 $this->logger->error( $message );
243 $this->collated[$message] = $this->errorEntry;
244 $functionname = $ofname;
245 } elseif ( $ofname !== $functionname ) {
246 $message ="Profiling error: in({$ofname}), out($functionname)";
247 $this->logger->error( $message );
248 $this->collated[$message] = $this->errorEntry;
249 }
250
251 $realTime = $this->getTime('wall' );
252 $cpuTime = $this->getTime('cpu' );
253 $memUsage = memory_get_usage();
254
255 $elapsedcpu = $cpuTime - $octime;
256 $elapsedreal = $realTime - $ortime;
257 $memchange = $memUsage - $omem;
258 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
259
260 $this->end = [
261'cpu' => $cpuTime,
262'real' => $realTime,
263'memory' => $memUsage
264 ];
265 }
266
270protectedfunctioncollateData() {
271if ( $this->collateDone ) {
272return;
273 }
274 $this->collateDone =true;
275// Close opened profiling sections
276while ( count( $this->workStack ) ) {
277 $this->profileOutInternal('close' );
278 }
279 }
280
291protectedfunctiongetTime( $metric ='wall' ) {
292if ( $metric ==='cpu' || $metric ==='user' ) {
293 $ru = getrusage( 0/* RUSAGE_SELF */ );
294 $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
295if ( $metric ==='cpu' ) {
296 # This is the time of system calls, added to the user time
297 # it gives the total CPU time
298 $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
299 }
300return $time;
301 }else {
302return microtime(true );
303 }
304 }
305}
MediaWiki\Logger\LoggerFactory
Create PSR-3 logger objects.
DefinitionLoggerFactory.php:46
SectionProfileCallback
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
DefinitionSectionProfileCallback.php:28
SectionProfiler
Arbitrary section name based PHP profiling.
DefinitionSectionProfiler.php:35
SectionProfiler\$collateDone
bool $collateDone
DefinitionSectionProfiler.php:47
SectionProfiler\$collated
array[] $collated
Map of (function name => aggregate data array)
DefinitionSectionProfiler.php:45
SectionProfiler\getZeroEntry
getZeroEntry()
DefinitionSectionProfiler.php:157
SectionProfiler\profileInInternal
profileInInternal( $functionname)
This method should not be called outside SectionProfiler.
DefinitionSectionProfiler.php:204
SectionProfiler\$logger
LoggerInterface $logger
DefinitionSectionProfiler.php:52
SectionProfiler\$errorEntry
array $errorEntry
Cache of a standard broken collation entry.
DefinitionSectionProfiler.php:50
SectionProfiler\scopedProfileOut
scopedProfileOut(ScopedCallback &$section)
DefinitionSectionProfiler.php:72
SectionProfiler\getTime
getTime( $metric='wall')
Get the initial time of the request, based on getrusage()
DefinitionSectionProfiler.php:291
SectionProfiler\$start
array null $start
Map of (mem,real,cpu)
DefinitionSectionProfiler.php:37
SectionProfiler\__construct
__construct(array $params=[])
DefinitionSectionProfiler.php:57
SectionProfiler\$workStack
array $workStack
Queue of open profile calls with start data.
DefinitionSectionProfiler.php:43
SectionProfiler\getFunctionStats
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
DefinitionSectionProfiler.php:97
SectionProfiler\$end
array null $end
Map of (mem,real,cpu)
DefinitionSectionProfiler.php:39
SectionProfiler\scopedProfileIn
scopedProfileIn( $section)
DefinitionSectionProfiler.php:66
SectionProfiler\getErrorEntry
getErrorEntry()
DefinitionSectionProfiler.php:171
SectionProfiler\profileOutInternal
profileOutInternal( $functionname)
This method should not be called outside SectionProfiler.
DefinitionSectionProfiler.php:232
SectionProfiler\$stack
array[] $stack
List of resolved profile calls with start/end data.
DefinitionSectionProfiler.php:41
SectionProfiler\reset
reset()
Clear all of the profiling data for another run.
DefinitionSectionProfiler.php:145
SectionProfiler\updateEntry
updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange)
Update the collation entry for a given method name.
DefinitionSectionProfiler.php:185
SectionProfiler\collateData
collateData()
Populate collated data.
DefinitionSectionProfiler.php:270

[8]ページ先頭

©2009-2025 Movatter.jp