MediaWiki-Docker/Configuration recipes/Profiling

Plaintext profiler edit

MediaWiki-Docker comes with php-tideways pre-installed, which provides support for profiling web requests and maintenance scripts. This result is the same as in production with WikimediaDebug#Plaintext_request_profile.

Add once to LocalSettings.php:

// Use ?forceprofile=1 to profile the request.
// Output is added to the end of the response (use view-source if the response is HTML).
if ( extension_loaded( 'tideways_xhprof' ) ) {
	if ( isset( $_GET['forceprofile'] ) || PHP_SAPI === 'cli' ) {
		$wgProfiler = [
			'class'  => ProfilerXhprof::class,
			'flags'  => TIDEWAYS_XHPROF_FLAGS_CPU | TIDEWAYS_XHPROF_FLAGS_NO_BUILTINS,
			'output' => 'text',
		];
	}
}

Flame graph edit

MediaWiki-Docker does not yet come with Excimer installed. The first time you use this, run the following steps:

you@home:mediawiki$ docker compose exec --user root mediawiki bash
root@docker:/w$ apt-get update && apt-get install php8.1-excimer
root@docker:/w$ exit
you@home:mediawiki$ docker compose restart

If you don't use PHP 8.1, specify the matching package for your PHP version. For example, apt-get install php8.3-excimer

And, add once to your LocalSettings.php:

// Use ?forceprofile=1 to generate a trace log, written to /w/logs/speedscope.json
if ( extension_loaded( 'excimer' ) && isset( $_GET['forceprofile'] ) ) {
	$excimer = new ExcimerProfiler();
	$excimer->setPeriod( 0.001 ); // 1ms
	$excimer->setEventType( EXCIMER_REAL );
	$excimer->start();
	register_shutdown_function( function () use ( $excimer ) {
		$excimer->stop();
		$data = $excimer->getLog()->getSpeedscopeData();
		$data['profiles'][0]['name'] = $_SERVER['REQUEST_URI'];
		file_put_contents( MW_INSTALL_PATH . '/logs/speedscope-' . gmdate( 'Y-m-d_His' ) . '.json',
				json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) );
	} );
}

After making a request with forceprofile=1 set, you should find a Speedscope JSON file in your MediaWiki directory, ready to browse in Speedscope.

Code steward edit