BenchmarkerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace MediaWiki\Tests\Maintenance;
  3. use Benchmarker;
  4. use MediaWikiCoversValidator;
  5. use Wikimedia\TestingAccessWrapper;
  6. /**
  7. * @covers Benchmarker
  8. */
  9. class BenchmarkerTest extends \PHPUnit\Framework\TestCase {
  10. use MediaWikiCoversValidator;
  11. public function testBenchSimple() {
  12. $bench = $this->getMockBuilder( Benchmarker::class )
  13. ->setMethods( [ 'execute', 'output' ] )
  14. ->getMock();
  15. $benchProxy = TestingAccessWrapper::newFromObject( $bench );
  16. $benchProxy->defaultCount = 3;
  17. $count = 0;
  18. $bench->bench( [
  19. 'test' => function () use ( &$count ) {
  20. $count++;
  21. }
  22. ] );
  23. $this->assertSame( 3, $count );
  24. }
  25. public function testBenchSetup() {
  26. $bench = $this->getMockBuilder( Benchmarker::class )
  27. ->setMethods( [ 'execute', 'output' ] )
  28. ->getMock();
  29. $benchProxy = TestingAccessWrapper::newFromObject( $bench );
  30. $benchProxy->defaultCount = 2;
  31. $buffer = [];
  32. $bench->bench( [
  33. 'test' => [
  34. 'setup' => function () use ( &$buffer ) {
  35. $buffer[] = 'setup';
  36. },
  37. 'function' => function () use ( &$buffer ) {
  38. $buffer[] = 'run';
  39. }
  40. ]
  41. ] );
  42. $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
  43. }
  44. public function testBenchVerbose() {
  45. $bench = $this->getMockBuilder( Benchmarker::class )
  46. ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
  47. ->getMock();
  48. $benchProxy = TestingAccessWrapper::newFromObject( $bench );
  49. $benchProxy->defaultCount = 1;
  50. $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
  51. ->will( $this->returnValueMap( [
  52. [ 'verbose', true ],
  53. [ 'count', false ],
  54. ] ) );
  55. $bench->expects( $this->once() )->method( 'verboseRun' )
  56. ->with( 0 )
  57. ->willReturn( null );
  58. $bench->bench( [
  59. 'test' => function () {
  60. }
  61. ] );
  62. }
  63. public function noop() {
  64. }
  65. public function testBenchName_method() {
  66. $bench = $this->getMockBuilder( Benchmarker::class )
  67. ->setMethods( [ 'execute', 'output', 'addResult' ] )
  68. ->getMock();
  69. $benchProxy = TestingAccessWrapper::newFromObject( $bench );
  70. $benchProxy->defaultCount = 1;
  71. $bench->expects( $this->once() )->method( 'addResult' )
  72. ->with( $this->callback( function ( $res ) {
  73. return isset( $res['name'] ) && $res['name'] === __CLASS__ . '::noop()';
  74. } ) );
  75. $bench->bench( [
  76. [ 'function' => [ $this, 'noop' ] ]
  77. ] );
  78. }
  79. public function testBenchName_string() {
  80. $bench = $this->getMockBuilder( Benchmarker::class )
  81. ->setMethods( [ 'execute', 'output', 'addResult' ] )
  82. ->getMock();
  83. $benchProxy = TestingAccessWrapper::newFromObject( $bench );
  84. $benchProxy->defaultCount = 1;
  85. $bench->expects( $this->once() )->method( 'addResult' )
  86. ->with( $this->callback( function ( $res ) {
  87. return 'strtolower(A)';
  88. } ) );
  89. $bench->bench( [ [
  90. 'function' => 'strtolower',
  91. 'args' => [ 'A' ],
  92. ] ] );
  93. }
  94. /**
  95. * @covers Benchmarker::verboseRun
  96. */
  97. public function testVerboseRun() {
  98. $bench = $this->getMockBuilder( Benchmarker::class )
  99. ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
  100. ->getMock();
  101. $benchProxy = TestingAccessWrapper::newFromObject( $bench );
  102. $benchProxy->defaultCount = 1;
  103. $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
  104. ->will( $this->returnValueMap( [
  105. [ 'verbose', true ],
  106. [ 'count', false ],
  107. ] ) );
  108. $bench->expects( $this->once() )->method( 'output' )
  109. ->with( $this->callback( function ( $out ) {
  110. return preg_match( '/memory.+ peak/', $out ) === 1;
  111. } ) );
  112. $bench->bench( [
  113. 'test' => function () {
  114. }
  115. ] );
  116. }
  117. }