3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWikiCoversValidator
;
7 use Wikimedia\TestingAccessWrapper
;
12 class BenchmarkerTest
extends \PHPUnit\Framework\TestCase
{
14 use MediaWikiCoversValidator
;
16 public function testBenchSimple() {
17 $bench = $this->getMockBuilder( Benchmarker
::class )
18 ->setMethods( [ 'execute', 'output' ] )
20 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
21 $benchProxy->defaultCount
= 3;
25 'test' => function () use ( &$count ) {
30 $this->assertSame( 3, $count );
33 public function testBenchSetup() {
34 $bench = $this->getMockBuilder( Benchmarker
::class )
35 ->setMethods( [ 'execute', 'output' ] )
37 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
38 $benchProxy->defaultCount
= 2;
43 'setup' => function () use ( &$buffer ) {
46 'function' => function () use ( &$buffer ) {
52 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
55 public function testBenchVerbose() {
56 $bench = $this->getMockBuilder( Benchmarker
::class )
57 ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
59 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
60 $benchProxy->defaultCount
= 1;
62 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
63 ->will( $this->returnValueMap( [
68 $bench->expects( $this->once() )->method( 'verboseRun' )
73 'test' => function () {
78 public function noop() {
81 public function testBenchName_method() {
82 $bench = $this->getMockBuilder( Benchmarker
::class )
83 ->setMethods( [ 'execute', 'output', 'addResult' ] )
85 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
86 $benchProxy->defaultCount
= 1;
88 $bench->expects( $this->once() )->method( 'addResult' )
89 ->with( $this->callback( function ( $res ) {
90 return isset( $res['name'] ) && $res['name'] === __CLASS__
. '::noop()';
94 [ 'function' => [ $this, 'noop' ] ]
98 public function testBenchName_string() {
99 $bench = $this->getMockBuilder( Benchmarker
::class )
100 ->setMethods( [ 'execute', 'output', 'addResult' ] )
102 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
103 $benchProxy->defaultCount
= 1;
105 $bench->expects( $this->once() )->method( 'addResult' )
106 ->with( $this->callback( function ( $res ) {
107 return 'strtolower(A)';
111 'function' => 'strtolower',
117 * @covers Benchmarker::verboseRun
119 public function testVerboseRun() {
120 $bench = $this->getMockBuilder( Benchmarker
::class )
121 ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
123 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
124 $benchProxy->defaultCount
= 1;
126 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
127 ->will( $this->returnValueMap( [
132 $bench->expects( $this->once() )->method( 'output' )
133 ->with( $this->callback( function ( $out ) {
134 return preg_match( '/memory.+ peak/', $out ) === 1;
138 'test' => function () {