3 namespace MediaWiki\Tests\Maintenance\Includes
;
5 use MediaWiki\Maintenance\Benchmarker
;
6 use MediaWikiCoversValidator
;
7 use PHPUnit\Framework\TestCase
;
8 use Wikimedia\TestingAccessWrapper
;
11 * @covers \MediaWiki\Maintenance\Benchmarker
13 class BenchmarkerTest
extends TestCase
{
15 use MediaWikiCoversValidator
;
17 public function testBenchSimple() {
18 $bench = $this->getMockBuilder( Benchmarker
::class )
19 ->onlyMethods( [ 'execute', 'output' ] )
21 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
22 $benchProxy->defaultCount
= 3;
26 'test' => static function () use ( &$count ) {
31 $this->assertSame( 3, $count );
34 public function testBenchSetup() {
35 $bench = $this->getMockBuilder( Benchmarker
::class )
36 ->onlyMethods( [ 'execute', 'output' ] )
38 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
39 $benchProxy->defaultCount
= 2;
44 'setup' => static function () use ( &$buffer ) {
47 'function' => static function () use ( &$buffer ) {
53 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
56 public function testBenchVerbose() {
57 $bench = $this->getMockBuilder( Benchmarker
::class )
58 ->onlyMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
60 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
61 $benchProxy->defaultCount
= 1;
63 $bench->expects( $this->once() )->method( 'hasOption' )
68 $bench->expects( $this->once() )->method( 'verboseRun' )
73 'test' => static function () {
78 public function noop() {
81 public function testBenchName_method() {
82 $bench = $this->getMockBuilder( Benchmarker
::class )
83 ->onlyMethods( [ 'execute', 'output', 'addResult' ] )
85 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
86 $benchProxy->defaultCount
= 1;
88 $bench->expects( $this->once() )->method( 'addResult' )
89 ->with( $this->callback( static 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 ->onlyMethods( [ 'execute', 'output', 'addResult' ] )
102 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
103 $benchProxy->defaultCount
= 1;
105 $bench->expects( $this->once() )->method( 'addResult' )
106 ->with( $this->callback( static function ( $res ) {
107 return $res['name'] === "strtolower('A')";
111 'function' => 'strtolower',
116 public function testVerboseRun() {
117 $bench = $this->getMockBuilder( Benchmarker
::class )
118 ->onlyMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
120 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
121 $benchProxy->defaultCount
= 1;
123 $bench->expects( $this->once() )->method( 'hasOption' )
128 $bench->expects( $this->once() )->method( 'output' )
129 ->with( $this->callback( static function ( $out ) {
130 return preg_match( '/memory.+ peak/', $out ) === 1;
134 'test' => static function () {
139 public function testNaming() {
140 $bench = $this->getMockBuilder( Benchmarker
::class )
141 ->onlyMethods( [ 'execute', 'output', 'startBench' ] )
143 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
144 $benchProxy->defaultCount
= 1;
147 $bench->expects( $this->any() )->method( 'output' )
148 ->willReturnCallback( static function ( $str ) use ( &$out ) {
155 'function' => 'in_array',
156 'args' => [ 'A', [ 'X', 'Y' ] ],
159 'function' => 'in_array',
160 'args' => [ 'A', [ 'X', 'Y', str_repeat( 'z', 900 ) ] ],
163 'function' => 'strtolower',
167 'function' => 'strtolower',
168 'args' => [ str_repeat( 'x', 900 ) ],
171 'function' => 'in_array',
172 'args' => [ str_repeat( 'y', 900 ), [] ],
175 'function' => 'in_array',
176 'args' => [ str_repeat( 'z', 900 ), [] ],
180 $out = preg_replace( '/^.*(: |memory).*\n/m', '', $out );
181 $out = trim( str_replace( "\n\n", "\n", $out ) );