| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Logger\LoggerFactory; |
| 22 |
use MediaWiki\WikiMap\WikiMap; |
| 23 |
use Psr\Log\LoggerInterface; |
| 24 |
use Wikimedia\Rdbms\TransactionProfiler; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
abstract class Profiler { |
| 37 |
|
| 38 |
protected $profileID = false; |
| 39 |
|
| 40 |
protected $params = []; |
| 41 |
|
| 42 |
protected $trxProfiler; |
| 43 |
|
| 44 |
protected $logger; |
| 45 |
|
| 46 |
private $allowOutput = false; |
| 47 |
|
| 48 |
|
| 49 |
private static $instance = null; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function __construct( array $params ) { |
| 55 |
if ( isset( $params['profileID'] ) ) { |
| 56 |
$this->profileID = $params['profileID']; |
| 57 |
} |
| 58 |
$this->params = $params; |
| 59 |
$this->trxProfiler = new TransactionProfiler(); |
| 60 |
$this->logger = LoggerFactory::getInstance( 'profiler' ); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
final public static function init( array $profilerConf ): void { |
| 68 |
$params = $profilerConf + [ |
| 69 |
'class' => ProfilerStub::class, |
| 70 |
'sampling' => 1, |
| 71 |
'threshold' => 0.0, |
| 72 |
'output' => [], |
| 73 |
'cliEnable' => false, |
| 74 |
]; |
| 75 |
|
| 76 |
|
| 77 |
$isCLI = ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ); |
| 78 |
$inSample = $params['sampling'] === 1 || mt_rand( 1, $params['sampling'] ) === 1; |
| 79 |
if ( |
| 80 |
!$inSample || |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
( $isCLI && !$params['cliEnable'] ) |
| 90 |
) { |
| 91 |
$params['class'] = ProfilerStub::class; |
| 92 |
} |
| 93 |
|
| 94 |
if ( !is_array( $params['output'] ) ) { |
| 95 |
$params['output'] = [ $params['output'] ]; |
| 96 |
} |
| 97 |
|
| 98 |
self::$instance = new $params['class']( $params ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
final public static function instance() { |
| 105 |
if ( !self::$instance ) { |
| 106 |
trigger_error( 'Called Profiler::instance before settings are loaded', E_USER_WARNING ); |
| 107 |
self::init( [] ); |
| 108 |
} |
| 109 |
|
| 110 |
return self::$instance; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function setProfileID( $id ) { |
| 118 |
wfDeprecated( __METHOD__, '1.41' ); |
| 119 |
$this->profileID = $id; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function getProfileID() { |
| 126 |
if ( $this->profileID === false ) { |
| 127 |
return WikiMap::getCurrentWikiDbDomain()->getId(); |
| 128 |
} else { |
| 129 |
return $this->profileID; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
abstract public function scopedProfileIn( $section ): ?SectionProfileCallback; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function scopedProfileOut( ?SectionProfileCallback &$section = null ) { |
| 146 |
$section = null; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function getTransactionProfiler() { |
| 154 |
return $this->trxProfiler; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
abstract public function close(); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private function getOutputs() { |
| 169 |
$outputs = []; |
| 170 |
foreach ( $this->params['output'] as $outputType ) { |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
$outputClass = strpos( $outputType, 'ProfilerOutput' ) === false |
| 175 |
? 'ProfilerOutput' . ucfirst( $outputType ) |
| 176 |
: $outputType; |
| 177 |
if ( !class_exists( $outputClass ) ) { |
| 178 |
throw new UnexpectedValueException( "'$outputType' is an invalid output type" ); |
| 179 |
} |
| 180 |
$outputInstance = new $outputClass( $this, $this->params ); |
| 181 |
if ( $outputInstance->canUse() ) { |
| 182 |
$outputs[] = $outputInstance; |
| 183 |
} |
| 184 |
} |
| 185 |
return $outputs; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function logData() { |
| 197 |
if ( $this->params['threshold'] > 0.0 ) { |
| 198 |
|
| 199 |
$timeElapsed = microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT']; |
| 200 |
if ( $timeElapsed <= $this->params['threshold'] ) { |
| 201 |
return; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
$outputs = []; |
| 206 |
foreach ( $this->getOutputs() as $output ) { |
| 207 |
if ( !$output->logsToOutput() ) { |
| 208 |
$outputs[] = $output; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if ( $outputs ) { |
| 213 |
$stats = $this->getFunctionStats(); |
| 214 |
foreach ( $outputs as $output ) { |
| 215 |
$output->log( $stats ); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function logDataPageOutputOnly() { |
| 226 |
if ( !$this->allowOutput ) { |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
$outputs = []; |
| 231 |
foreach ( $this->getOutputs() as $output ) { |
| 232 |
if ( $output->logsToOutput() ) { |
| 233 |
$outputs[] = $output; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if ( $outputs ) { |
| 238 |
$stats = $this->getFunctionStats(); |
| 239 |
foreach ( $outputs as $output ) { |
| 240 |
$output->log( $stats ); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function getContentType() { |
| 255 |
if ( $this->allowOutput ) { |
| 256 |
foreach ( headers_list() as $header ) { |
| 257 |
if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) { |
| 258 |
return $m[1]; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
return null; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function setAllowOutput() { |
| 271 |
$this->allowOutput = true; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public function getAllowOutput() { |
| 283 |
wfDeprecated( __METHOD__, '1.41' ); |
| 284 |
return $this->allowOutput; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
abstract public function getFunctionStats(); |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
abstract public function getOutput(); |
| 321 |
} |