Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / includes / BenchmarkerTest.php
blobe6d1fe018e9918fcd50531ffff9b107c0dd2f362
1 <?php
3 namespace MediaWiki\Tests\Maintenance\Includes;
5 use MediaWiki\Maintenance\Benchmarker;
6 use MediaWikiCoversValidator;
7 use PHPUnit\Framework\TestCase;
8 use Wikimedia\TestingAccessWrapper;
10 /**
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' ] )
20 ->getMock();
21 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
22 $benchProxy->defaultCount = 3;
24 $count = 0;
25 $bench->bench( [
26 'test' => static function () use ( &$count ) {
27 $count++;
29 ] );
31 $this->assertSame( 3, $count );
34 public function testBenchSetup() {
35 $bench = $this->getMockBuilder( Benchmarker::class )
36 ->onlyMethods( [ 'execute', 'output' ] )
37 ->getMock();
38 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
39 $benchProxy->defaultCount = 2;
41 $buffer = [];
42 $bench->bench( [
43 'test' => [
44 'setup' => static function () use ( &$buffer ) {
45 $buffer[] = 'setup';
47 'function' => static function () use ( &$buffer ) {
48 $buffer[] = 'run';
51 ] );
53 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
56 public function testBenchVerbose() {
57 $bench = $this->getMockBuilder( Benchmarker::class )
58 ->onlyMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
59 ->getMock();
60 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
61 $benchProxy->defaultCount = 1;
63 $bench->expects( $this->once() )->method( 'hasOption' )
64 ->willReturnMap( [
65 [ 'verbose', true ],
66 ] );
68 $bench->expects( $this->once() )->method( 'verboseRun' )
69 ->with( 0 )
70 ->willReturn( null );
72 $bench->bench( [
73 'test' => static function () {
75 ] );
78 public function noop() {
81 public function testBenchName_method() {
82 $bench = $this->getMockBuilder( Benchmarker::class )
83 ->onlyMethods( [ 'execute', 'output', 'addResult' ] )
84 ->getMock();
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()' );
91 } ) );
93 $bench->bench( [
94 [ 'function' => [ $this, 'noop' ] ]
95 ] );
98 public function testBenchName_string() {
99 $bench = $this->getMockBuilder( Benchmarker::class )
100 ->onlyMethods( [ 'execute', 'output', 'addResult' ] )
101 ->getMock();
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')";
108 } ) );
110 $bench->bench( [ [
111 'function' => 'strtolower',
112 'args' => [ 'A' ],
113 ] ] );
116 public function testVerboseRun() {
117 $bench = $this->getMockBuilder( Benchmarker::class )
118 ->onlyMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
119 ->getMock();
120 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
121 $benchProxy->defaultCount = 1;
123 $bench->expects( $this->once() )->method( 'hasOption' )
124 ->willReturnMap( [
125 [ 'verbose', true ],
126 ] );
128 $bench->expects( $this->once() )->method( 'output' )
129 ->with( $this->callback( static function ( $out ) {
130 return preg_match( '/memory.+ peak/', $out ) === 1;
131 } ) );
133 $bench->bench( [
134 'test' => static function () {
136 ] );
139 public function testNaming() {
140 $bench = $this->getMockBuilder( Benchmarker::class )
141 ->onlyMethods( [ 'execute', 'output', 'startBench' ] )
142 ->getMock();
143 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
144 $benchProxy->defaultCount = 1;
146 $out = '';
147 $bench->expects( $this->any() )->method( 'output' )
148 ->willReturnCallback( static function ( $str ) use ( &$out ) {
149 $out .= $str;
150 return null;
151 } );
153 $bench->bench( [
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',
164 'args' => [ 'A' ],
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 ), [] ],
178 ] );
180 $out = preg_replace( '/^.*(: |memory).*\n/m', '', $out );
181 $out = trim( str_replace( "\n\n", "\n", $out ) );
182 $this->assertEquals(
183 <<<TEXT
184 in_array@1
185 in_array@2
186 strtolower('A')
187 strtolower@2
188 in_array@3
189 in_array@4
190 TEXT,
191 $out