0b8d0d98e457d20f5958b6ba4e36bee93039b8c8
[lhc/web/wiklou.git] / tests / phpunit / maintenance / BenchmarkerTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use Benchmarker;
6 use MediaWikiCoversValidator;
7 use PHPUnit_Framework_TestCase;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @covers Benchmarker
12 */
13 class BenchmarkerTest extends PHPUnit_Framework_TestCase {
14
15 use MediaWikiCoversValidator;
16
17 public function testBenchSimple() {
18 $bench = $this->getMockBuilder( Benchmarker::class )
19 ->setMethods( [ 'execute', 'output' ] )
20 ->getMock();
21 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
22 $benchProxy->defaultCount = 3;
23
24 $count = 0;
25 $bench->bench( [
26 'test' => 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 ->setMethods( [ 'execute', 'output' ] )
37 ->getMock();
38 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
39 $benchProxy->defaultCount = 2;
40
41 $buffer = [];
42 $bench->bench( [
43 'test' => [
44 'setup' => function () use ( &$buffer ) {
45 $buffer[] = 'setup';
46 },
47 'function' => 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 ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
59 ->getMock();
60 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
61 $benchProxy->defaultCount = 1;
62
63 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
64 ->will( $this->returnValueMap( [
65 [ 'verbose', true ],
66 [ 'count', false ],
67 ] ) );
68
69 $bench->expects( $this->once() )->method( 'verboseRun' )
70 ->with( 0 )
71 ->willReturn( null );
72
73 $bench->bench( [
74 'test' => function () {
75 }
76 ] );
77 }
78
79 public function noop() {
80 }
81
82 public function testBenchName_method() {
83 $bench = $this->getMockBuilder( Benchmarker::class )
84 ->setMethods( [ 'execute', 'output', 'addResult' ] )
85 ->getMock();
86 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
87 $benchProxy->defaultCount = 1;
88
89 $bench->expects( $this->once() )->method( 'addResult' )
90 ->with( $this->callback( function ( $res ) {
91 return isset( $res['name'] ) && $res['name'] === __CLASS__ . '::noop()';
92 } ) );
93
94 $bench->bench( [
95 [ 'function' => [ $this, 'noop' ] ]
96 ] );
97 }
98
99 public function testBenchName_string() {
100 $bench = $this->getMockBuilder( Benchmarker::class )
101 ->setMethods( [ 'execute', 'output', 'addResult' ] )
102 ->getMock();
103 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
104 $benchProxy->defaultCount = 1;
105
106 $bench->expects( $this->once() )->method( 'addResult' )
107 ->with( $this->callback( function ( $res ) {
108 return 'strtolower(A)';
109 } ) );
110
111 $bench->bench( [ [
112 'function' => 'strtolower',
113 'args' => [ 'A' ],
114 ] ] );
115 }
116
117 /**
118 * @covers Benchmarker::verboseRun
119 */
120 public function testVerboseRun() {
121 $bench = $this->getMockBuilder( Benchmarker::class )
122 ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
123 ->getMock();
124 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
125 $benchProxy->defaultCount = 1;
126
127 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
128 ->will( $this->returnValueMap( [
129 [ 'verbose', true ],
130 [ 'count', false ],
131 ] ) );
132
133 $bench->expects( $this->once() )->method( 'output' )
134 ->with( $this->callback( function ( $out ) {
135 return preg_match( '/memory.+ peak/', $out ) === 1;
136 } ) );
137
138 $bench->bench( [
139 'test' => function () {
140 }
141 ] );
142 }
143 }