3 use Wikimedia\Rdbms\TransactionProfiler
;
4 use Psr\Log\LoggerInterface
;
7 * @covers \Wikimedia\Rdbms\TransactionProfiler
9 class TransactionProfilerTest
extends PHPUnit_Framework_TestCase
{
10 public function testAffected() {
11 $logger = $this->getMockBuilder( LoggerInterface
::class )->getMock();
12 $logger->expects( $this->exactly( 3 ) )->method( 'info' );
14 $tp = new TransactionProfiler();
15 $tp->setLogger( $logger );
16 $tp->setExpectation( 'maxAffected', 100, __METHOD__
);
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 );
24 public function testReadTime() {
25 $logger = $this->getMockBuilder( LoggerInterface
::class )->getMock();
27 $logger->expects( $this->exactly( 2 ) )->method( 'info' );
29 $tp = new TransactionProfiler();
30 $tp->setLogger( $logger );
31 $tp->setExpectation( 'readQueryTime', 5, __METHOD__
);
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 );
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' );
44 $tp = new TransactionProfiler();
45 $tp->setLogger( $logger );
46 $tp->setExpectation( 'writeQueryTime', 5, __METHOD__
);
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 );
54 public function testAffectedTrx() {
55 $logger = $this->getMockBuilder( LoggerInterface
::class )->getMock();
56 $logger->expects( $this->exactly( 1 ) )->method( 'info' );
58 $tp = new TransactionProfiler();
59 $tp->setLogger( $logger );
60 $tp->setExpectation( 'maxAffected', 100, __METHOD__
);
62 $tp->transactionWritingIn( 'srv1', 'db1', '123' );
63 $tp->transactionWritingOut( 'srv1', 'db1', '123', 1, 200 );
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' );
71 $tp = new TransactionProfiler();
72 $tp->setLogger( $logger );
73 $tp->setExpectation( 'writeQueryTime', 5, __METHOD__
);
75 $tp->transactionWritingIn( 'srv1', 'db1', '123' );
76 $tp->transactionWritingOut( 'srv1', 'db1', '123', 10, 1 );
79 public function testConns() {
80 $logger = $this->getMockBuilder( LoggerInterface
::class )->getMock();
81 $logger->expects( $this->exactly( 2 ) )->method( 'info' );
83 $tp = new TransactionProfiler();
84 $tp->setLogger( $logger );
85 $tp->setExpectation( 'conns', 2, __METHOD__
);
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
93 public function testMasterConns() {
94 $logger = $this->getMockBuilder( LoggerInterface
::class )->getMock();
95 $logger->expects( $this->exactly( 2 ) )->method( 'info' );
97 $tp = new TransactionProfiler();
98 $tp->setLogger( $logger );
99 $tp->setExpectation( 'masterConns', 2, __METHOD__
);
101 $tp->recordConnection( 'srv1', 'db1', false );
102 $tp->recordConnection( 'srv1', 'db2', false );
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
110 public function testReadQueryCount() {
111 $logger = $this->getMockBuilder( LoggerInterface
::class )->getMock();
112 $logger->expects( $this->exactly( 2 ) )->method( 'info' );
114 $tp = new TransactionProfiler();
115 $tp->setLogger( $logger );
116 $tp->setExpectation( 'queries', 2, __METHOD__
);
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
124 public function testWriteQueryCount() {
125 $logger = $this->getMockBuilder( LoggerInterface
::class )->getMock();
126 $logger->expects( $this->exactly( 2 ) )->method( 'info' );
128 $tp = new TransactionProfiler();
129 $tp->setLogger( $logger );
130 $tp->setExpectation( 'writes', 2, __METHOD__
);
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 );
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 );