2017-12-27 20:44:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-01-16 18:20:31 +00:00
|
|
|
namespace MediaWiki\Tests\Maintenance;
|
|
|
|
|
|
|
|
|
|
use Benchmarker;
|
2018-01-16 23:42:29 +00:00
|
|
|
use MediaWikiCoversValidator;
|
2017-12-27 20:44:38 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Benchmarker
|
|
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
class BenchmarkerTest extends \PHPUnit\Framework\TestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
2018-01-16 23:42:29 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2017-12-27 20:44:38 +00:00
|
|
|
public function testBenchSimple() {
|
|
|
|
|
$bench = $this->getMockBuilder( Benchmarker::class )
|
|
|
|
|
->setMethods( [ 'execute', 'output' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$benchProxy = TestingAccessWrapper::newFromObject( $bench );
|
|
|
|
|
$benchProxy->defaultCount = 3;
|
|
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
$bench->bench( [
|
|
|
|
|
'test' => function () use ( &$count ) {
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 3, $count );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBenchSetup() {
|
|
|
|
|
$bench = $this->getMockBuilder( Benchmarker::class )
|
|
|
|
|
->setMethods( [ 'execute', 'output' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$benchProxy = TestingAccessWrapper::newFromObject( $bench );
|
|
|
|
|
$benchProxy->defaultCount = 2;
|
|
|
|
|
|
|
|
|
|
$buffer = [];
|
|
|
|
|
$bench->bench( [
|
|
|
|
|
'test' => [
|
|
|
|
|
'setup' => function () use ( &$buffer ) {
|
|
|
|
|
$buffer[] = 'setup';
|
|
|
|
|
},
|
|
|
|
|
'function' => function () use ( &$buffer ) {
|
|
|
|
|
$buffer[] = 'run';
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBenchVerbose() {
|
|
|
|
|
$bench = $this->getMockBuilder( Benchmarker::class )
|
|
|
|
|
->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$benchProxy = TestingAccessWrapper::newFromObject( $bench );
|
|
|
|
|
$benchProxy->defaultCount = 1;
|
|
|
|
|
|
|
|
|
|
$bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
|
|
|
|
|
->will( $this->returnValueMap( [
|
|
|
|
|
[ 'verbose', true ],
|
|
|
|
|
[ 'count', false ],
|
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
|
|
$bench->expects( $this->once() )->method( 'verboseRun' )
|
|
|
|
|
->with( 0 )
|
|
|
|
|
->willReturn( null );
|
|
|
|
|
|
|
|
|
|
$bench->bench( [
|
2021-02-06 19:40:52 +00:00
|
|
|
'test' => static function () {
|
2017-12-27 20:44:38 +00:00
|
|
|
}
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function noop() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBenchName_method() {
|
|
|
|
|
$bench = $this->getMockBuilder( Benchmarker::class )
|
|
|
|
|
->setMethods( [ 'execute', 'output', 'addResult' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$benchProxy = TestingAccessWrapper::newFromObject( $bench );
|
|
|
|
|
$benchProxy->defaultCount = 1;
|
|
|
|
|
|
|
|
|
|
$bench->expects( $this->once() )->method( 'addResult' )
|
|
|
|
|
->with( $this->callback( function ( $res ) {
|
2019-11-02 04:05:36 +00:00
|
|
|
return isset( $res['name'] ) && $res['name'] === ( __CLASS__ . '::noop()' );
|
2017-12-27 20:44:38 +00:00
|
|
|
} ) );
|
|
|
|
|
|
|
|
|
|
$bench->bench( [
|
|
|
|
|
[ 'function' => [ $this, 'noop' ] ]
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBenchName_string() {
|
|
|
|
|
$bench = $this->getMockBuilder( Benchmarker::class )
|
|
|
|
|
->setMethods( [ 'execute', 'output', 'addResult' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$benchProxy = TestingAccessWrapper::newFromObject( $bench );
|
|
|
|
|
$benchProxy->defaultCount = 1;
|
|
|
|
|
|
|
|
|
|
$bench->expects( $this->once() )->method( 'addResult' )
|
|
|
|
|
->with( $this->callback( function ( $res ) {
|
2019-11-11 08:04:38 +00:00
|
|
|
return $res['name'] === 'strtolower(A)';
|
2017-12-27 20:44:38 +00:00
|
|
|
} ) );
|
|
|
|
|
|
|
|
|
|
$bench->bench( [ [
|
|
|
|
|
'function' => 'strtolower',
|
|
|
|
|
'args' => [ 'A' ],
|
|
|
|
|
] ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Benchmarker::verboseRun
|
|
|
|
|
*/
|
|
|
|
|
public function testVerboseRun() {
|
|
|
|
|
$bench = $this->getMockBuilder( Benchmarker::class )
|
|
|
|
|
->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$benchProxy = TestingAccessWrapper::newFromObject( $bench );
|
|
|
|
|
$benchProxy->defaultCount = 1;
|
|
|
|
|
|
|
|
|
|
$bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
|
|
|
|
|
->will( $this->returnValueMap( [
|
|
|
|
|
[ 'verbose', true ],
|
|
|
|
|
[ 'count', false ],
|
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
|
|
$bench->expects( $this->once() )->method( 'output' )
|
|
|
|
|
->with( $this->callback( function ( $out ) {
|
|
|
|
|
return preg_match( '/memory.+ peak/', $out ) === 1;
|
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
|
|
$bench->bench( [
|
2021-02-06 19:40:52 +00:00
|
|
|
'test' => static function () {
|
2017-12-27 20:44:38 +00:00
|
|
|
}
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
}
|