Debian Codegraph @

tests/phpunit/maintenance/includes/BenchmarkerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance\Includes;
4
5 use MediaWiki\Maintenance\Benchmarker;
6 use MediaWikiCoversValidator;
7 use PHPUnit\Framework\TestCase;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @covers \MediaWiki\Maintenance\Benchmarker
12 */
13 class BenchmarkerTest extends TestCase {
14
15 use MediaWikiCoversValidator;
16
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;
23
24 $count = 0;
25 $bench->bench( [
26 'test' => static function () use ( &$count ) {
27 $count++;
28 }
29 ] );
30
31 $this->assertSame( 3, $count );
32 }
33
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;
40
41 $buffer = [];
42 $bench->bench( [
43 'test' => [
44 'setup' => static function () use ( &$buffer ) {
45 $buffer[] = 'setup';
46 },
47 'function' => static function () use ( &$buffer ) {
48 $buffer[] = 'run';
49 }
50 ]
51 ] );
52
53 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
54 }
55
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;
62
63 $bench->expects( $this->once() )->method( 'hasOption' )
64 ->willReturnMap( [
65 [ 'verbose', true ],
66 ] );
67
68 $bench->expects( $this->once() )->method( 'verboseRun' )
69 ->with( 0 )
70 ->willReturn( null );
71
72 $bench->bench( [
73 'test' => static function () {
74 }
75 ] );
76 }
77
78 public function noop() {
79 }
80
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;
87
88 $bench->expects( $this->once() )->method( 'addResult' )
89 ->with( $this->callback( static function ( $res ) {
90 return isset( $res['name'] ) && $res['name'] === ( __CLASS__ . '::noop()' );
91 } ) );
92
93 $bench->bench( [
94 [ 'function' => [ $this, 'noop' ] ]
95 ] );
96 }
97
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;
104
105 $bench->expects( $this->once() )->method( 'addResult' )
106 ->with( $this->callback( static function ( $res ) {
107 return $res['name'] === "strtolower('A')";
108 } ) );
109
110 $bench->bench( [ [
111 'function' => 'strtolower',
112 'args' => [ 'A' ],
113 ] ] );
114 }
115
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;
122
123 $bench->expects( $this->once() )->method( 'hasOption' )
124 ->willReturnMap( [
125 [ 'verbose', true ],
126 ] );
127
128 $bench->expects( $this->once() )->method( 'output' )
129 ->with( $this->callback( static function ( $out ) {
130 return preg_match( '/memory.+ peak/', $out ) === 1;
131 } ) );
132
133 $bench->bench( [
134 'test' => static function () {
135 }
136 ] );
137 }
138
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;
145
146 $out = '';
147 $bench->expects( $this->any() )->method( 'output' )
148 ->willReturnCallback( static function ( $str ) use ( &$out ) {
149 $out .= $str;
150 return null;
151 } );
152
153 $bench->bench( [
154 [
155 'function' => 'in_array',
156 'args' => [ 'A', [ 'X', 'Y' ] ],
157 ],
158 [
159 'function' => 'in_array',
160 'args' => [ 'A', [ 'X', 'Y', str_repeat( 'z', 900 ) ] ],
161 ],
162 [
163 'function' => 'strtolower',
164 'args' => [ 'A' ],
165 ],
166 [
167 'function' => 'strtolower',
168 'args' => [ str_repeat( 'x', 900 ) ],
169 ],
170 [
171 'function' => 'in_array',
172 'args' => [ str_repeat( 'y', 900 ), [] ],
173 ],
174 [
175 'function' => 'in_array',
176 'args' => [ str_repeat( 'z', 900 ), [] ],
177 ],
178 ] );
179
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
192 );
193 }
194 }