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