| 1 |
<?php |
| 2 |
|
| 3 |
use PHPUnit\Framework\TestCase; |
| 4 |
use Psr\Log\LoggerInterface; |
| 5 |
use Wikimedia\Rdbms\TransactionProfiler; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class TransactionProfilerTest extends TestCase { |
| 11 |
|
| 12 |
use MediaWikiCoversValidator; |
| 13 |
|
| 14 |
public function testAffected() { |
| 15 |
$logger = $this->createMock( LoggerInterface::class ); |
| 16 |
$logger->expects( $this->exactly( 3 ) )->method( 'warning' ); |
| 17 |
|
| 18 |
$now = 1668108368.0; |
| 19 |
$tp = new TransactionProfiler(); |
| 20 |
$tp->setMockTime( $now ); |
| 21 |
$tp->setLogger( $logger ); |
| 22 |
$tp->setExpectation( 'maxAffected', 100, __METHOD__ ); |
| 23 |
|
| 24 |
$tp->transactionWritingIn( 'srv1', 'db1', '123', $now ); |
| 25 |
$tp->recordQueryCompletion( "SQL 1", $now - 3, true, 200, '1' ); |
| 26 |
$tp->recordQueryCompletion( "SQL 2", $now - 3, true, 200, '1' ); |
| 27 |
$tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 400 ); |
| 28 |
} |
| 29 |
|
| 30 |
public function testReadTime() { |
| 31 |
$logger = $this->createMock( LoggerInterface::class ); |
| 32 |
|
| 33 |
$logger->expects( $this->exactly( 2 ) )->method( 'warning' ); |
| 34 |
|
| 35 |
$now = 1668108368.0; |
| 36 |
$tp = new TransactionProfiler(); |
| 37 |
$tp->setMockTime( $now ); |
| 38 |
$tp->setLogger( $logger ); |
| 39 |
$tp->setExpectation( 'readQueryTime', 5, __METHOD__ ); |
| 40 |
|
| 41 |
$tp->transactionWritingIn( 'srv1', 'db1', '123', $now ); |
| 42 |
$tp->recordQueryCompletion( "SQL 1", $now - 10, false, 1, '1' ); |
| 43 |
$tp->recordQueryCompletion( "SQL 2", $now - 10, false, 1, '1' ); |
| 44 |
$tp->transactionWritingOut( 'srv1', 'db1', '123', 0, 0 ); |
| 45 |
} |
| 46 |
|
| 47 |
public function testWriteTime() { |
| 48 |
$logger = $this->createMock( LoggerInterface::class ); |
| 49 |
|
| 50 |
$logger->expects( $this->exactly( 4 ) )->method( 'warning' ); |
| 51 |
|
| 52 |
$now = 1668108368.0; |
| 53 |
$tp = new TransactionProfiler(); |
| 54 |
$tp->setMockTime( $now ); |
| 55 |
$tp->setLogger( $logger ); |
| 56 |
$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ ); |
| 57 |
|
| 58 |
$tp->transactionWritingIn( 'srv1', 'db1', '123', $now ); |
| 59 |
$tp->recordQueryCompletion( "SQL 1", $now - 10, true, 1, '1' ); |
| 60 |
$tp->recordQueryCompletion( "SQL 2", $now - 10, true, 1, '1' ); |
| 61 |
$tp->transactionWritingOut( 'srv1', 'db1', '123', 20, 1 ); |
| 62 |
} |
| 63 |
|
| 64 |
public function testAffectedTrx() { |
| 65 |
$logger = $this->createMock( LoggerInterface::class ); |
| 66 |
$logger->expects( $this->once() )->method( 'warning' ); |
| 67 |
|
| 68 |
$now = 1668108368.0; |
| 69 |
$tp = new TransactionProfiler(); |
| 70 |
$tp->setMockTime( $now ); |
| 71 |
$tp->setLogger( $logger ); |
| 72 |
$tp->setExpectation( 'maxAffected', 100, __METHOD__ ); |
| 73 |
|
| 74 |
$tp->transactionWritingIn( 'srv1', 'db1', '123', $now ); |
| 75 |
$tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 200 ); |
| 76 |
} |
| 77 |
|
| 78 |
public function testWriteTimeTrx() { |
| 79 |
$logger = $this->createMock( LoggerInterface::class ); |
| 80 |
|
| 81 |
$logger->expects( $this->exactly( 2 ) )->method( 'warning' ); |
| 82 |
|
| 83 |
$now = 1668108368.0; |
| 84 |
$tp = new TransactionProfiler(); |
| 85 |
$tp->setMockTime( $now ); |
| 86 |
$tp->setLogger( $logger ); |
| 87 |
$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ ); |
| 88 |
|
| 89 |
$tp->transactionWritingIn( 'srv1', 'db1', '123', $now ); |
| 90 |
$tp->transactionWritingOut( 'srv1', 'db1', '123', 10, 1 ); |
| 91 |
} |
| 92 |
|
| 93 |
public function testConns() { |
| 94 |
$logger = $this->createMock( LoggerInterface::class ); |
| 95 |
$logger->expects( $this->exactly( 2 ) )->method( 'warning' ); |
| 96 |
|
| 97 |
$now = 1668108368.0; |
| 98 |
$tp = new TransactionProfiler(); |
| 99 |
$tp->setMockTime( $now ); |
| 100 |
$tp->setLogger( $logger ); |
| 101 |
$tp->setExpectation( 'conns', 2, __METHOD__ ); |
| 102 |
|
| 103 |
$tp->recordConnection( 'srv1', 'db1', false ); |
| 104 |
$tp->recordConnection( 'srv1', 'db2', false ); |
| 105 |
$tp->recordConnection( 'srv1', 'db3', false ); |
| 106 |
$tp->recordConnection( 'srv1', 'db4', false ); |
| 107 |
} |
| 108 |
|
| 109 |
public function testMasterConns() { |
| 110 |
$logger = $this->createMock( LoggerInterface::class ); |
| 111 |
$logger->expects( $this->exactly( 2 ) )->method( 'warning' ); |
| 112 |
|
| 113 |
$now = 1668108368.0; |
| 114 |
$tp = new TransactionProfiler(); |
| 115 |
$tp->setMockTime( $now ); |
| 116 |
$tp->setLogger( $logger ); |
| 117 |
$tp->setExpectation( 'masterConns', 2, __METHOD__ ); |
| 118 |
|
| 119 |
$tp->recordConnection( 'srv1', 'db1', false ); |
| 120 |
$tp->recordConnection( 'srv1', 'db2', false ); |
| 121 |
|
| 122 |
$tp->recordConnection( 'srv1', 'db1', true ); |
| 123 |
$tp->recordConnection( 'srv1', 'db2', true ); |
| 124 |
$tp->recordConnection( 'srv1', 'db3', true ); |
| 125 |
$tp->recordConnection( 'srv1', 'db4', true ); |
| 126 |
} |
| 127 |
|
| 128 |
public function testReadQueryCount() { |
| 129 |
$logger = $this->createMock( LoggerInterface::class ); |
| 130 |
$logger->expects( $this->exactly( 2 ) )->method( 'warning' ); |
| 131 |
|
| 132 |
$now = 1668108368.0; |
| 133 |
$tp = new TransactionProfiler(); |
| 134 |
$tp->setMockTime( $now ); |
| 135 |
$tp->setLogger( $logger ); |
| 136 |
$tp->setExpectation( 'queries', 2, __METHOD__ ); |
| 137 |
|
| 138 |
$tp->recordQueryCompletion( "SQL 1", $now - 0.01, false, 0, '1' ); |
| 139 |
$tp->recordQueryCompletion( "SQL 2", $now - 0.01, false, 0, '1' ); |
| 140 |
$tp->recordQueryCompletion( "SQL 3", $now - 0.01, false, 0, '1' ); |
| 141 |
$tp->recordQueryCompletion( "SQL 4", $now - 0.01, false, 0, '1' ); |
| 142 |
} |
| 143 |
|
| 144 |
public function testWriteQueryCount() { |
| 145 |
$logger = $this->createMock( LoggerInterface::class ); |
| 146 |
$logger->expects( $this->exactly( 2 ) )->method( 'warning' ); |
| 147 |
|
| 148 |
$now = 1668108368.0; |
| 149 |
$tp = new TransactionProfiler(); |
| 150 |
$tp->setMockTime( $now ); |
| 151 |
$tp->setLogger( $logger ); |
| 152 |
$tp->setExpectation( 'writes', 2, __METHOD__ ); |
| 153 |
|
| 154 |
$tp->recordQueryCompletion( "SQL 1", $now - 0.01, false, 0, '1' ); |
| 155 |
$tp->recordQueryCompletion( "SQL 2", $now - 0.01, false, 0, '1' ); |
| 156 |
$tp->recordQueryCompletion( "SQL 3", $now - 0.01, false, 0, '1' ); |
| 157 |
$tp->recordQueryCompletion( "SQL 4", $now - 0.01, false, 0, '1' ); |
| 158 |
|
| 159 |
$tp->transactionWritingIn( 'srv1', 'db1', '123', $now ); |
| 160 |
$tp->recordQueryCompletion( "SQL 1w", $now - 0.01, true, 2, '1' ); |
| 161 |
$tp->recordQueryCompletion( "SQL 2w", $now - 0.01, true, 5, '1' ); |
| 162 |
$tp->recordQueryCompletion( "SQL 3w", $now - 0.01, true, 3, '1' ); |
| 163 |
$tp->recordQueryCompletion( "SQL 4w", $now - 0.01, true, 1, '1' ); |
| 164 |
$tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 1 ); |
| 165 |
} |
| 166 |
|
| 167 |
public function testSilence() { |
| 168 |
$logger = $this->createMock( LoggerInterface::class ); |
| 169 |
$logger->expects( $this->never() )->method( 'warning' ); |
| 170 |
|
| 171 |
$now = 1668108368.0; |
| 172 |
$tp = new TransactionProfiler(); |
| 173 |
$tp->setMockTime( $now ); |
| 174 |
$tp->setLogger( $logger ); |
| 175 |
$tp->setExpectation( 'conns', 2, __METHOD__ ); |
| 176 |
$tp->setExpectation( 'masterConns', 0, __METHOD__ ); |
| 177 |
$tp->setExpectation( 'writes', 0, __METHOD__ ); |
| 178 |
$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ ); |
| 179 |
|
| 180 |
$scope = $tp->silenceForScope(); |
| 181 |
|
| 182 |
$tp->recordConnection( 'srv1', 'enwiki', true ); |
| 183 |
$tp->transactionWritingIn( 'srv1', 'db1', '123', $now ); |
| 184 |
$tp->recordConnection( 'srv2', 'enwiki', false ); |
| 185 |
$tp->recordConnection( 'srv3', 'enwiki', false ); |
| 186 |
$tp->recordQueryCompletion( "SQL 1", $now - 10, true, 1, '1' ); |
| 187 |
$tp->transactionWritingOut( 'srv1', 'db1', '123', 10, 1 ); |
| 188 |
|
| 189 |
unset( $scope ); |
| 190 |
} |
| 191 |
|
| 192 |
public function testUnsilence() { |
| 193 |
$logger = $this->createMock( LoggerInterface::class ); |
| 194 |
|
| 195 |
$logger->expects( $this->exactly( 4 ) )->method( 'warning' ); |
| 196 |
|
| 197 |
$now = 1668108368.0; |
| 198 |
$tp = new TransactionProfiler(); |
| 199 |
$tp->setMockTime( $now ); |
| 200 |
$tp->setLogger( $logger ); |
| 201 |
|
| 202 |
$tp->setExpectation( 'conns', 2, __METHOD__ ); |
| 203 |
$tp->setExpectation( 'masterConns', 0, __METHOD__ ); |
| 204 |
$tp->setExpectation( 'writes', 0, __METHOD__ ); |
| 205 |
$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ ); |
| 206 |
|
| 207 |
$scope = $tp->silenceForScope(); |
| 208 |
$tp->recordConnection( 'srv1', 'enwiki', true ); |
| 209 |
$tp->transactionWritingIn( 'srv1', 'db1', '123', $now ); |
| 210 |
$tp->recordConnection( 'srv2', 'enwiki', false ); |
| 211 |
$tp->recordConnection( 'srv3', 'enwiki', false ); |
| 212 |
$tp->recordQueryCompletion( "SQL 1", $now - 10, true, 1, '1' ); |
| 213 |
$tp->transactionWritingOut( 'srv1', 'db1', '123', 10, 1 ); |
| 214 |
unset( $scope ); |
| 215 |
|
| 216 |
$tp->recordConnection( 'srv1', 'enwiki', true ); |
| 217 |
$tp->recordConnection( 'srv2', 'enwiki', false ); |
| 218 |
$tp->recordConnection( 'srv3', 'enwiki', false ); |
| 219 |
$tp->recordQueryCompletion( "SQL 2", $now - 10, true, 1, '1' ); |
| 220 |
} |
| 221 |
|
| 222 |
public function testPartialSilence() { |
| 223 |
$logger = $this->createMock( LoggerInterface::class ); |
| 224 |
|
| 225 |
$logger->expects( $this->once() )->method( 'warning' ); |
| 226 |
|
| 227 |
$now = 1668108368.0; |
| 228 |
$tp = new TransactionProfiler(); |
| 229 |
$tp->setMockTime( $now ); |
| 230 |
$tp->setLogger( $logger ); |
| 231 |
$tp->setExpectation( 'conns', 2, __METHOD__ ); |
| 232 |
$tp->setExpectation( 'masterConns', 0, __METHOD__ ); |
| 233 |
$tp->setExpectation( 'writes', 0, __METHOD__ ); |
| 234 |
$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ ); |
| 235 |
|
| 236 |
$scope = $tp->silenceForScope( $tp::EXPECTATION_REPLICAS_ONLY ); |
| 237 |
|
| 238 |
$tp->recordConnection( 'srv1', 'enwiki', true ); |
| 239 |
$tp->recordConnection( 'srv2', 'enwiki', false ); |
| 240 |
$tp->recordQueryCompletion( "SQL 1", $now - 10, true, 1, '1' ); |
| 241 |
|
| 242 |
unset( $scope ); |
| 243 |
} |
| 244 |
} |