3 use Wikimedia\TestingAccessWrapper
;
8 class BenchmarkerTest
extends PHPUnit_Framework_TestCase
{
9 public function testBenchSimple() {
10 $bench = $this->getMockBuilder( Benchmarker
::class )
11 ->setMethods( [ 'execute', 'output' ] )
13 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
14 $benchProxy->defaultCount
= 3;
18 'test' => function () use ( &$count ) {
23 $this->assertSame( 3, $count );
26 public function testBenchSetup() {
27 $bench = $this->getMockBuilder( Benchmarker
::class )
28 ->setMethods( [ 'execute', 'output' ] )
30 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
31 $benchProxy->defaultCount
= 2;
36 'setup' => function () use ( &$buffer ) {
39 'function' => function () use ( &$buffer ) {
45 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
48 public function testBenchVerbose() {
49 $bench = $this->getMockBuilder( Benchmarker
::class )
50 ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
52 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
53 $benchProxy->defaultCount
= 1;
55 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
56 ->will( $this->returnValueMap( [
61 $bench->expects( $this->once() )->method( 'verboseRun' )
66 'test' => function () {
71 public function noop() {
74 public function testBenchName_method() {
75 $bench = $this->getMockBuilder( Benchmarker
::class )
76 ->setMethods( [ 'execute', 'output', 'addResult' ] )
78 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
79 $benchProxy->defaultCount
= 1;
81 $bench->expects( $this->once() )->method( 'addResult' )
82 ->with( $this->callback( function ( $res ) {
83 return isset( $res['name'] ) && $res['name'] === __CLASS__
. '::noop()';
87 [ 'function' => [ $this, 'noop' ] ]
91 public function testBenchName_string() {
92 $bench = $this->getMockBuilder( Benchmarker
::class )
93 ->setMethods( [ 'execute', 'output', 'addResult' ] )
95 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
96 $benchProxy->defaultCount
= 1;
98 $bench->expects( $this->once() )->method( 'addResult' )
99 ->with( $this->callback( function ( $res ) {
100 return 'strtolower(A)';
104 'function' => 'strtolower',
110 * @covers Benchmarker::verboseRun
112 public function testVerboseRun() {
113 $bench = $this->getMockBuilder( Benchmarker
::class )
114 ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
116 $benchProxy = TestingAccessWrapper
::newFromObject( $bench );
117 $benchProxy->defaultCount
= 1;
119 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
120 ->will( $this->returnValueMap( [
125 $bench->expects( $this->once() )->method( 'output' )
126 ->with( $this->callback( function ( $out ) {
127 return preg_match( '/memory.+ peak/', $out ) === 1;
131 'test' => function () {