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