MediaWiki-Docker/Configuration recipes/Profiling

From mediawiki.org

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 php-excimer installed. The first time you use this, run the following steps:

you@home:mediawiki$
docker compose exec --user root mediawiki bash

root@docker in /w$
apt-get update;
apt-get install php-excimer;
curl 'https://raw.githubusercontent.com/brendangregg/FlameGraph/810687f180f3c4929b5d965f54817a5218c9d89b/flamegraph.pl' > /usr/local/bin/flamegraph.pl
chmod +x /usr/local/bin/flamegraph.pl;
exit;

you@home:mediawiki$ docker compose restart

And, add once to LocalSettings.php:

// Use ?forceflame=1 to generate a trace log, written to /w/cache/trace.log
if ( extension_loaded( 'excimer' ) && isset( $_GET['forceflame'] ) ) {
	$prof = new ExcimerProfiler();
	$prof->setPeriod( 0.1 / 1000 ); // Every 0.1ms
	$prof->setEventType( EXCIMER_REAL );
	$prof->start();
	register_shutdown_function( function () use ( $prof ) {
		$prof->stop();
		$title = MW_ENTRY_POINT . '.php ' . gmdate( 'Y-m-d H:i:s' ) . ' UTC';
		$pipe = popen( "/usr/local/bin/flamegraph.pl --title=\"${title}\" > /var/www/html/w/docs/flamegraph.svg", 'w' );
		fwrite( $pipe, $prof->getLog()->formatCollapsed() );
	} );
}

After making a request with forceflame=1 set, you should find a flamegraph at http://mw.localhost:8080/w/docs/flamegraph.svg