3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWikiCoversValidator
;
7 use PHPUnit_Framework_TestCase
;
8 use Wikimedia\TestingAccessWrapper
;
13 class BenchmarkerTest
extends PHPUnit_Framework_TestCase
{
15 use MediaWikiCoversValidator
;
17 public function testBenchSimple() {
18 $bench = $this->getMockBuilder( Benchmarker
::class )
19 ->setMethods( [ 'execute', 'output' ] )
21 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
22 $benchProxy->defaultCount
= 3;
26 'test' => function () use ( &$count ) {
31 $this->assertSame( 3, $count );
34 public function testBenchSetup() {
35 $bench = $this->getMockBuilder( Benchmarker
::class )
36 ->setMethods( [ 'execute', 'output' ] )
38 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
39 $benchProxy->defaultCount
= 2;
44 'setup' => function () use ( &$buffer ) {
47 'function' => function () use ( &$buffer ) {
53 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
56 public function testBenchVerbose() {
57 $bench = $this->getMockBuilder( Benchmarker
::class )
58 ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
60 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
61 $benchProxy->defaultCount
= 1;
63 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
64 ->will( $this->returnValueMap( [
69 $bench->expects( $this->once() )->method( 'verboseRun' )
74 'test' => function () {
79 public function noop() {
82 public function testBenchName_method() {
83 $bench = $this->getMockBuilder( Benchmarker
::class )
84 ->setMethods( [ 'execute', 'output', 'addResult' ] )
86 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
87 $benchProxy->defaultCount
= 1;
89 $bench->expects( $this->once() )->method( 'addResult' )
90 ->with( $this->callback( function ( $res ) {
91 return isset( $res['name'] ) && $res['name'] === __CLASS__
. '::noop()';
95 [ 'function' => [ $this, 'noop' ] ]
99 public function testBenchName_string() {
100 $bench = $this->getMockBuilder( Benchmarker
::class )
101 ->setMethods( [ 'execute', 'output', 'addResult' ] )
103 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
104 $benchProxy->defaultCount
= 1;
106 $bench->expects( $this->once() )->method( 'addResult' )
107 ->with( $this->callback( function ( $res ) {
108 return 'strtolower(A)';
112 'function' => 'strtolower',
118 * @covers Benchmarker::verboseRun
120 public function testVerboseRun() {
121 $bench = $this->getMockBuilder( Benchmarker
::class )
122 ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
124 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
125 $benchProxy->defaultCount
= 1;
127 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
128 ->will( $this->returnValueMap( [
133 $bench->expects( $this->once() )->method( 'output' )
134 ->with( $this->callback( function ( $out ) {
135 return preg_match( '/memory.+ peak/', $out ) === 1;
139 'test' => function () {