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