MediaWiki  1.32.5
BenchmarkerTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use MediaWikiCoversValidator;
7 use Wikimedia\TestingAccessWrapper;
8 
12 class BenchmarkerTest extends \PHPUnit\Framework\TestCase {
13 
14  use MediaWikiCoversValidator;
15 
16  public function testBenchSimple() {
17  $bench = $this->getMockBuilder( Benchmarker::class )
18  ->setMethods( [ 'execute', 'output' ] )
19  ->getMock();
20  $benchProxy = TestingAccessWrapper::newFromObject( $bench );
21  $benchProxy->defaultCount = 3;
22 
23  $count = 0;
24  $bench->bench( [
25  'test' => function () use ( &$count ) {
26  $count++;
27  }
28  ] );
29 
30  $this->assertSame( 3, $count );
31  }
32 
33  public function testBenchSetup() {
34  $bench = $this->getMockBuilder( Benchmarker::class )
35  ->setMethods( [ 'execute', 'output' ] )
36  ->getMock();
37  $benchProxy = TestingAccessWrapper::newFromObject( $bench );
38  $benchProxy->defaultCount = 2;
39 
40  $buffer = [];
41  $bench->bench( [
42  'test' => [
43  'setup' => function () use ( &$buffer ) {
44  $buffer[] = 'setup';
45  },
46  'function' => function () use ( &$buffer ) {
47  $buffer[] = 'run';
48  }
49  ]
50  ] );
51 
52  $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
53  }
54 
55  public function testBenchVerbose() {
56  $bench = $this->getMockBuilder( Benchmarker::class )
57  ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
58  ->getMock();
59  $benchProxy = TestingAccessWrapper::newFromObject( $bench );
60  $benchProxy->defaultCount = 1;
61 
62  $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
63  ->will( $this->returnValueMap( [
64  [ 'verbose', true ],
65  [ 'count', false ],
66  ] ) );
67 
68  $bench->expects( $this->once() )->method( 'verboseRun' )
69  ->with( 0 )
70  ->willReturn( null );
71 
72  $bench->bench( [
73  'test' => function () {
74  }
75  ] );
76  }
77 
78  public function noop() {
79  }
80 
81  public function testBenchName_method() {
82  $bench = $this->getMockBuilder( Benchmarker::class )
83  ->setMethods( [ 'execute', 'output', 'addResult' ] )
84  ->getMock();
85  $benchProxy = TestingAccessWrapper::newFromObject( $bench );
86  $benchProxy->defaultCount = 1;
87 
88  $bench->expects( $this->once() )->method( 'addResult' )
89  ->with( $this->callback( function ( $res ) {
90  return isset( $res['name'] ) && $res['name'] === __CLASS__ . '::noop()';
91  } ) );
92 
93  $bench->bench( [
94  [ 'function' => [ $this, 'noop' ] ]
95  ] );
96  }
97 
98  public function testBenchName_string() {
99  $bench = $this->getMockBuilder( Benchmarker::class )
100  ->setMethods( [ 'execute', 'output', 'addResult' ] )
101  ->getMock();
102  $benchProxy = TestingAccessWrapper::newFromObject( $bench );
103  $benchProxy->defaultCount = 1;
104 
105  $bench->expects( $this->once() )->method( 'addResult' )
106  ->with( $this->callback( function ( $res ) {
107  return 'strtolower(A)';
108  } ) );
109 
110  $bench->bench( [ [
111  'function' => 'strtolower',
112  'args' => [ 'A' ],
113  ] ] );
114  }
115 
119  public function testVerboseRun() {
120  $bench = $this->getMockBuilder( Benchmarker::class )
121  ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
122  ->getMock();
123  $benchProxy = TestingAccessWrapper::newFromObject( $bench );
124  $benchProxy->defaultCount = 1;
125 
126  $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
127  ->will( $this->returnValueMap( [
128  [ 'verbose', true ],
129  [ 'count', false ],
130  ] ) );
131 
132  $bench->expects( $this->once() )->method( 'output' )
133  ->with( $this->callback( function ( $out ) {
134  return preg_match( '/memory.+ peak/', $out ) === 1;
135  } ) );
136 
137  $bench->bench( [
138  'test' => function () {
139  }
140  ] );
141  }
142 }
MediaWiki\Tests\Maintenance\BenchmarkerTest\testBenchName_method
testBenchName_method()
Definition: BenchmarkerTest.php:81
MediaWiki\Tests\Maintenance\BenchmarkerTest
Benchmarker.
Definition: BenchmarkerTest.php:12
MediaWiki\Tests\Maintenance\BenchmarkerTest\testBenchSetup
testBenchSetup()
Definition: BenchmarkerTest.php:33
MediaWiki\Tests\Maintenance\BenchmarkerTest\testBenchSimple
testBenchSimple()
Definition: BenchmarkerTest.php:16
MediaWiki\Tests\Maintenance\BenchmarkerTest\testBenchVerbose
testBenchVerbose()
Definition: BenchmarkerTest.php:55
$res
$res
Definition: database.txt:21
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Tests\Maintenance
Definition: backup_LogTest.php:3
MediaWiki\Tests\Maintenance\BenchmarkerTest\testBenchName_string
testBenchName_string()
Definition: BenchmarkerTest.php:98
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Tests\Maintenance\BenchmarkerTest\testVerboseRun
testVerboseRun()
Benchmarker::verboseRun.
Definition: BenchmarkerTest.php:119
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
MediaWiki\Tests\Maintenance\BenchmarkerTest\noop
noop()
Definition: BenchmarkerTest.php:78
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$buffer
$buffer
Definition: mwdoc-filter.php:49
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:813