3 namespace MediaWiki\Tests\Maintenance
;
6 use PHPUnit_Framework_TestCase
;
7 use Wikimedia\TestingAccessWrapper
;
12 class BenchmarkerTest
extends PHPUnit_Framework_TestCase
{
14 public function testBenchSimple() {
15 $bench = $this->getMockBuilder( Benchmarker
::class )
16 ->setMethods( [ 'execute', 'output' ] )
18 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
19 $benchProxy->defaultCount
= 3;
23 'test' => function () use ( &$count ) {
28 $this->assertSame( 3, $count );
31 public function testBenchSetup() {
32 $bench = $this->getMockBuilder( Benchmarker
::class )
33 ->setMethods( [ 'execute', 'output' ] )
35 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
36 $benchProxy->defaultCount
= 2;
41 'setup' => function () use ( &$buffer ) {
44 'function' => function () use ( &$buffer ) {
50 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
53 public function testBenchVerbose() {
54 $bench = $this->getMockBuilder( Benchmarker
::class )
55 ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
57 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
58 $benchProxy->defaultCount
= 1;
60 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
61 ->will( $this->returnValueMap( [
66 $bench->expects( $this->once() )->method( 'verboseRun' )
71 'test' => function () {
76 public function noop() {
79 public function testBenchName_method() {
80 $bench = $this->getMockBuilder( Benchmarker
::class )
81 ->setMethods( [ 'execute', 'output', 'addResult' ] )
83 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
84 $benchProxy->defaultCount
= 1;
86 $bench->expects( $this->once() )->method( 'addResult' )
87 ->with( $this->callback( function ( $res ) {
88 return isset( $res['name'] ) && $res['name'] === __CLASS__
. '::noop()';
92 [ 'function' => [ $this, 'noop' ] ]
96 public function testBenchName_string() {
97 $bench = $this->getMockBuilder( Benchmarker
::class )
98 ->setMethods( [ 'execute', 'output', 'addResult' ] )
100 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
101 $benchProxy->defaultCount
= 1;
103 $bench->expects( $this->once() )->method( 'addResult' )
104 ->with( $this->callback( function ( $res ) {
105 return 'strtolower(A)';
109 'function' => 'strtolower',
115 * @covers Benchmarker::verboseRun
117 public function testVerboseRun() {
118 $bench = $this->getMockBuilder( Benchmarker
::class )
119 ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
121 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
122 $benchProxy->defaultCount
= 1;
124 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
125 ->will( $this->returnValueMap( [
130 $bench->expects( $this->once() )->method( 'output' )
131 ->with( $this->callback( function ( $out ) {
132 return preg_match( '/memory.+ peak/', $out ) === 1;
136 'test' => function () {