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