MediaWiki  1.33.0
TransactionProfilerTest.php
Go to the documentation of this file.
1 <?php
2 
4 use Psr\Log\LoggerInterface;
5 
9 class TransactionProfilerTest extends PHPUnit\Framework\TestCase {
10 
11  use MediaWikiCoversValidator;
12 
13  public function testAffected() {
14  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
15  $logger->expects( $this->exactly( 3 ) )->method( 'warning' );
16 
17  $tp = new TransactionProfiler();
18  $tp->setLogger( $logger );
19  $tp->setExpectation( 'maxAffected', 100, __METHOD__ );
20 
21  $tp->transactionWritingIn( 'srv1', 'db1', '123' );
22  $tp->recordQueryCompletion( "SQL 1", microtime( true ) - 3, true, 200 );
23  $tp->recordQueryCompletion( "SQL 2", microtime( true ) - 3, true, 200 );
24  $tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 400 );
25  }
26 
27  public function testReadTime() {
28  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
29  // 1 per query
30  $logger->expects( $this->exactly( 2 ) )->method( 'warning' );
31 
32  $tp = new TransactionProfiler();
33  $tp->setLogger( $logger );
34  $tp->setExpectation( 'readQueryTime', 5, __METHOD__ );
35 
36  $tp->transactionWritingIn( 'srv1', 'db1', '123' );
37  $tp->recordQueryCompletion( "SQL 1", microtime( true ) - 10, false, 1 );
38  $tp->recordQueryCompletion( "SQL 2", microtime( true ) - 10, false, 1 );
39  $tp->transactionWritingOut( 'srv1', 'db1', '123', 0, 0 );
40  }
41 
42  public function testWriteTime() {
43  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
44  // 1 per query, 1 per trx, and one "sub-optimal trx" entry
45  $logger->expects( $this->exactly( 4 ) )->method( 'warning' );
46 
47  $tp = new TransactionProfiler();
48  $tp->setLogger( $logger );
49  $tp->setExpectation( 'writeQueryTime', 5, __METHOD__ );
50 
51  $tp->transactionWritingIn( 'srv1', 'db1', '123' );
52  $tp->recordQueryCompletion( "SQL 1", microtime( true ) - 10, true, 1 );
53  $tp->recordQueryCompletion( "SQL 2", microtime( true ) - 10, true, 1 );
54  $tp->transactionWritingOut( 'srv1', 'db1', '123', 20, 1 );
55  }
56 
57  public function testAffectedTrx() {
58  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
59  $logger->expects( $this->exactly( 1 ) )->method( 'warning' );
60 
61  $tp = new TransactionProfiler();
62  $tp->setLogger( $logger );
63  $tp->setExpectation( 'maxAffected', 100, __METHOD__ );
64 
65  $tp->transactionWritingIn( 'srv1', 'db1', '123' );
66  $tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 200 );
67  }
68 
69  public function testWriteTimeTrx() {
70  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
71  // 1 per trx, and one "sub-optimal trx" entry
72  $logger->expects( $this->exactly( 2 ) )->method( 'warning' );
73 
74  $tp = new TransactionProfiler();
75  $tp->setLogger( $logger );
76  $tp->setExpectation( 'writeQueryTime', 5, __METHOD__ );
77 
78  $tp->transactionWritingIn( 'srv1', 'db1', '123' );
79  $tp->transactionWritingOut( 'srv1', 'db1', '123', 10, 1 );
80  }
81 
82  public function testConns() {
83  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
84  $logger->expects( $this->exactly( 2 ) )->method( 'warning' );
85 
86  $tp = new TransactionProfiler();
87  $tp->setLogger( $logger );
88  $tp->setExpectation( 'conns', 2, __METHOD__ );
89 
90  $tp->recordConnection( 'srv1', 'db1', false );
91  $tp->recordConnection( 'srv1', 'db2', false );
92  $tp->recordConnection( 'srv1', 'db3', false ); // warn
93  $tp->recordConnection( 'srv1', 'db4', false ); // warn
94  }
95 
96  public function testMasterConns() {
97  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
98  $logger->expects( $this->exactly( 2 ) )->method( 'warning' );
99 
100  $tp = new TransactionProfiler();
101  $tp->setLogger( $logger );
102  $tp->setExpectation( 'masterConns', 2, __METHOD__ );
103 
104  $tp->recordConnection( 'srv1', 'db1', false );
105  $tp->recordConnection( 'srv1', 'db2', false );
106 
107  $tp->recordConnection( 'srv1', 'db1', true );
108  $tp->recordConnection( 'srv1', 'db2', true );
109  $tp->recordConnection( 'srv1', 'db3', true ); // warn
110  $tp->recordConnection( 'srv1', 'db4', true ); // warn
111  }
112 
113  public function testReadQueryCount() {
114  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
115  $logger->expects( $this->exactly( 2 ) )->method( 'warning' );
116 
117  $tp = new TransactionProfiler();
118  $tp->setLogger( $logger );
119  $tp->setExpectation( 'queries', 2, __METHOD__ );
120 
121  $tp->recordQueryCompletion( "SQL 1", microtime( true ) - 0.01, false, 0 );
122  $tp->recordQueryCompletion( "SQL 2", microtime( true ) - 0.01, false, 0 );
123  $tp->recordQueryCompletion( "SQL 3", microtime( true ) - 0.01, false, 0 ); // warn
124  $tp->recordQueryCompletion( "SQL 4", microtime( true ) - 0.01, false, 0 ); // warn
125  }
126 
127  public function testWriteQueryCount() {
128  $logger = $this->getMockBuilder( LoggerInterface::class )->getMock();
129  $logger->expects( $this->exactly( 2 ) )->method( 'warning' );
130 
131  $tp = new TransactionProfiler();
132  $tp->setLogger( $logger );
133  $tp->setExpectation( 'writes', 2, __METHOD__ );
134 
135  $tp->recordQueryCompletion( "SQL 1", microtime( true ) - 0.01, false, 0 );
136  $tp->recordQueryCompletion( "SQL 2", microtime( true ) - 0.01, false, 0 );
137  $tp->recordQueryCompletion( "SQL 3", microtime( true ) - 0.01, false, 0 );
138  $tp->recordQueryCompletion( "SQL 4", microtime( true ) - 0.01, false, 0 );
139 
140  $tp->transactionWritingIn( 'srv1', 'db1', '123' );
141  $tp->recordQueryCompletion( "SQL 1w", microtime( true ) - 0.01, true, 2 );
142  $tp->recordQueryCompletion( "SQL 2w", microtime( true ) - 0.01, true, 5 );
143  $tp->recordQueryCompletion( "SQL 3w", microtime( true ) - 0.01, true, 3 );
144  $tp->recordQueryCompletion( "SQL 4w", microtime( true ) - 0.01, true, 1 );
145  $tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 1 );
146  }
147 }
TransactionProfilerTest\testReadQueryCount
testReadQueryCount()
Definition: TransactionProfilerTest.php:113
TransactionProfilerTest\testWriteTime
testWriteTime()
Definition: TransactionProfilerTest.php:42
TransactionProfilerTest\testAffectedTrx
testAffectedTrx()
Definition: TransactionProfilerTest.php:57
TransactionProfilerTest\testMasterConns
testMasterConns()
Definition: TransactionProfilerTest.php:96
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
TransactionProfilerTest\testReadTime
testReadTime()
Definition: TransactionProfilerTest.php:27
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
TransactionProfilerTest
\Wikimedia\Rdbms\TransactionProfiler
Definition: TransactionProfilerTest.php:9
TransactionProfilerTest\testAffected
testAffected()
Definition: TransactionProfilerTest.php:13
TransactionProfilerTest\testWriteQueryCount
testWriteQueryCount()
Definition: TransactionProfilerTest.php:127
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
TransactionProfilerTest\testWriteTimeTrx
testWriteTimeTrx()
Definition: TransactionProfilerTest.php:69
TransactionProfilerTest\testConns
testConns()
Definition: TransactionProfilerTest.php:82
Wikimedia\Rdbms\TransactionProfiler
Helper class that detects high-contention DB queries via profiling calls.
Definition: TransactionProfiler.php:38