maintenance: Remove LanguageFactory dependency from Benchmarker.php

By not pulling in the entire Language service tree, benchmarks become
standalone CLI scripts that work even when MW is not installed.

This is useful for benchmarks specifically, as those are generally
better run outside VMs one may have for developing MW, so all one has
to do is move or comment-out LocalSettings.php and then run the
benchmark in question.

Change-Id: Icfc0a818d307bce805da45c8304cfd6d3b810768
This commit is contained in:
Timo Tijhof 2021-07-31 23:09:45 +01:00 committed by DannyS712
parent 4705442f68
commit 6153a981b6

View file

@ -26,7 +26,6 @@
* @ingroup Benchmark
*/
use MediaWiki\MediaWikiServices;
use Wikimedia\RunningStat;
// @codeCoverageIgnoreStart
@ -49,8 +48,6 @@ abstract class Benchmarker extends Maintenance {
}
public function bench( array $benchs ) {
$this->lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' );
$this->startBench();
$count = $this->getOption( 'count', $this->defaultCount );
$verbose = $this->hasOption( 'verbose' );
@ -160,7 +157,7 @@ abstract class Benchmarker extends Maintenance {
] as $key => $label ) {
$ret .= sprintf( "%' 20s: %s\n",
$label,
$this->lang->formatSize( $res['usage'][$key] )
$this->formatSize( $res['usage'][$key] )
);
}
@ -170,11 +167,34 @@ abstract class Benchmarker extends Maintenance {
protected function verboseRun( $iteration ) {
$this->output( sprintf( "#%3d - memory: %-10s - peak: %-10s\n",
$iteration,
$this->lang->formatSize( memory_get_usage( true ) ),
$this->lang->formatSize( memory_get_peak_usage( true ) )
$this->formatSize( memory_get_usage( true ) ),
$this->formatSize( memory_get_peak_usage( true ) )
) );
}
/**
* Format an amount of bytes into short human-readable string.
*
* This is simplified version of Language::formatSize() to avoid pulling
* all the general MediaWiki services, which can significantly influence
* measured memory use.
*
* @param int|float $bytes
* @return string Formatted in using IEC bytes (multiples of 1024)
*/
private function formatSize( $bytes ): string {
if ( $bytes >= ( 1024 ** 3 ) ) {
return number_format( $bytes / ( 1024 ** 3 ), 2 ) . ' GiB';
}
if ( $bytes >= ( 1024 ** 2 ) ) {
return number_format( $bytes / ( 1024 ** 2 ), 2 ) . ' MiB';
}
if ( $bytes >= 1024 ) {
return number_format( $bytes / 1024, 1 ) . ' KiB';
}
return $bytes . ' B';
}
/**
* @since 1.32
* @param string $file Path to file (maybe compressed with gzip)