File: TransactionProfilerTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (244 lines) | stat: -rw-r--r-- 8,697 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Wikimedia\Rdbms\TransactionProfiler;

/**
 * @covers \Wikimedia\Rdbms\TransactionProfiler
 */
class TransactionProfilerTest extends TestCase {

	use MediaWikiCoversValidator;

	public function testAffected() {
		$logger = $this->createMock( LoggerInterface::class );
		$logger->expects( $this->exactly( 3 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'maxAffected', 100, __METHOD__ );

		$tp->transactionWritingIn( 'srv1', 'db1', '123', $now );
		$tp->recordQueryCompletion( "SQL 1", $now - 3, true, 200, '1' );
		$tp->recordQueryCompletion( "SQL 2", $now - 3, true, 200, '1' );
		$tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 400 );
	}

	public function testReadTime() {
		$logger = $this->createMock( LoggerInterface::class );
		// 1 per query
		$logger->expects( $this->exactly( 2 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'readQueryTime', 5, __METHOD__ );

		$tp->transactionWritingIn( 'srv1', 'db1', '123', $now );
		$tp->recordQueryCompletion( "SQL 1", $now - 10, false, 1, '1' );
		$tp->recordQueryCompletion( "SQL 2", $now - 10, false, 1, '1' );
		$tp->transactionWritingOut( 'srv1', 'db1', '123', 0, 0 );
	}

	public function testWriteTime() {
		$logger = $this->createMock( LoggerInterface::class );
		// 1 per query, 1 per trx, and one "sub-optimal trx" entry
		$logger->expects( $this->exactly( 4 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ );

		$tp->transactionWritingIn( 'srv1', 'db1', '123', $now );
		$tp->recordQueryCompletion( "SQL 1", $now - 10, true, 1, '1' );
		$tp->recordQueryCompletion( "SQL 2", $now - 10, true, 1, '1' );
		$tp->transactionWritingOut( 'srv1', 'db1', '123', 20, 1 );
	}

	public function testAffectedTrx() {
		$logger = $this->createMock( LoggerInterface::class );
		$logger->expects( $this->once() )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'maxAffected', 100, __METHOD__ );

		$tp->transactionWritingIn( 'srv1', 'db1', '123', $now );
		$tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 200 );
	}

