3 use Wikimedia\TestingAccessWrapper
;
8 class BenchmarkerTest
extends PHPUnit_Framework_TestCase
{
10 use MediaWikiCoversValidator
;
12 public function testBenchSimple() {
13 $bench = $this->getMockBuilder( Benchmarker
::class )
14 ->setMethods( [ 'execute', 'output' ] )
16 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
17 $benchProxy->defaultCount
= 3;
21 'test' => function () use ( &$count ) {
26 $this->assertSame( 3, $count );
29 public function testBenchSetup() {
30 $bench = $this->getMockBuilder( Benchmarker
::class )
31 ->setMethods( [ 'execute', 'output' ] )
33 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
34 $benchProxy->defaultCount
= 2;
39 'setup' => function () use ( &$buffer ) {
42 'function' => function () use ( &$buffer ) {
48 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
51 public function testBenchVerbose() {
52 $bench = $this->getMockBuilder( Benchmarker
::class )
53 ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
55 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
56 $benchProxy->defaultCount
= 1;
58 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
59 ->will( $this->returnValueMap( [
64 $bench->expects( $this->once() )->method( 'verboseRun' )
69 'test' => function () {
74 public function noop() {
77 public function testBenchName_method() {
78 $bench = $this->getMockBuilder( Benchmarker
::class )
79 ->setMethods( [ 'execute', 'output', 'addResult' ] )
81 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
82 $benchProxy->defaultCount
= 1;
84 $bench->expects( $this->once() )->method( 'addResult' )
85 ->with( $this->callback( function ( $res ) {
86 return isset( $res['name'] ) && $res['name'] === __CLASS__
. '::noop()';
90 [ 'function' => [ $this, 'noop' ] ]
94 public function testBenchName_string() {
95 $bench = $this->getMockBuilder( Benchmarker
::class )
96 ->setMethods( [ 'execute', 'output', 'addResult' ] )
98 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
99 $benchProxy->defaultCount
= 1;
101 $bench->expects( $this->once() )->method( 'addResult' )
102 ->with( $this->callback( function ( $res ) {
103 return 'strtolower(A)';
107 'function' => 'strtolower',
113 * @covers Benchmarker::verboseRun
115 public function testVerboseRun() {
116 $bench = $this->getMockBuilder( Benchmarker
::class )
117 ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
119 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
120 $benchProxy->defaultCount
= 1;
122 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
123 ->will( $this->returnValueMap( [
128 $bench->expects( $this->once() )->method( 'output' )
129 ->with( $this->callback( function ( $out ) {
130 return preg_match( '/memory.+ peak/', $out ) === 1;
134 'test' => function () {