	public function testWriteTimeTrx() {
		$logger = $this->createMock( LoggerInterface::class );
		// 1 per trx, and one "sub-optimal trx" entry
		$logger->expects( $this->exactly( 2 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ );

		$tp->transactionWritingIn( 'srv1', 'db1', '123', $now );
		$tp->transactionWritingOut( 'srv1', 'db1', '123', 10, 1 );
	}

	public function testConns() {
		$logger = $this->createMock( LoggerInterface::class );
		$logger->expects( $this->exactly( 2 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'conns', 2, __METHOD__ );

		$tp->recordConnection( 'srv1', 'db1', false );
		$tp->recordConnection( 'srv1', 'db2', false );
		$tp->recordConnection( 'srv1', 'db3', false ); // warn
		$tp->recordConnection( 'srv1', 'db4', false ); // warn
	}

	public function testMasterConns() {
		$logger = $this->createMock( LoggerInterface::class );
		$logger->expects( $this->exactly( 2 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'masterConns', 2, __METHOD__ );

		$tp->recordConnection( 'srv1', 'db1', false );
		$tp->recordConnection( 'srv1', 'db2', false );

		$tp->recordConnection( 'srv1', 'db1', true );
		$tp->recordConnection( 'srv1', 'db2', true );
		$tp->recordConnection( 'srv1', 'db3', true ); // warn
		$tp->recordConnection( 'srv1', 'db4', true ); // warn
	}

	public function testReadQueryCount() {
		$logger = $this->createMock( LoggerInterface::class );
		$logger->expects( $this->exactly( 2 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'queries', 2, __METHOD__ );

		$tp->recordQueryCompletion( "SQL 1", $now - 0.01, false, 0, '1' );
		$tp->recordQueryCompletion( "SQL 2", $now - 0.01, false, 0, '1' );
		$tp->recordQueryCompletion( "SQL 3", $now - 0.01, false, 0, '1' ); // warn
		$tp->recordQueryCompletion( "SQL 4", $now - 0.01, false, 0, '1' ); // warn
	}

	public function testWriteQueryCount() {
		$logger = $this->createMock( LoggerInterface::class );
		$logger->expects( $this->exactly( 2 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'writes', 2, __METHOD__ );

		$tp->recordQueryCompletion( "SQL 1", $now - 0.01, false, 0, '1' );
		$tp->recordQueryCompletion( "SQL 2", $now - 0.01, false, 0, '1' );
		$tp->recordQueryCompletion( "SQL 3", $now - 0.01, false, 0, '1' );
		$tp->recordQueryCompletion( "SQL 4", $now - 0.01, false, 0, '1' );

		$tp->transactionWritingIn( 'srv1', 'db1', '123', $now );
		$tp->recordQueryCompletion( "SQL 1w", $now - 0.01, true, 2, '1' );
		$tp->recordQueryCompletion( "SQL 2w", $now - 0.01, true, 5, '1' );
		$tp->recordQueryCompletion( "SQL 3w", $now - 0.01, true, 3, '1' );
		$tp->recordQueryCompletion( "SQL 4w", $now - 0.01, true, 1, '1' );
		$tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 1 );
	}

	public function testSilence() {
		$logger = $this->createMock( LoggerInterface::class );
		$logger->expects( $this->never() )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'conns', 2, __METHOD__ );
		$tp->setExpectation( 'masterConns', 0, __METHOD__ );
		$tp->setExpectation( 'writes', 0, __METHOD__ );
		$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ );

		$scope = $tp->silenceForScope();

		$tp->recordConnection( 'srv1', 'enwiki', true );
		$tp->transactionWritingIn( 'srv1', 'db1', '123', $now );
		$tp->recordConnection( 'srv2', 'enwiki', false );
		$tp->recordConnection( 'srv3', 'enwiki', false );
		$tp->recordQueryCompletion( "SQL 1", $now - 10, true, 1, '1' );
		$tp->transactionWritingOut( 'srv1', 'db1', '123', 10, 1 );

		unset( $scope );
	}

	public function testUnsilence() {
		$logger = $this->createMock( LoggerInterface::class );
		// 1 "masterConns" entry, 1 "conns" entry, 1 "writes" entry, 1 "writeQueryTime" entry
		$logger->expects( $this->exactly( 4 ) )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );

		$tp->setExpectation( 'conns', 2, __METHOD__ );
		$tp->setExpectation( 'masterConns', 0, __METHOD__ );
		$tp->setExpectation( 'writes', 0, __METHOD__ );
		$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ );

		$scope = $tp->silenceForScope();
		$tp->recordConnection( 'srv1', 'enwiki', true );
		$tp->transactionWritingIn( 'srv1', 'db1', '123', $now );
		$tp->recordConnection( 'srv2', 'enwiki', false );
		$tp->recordConnection( 'srv3', 'enwiki', false );
		$tp->recordQueryCompletion( "SQL 1", $now - 10, true, 1, '1' );
		$tp->transactionWritingOut( 'srv1', 'db1', '123', 10, 1 );
		unset( $scope );

		$tp->recordConnection( 'srv1', 'enwiki', true );
		$tp->recordConnection( 'srv2', 'enwiki', false );
		$tp->recordConnection( 'srv3', 'enwiki', false );
		$tp->recordQueryCompletion( "SQL 2", $now - 10, true, 1, '1' );
	}

	public function testPartialSilence() {
		$logger = $this->createMock( LoggerInterface::class );
		// 1 entry for slow write
		$logger->expects( $this->once() )->method( 'warning' );

		$now = 1668108368.0;
		$tp = new TransactionProfiler();
		$tp->setMockTime( $now );
		$tp->setLogger( $logger );
		$tp->setExpectation( 'conns', 2, __METHOD__ );
		$tp->setExpectation( 'masterConns', 0, __METHOD__ );
		$tp->setExpectation( 'writes', 0, __METHOD__ );
		$tp->setExpectation( 'writeQueryTime', 5, __METHOD__ );

		$scope = $tp->silenceForScope( $tp::EXPECTATION_REPLICAS_ONLY );

		$tp->recordConnection( 'srv1', 'enwiki', true );
		$tp->recordConnection( 'srv2', 'enwiki', false );
		$tp->recordQueryCompletion( "SQL 1", $now - 10, true, 1, '1' );

		unset( $scope );
	}
}