Skip to content

Commit 9950dc4

Browse files
committed
Code style
1 parent 57a3d26 commit 9950dc4

File tree

4 files changed

+80
-72
lines changed

4 files changed

+80
-72
lines changed

src/PHPFUI/MySQLSlowQuery/Entry.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct(array $parameters = [])
4040
// $session (not object but index in array of sessions) later.
4141
'Session' => 0,
4242
];
43+
4344
if (($this->parameters['parse_mode'] ?? '') == 'mariadb')
4445
{
4546
unset($this->fields['Id']);
@@ -102,7 +103,8 @@ public function setFromLine(string $line) : self
102103
// Unify with mysql log format: replace space with T, replace second space
103104
// with leading zero, expand YYMMDD value and add microseconds.
104105
$parts = \explode(' ', \substr($line, 8), 2);
105-
if ($parts[1][0] === ' ')
106+
107+
if (' ' === $parts[1][0])
106108
{
107109
$parts[1][0] = '0';
108110
}

src/PHPFUI/MySQLSlowQuery/Parser.php

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ private function getNextLine() : string
127127
return $line;
128128
}
129129

130+
/**
131+
* Derive a string value that determines how the log is parsed.
132+
*/
133+
private function getParseMode(string $sessionHeaderFirstLine) : string
134+
{
135+
return \stripos($sessionHeaderFirstLine, 'MariaDB') ? 'mariadb' : '';
136+
}
137+
130138
private function parse() : void
131139
{
132140
$this->handle = @\fopen($this->fileName, 'r');
@@ -160,7 +168,7 @@ private function parse() : void
160168
// query in this session. In non backward compatible mode, check this:
161169
// if it's not a comment line, this is assumed to be the start of the
162170
// next session header (i.e. there are zero queries in this session).
163-
if ($parseMode === 'mariadb' && \strlen($line = $this->getNextLine()) > 0)
171+
if ('mariadb' === $parseMode && \strlen($line = $this->getNextLine()) > 0)
164172
{
165173
if ('#' !== $line[0])
166174
{
@@ -178,43 +186,48 @@ private function parse() : void
178186
{
179187
$entry = new \PHPFUI\MySQLSlowQuery\Entry(['parse_mode' => $parseMode]);
180188
$query = [];
181-
if ($parseMode === '')
189+
190+
if ('' === $parseMode)
182191
{
183192
// Backward compatible parsing:
184193
// - Ignore comment lines up until "# Time:"
185194
// - Iarse exactly three lines. If any of these are non-comments,
186195
// throw an exception.
187196
// - If there are more than three comment lines, the query below
188197
// them is ignored.
189-
if (!\str_starts_with($line, self::TIME))
198+
if (! \str_starts_with($line, self::TIME))
199+
{
190200
continue;
201+
}
191202
$entry->setFromLine($line);
192203
$entry->setFromLine(\fgets($this->handle));
193204
$entry->setFromLine(\fgets($this->handle));
194205
}
195206
else
196207
{
197-
$timeLineFound = false;
198-
// Parse any following comment lines, and interpret the next
199-
// non-comment line as a query line.
200-
do
201-
{
202-
$entry->setFromLine($line);
203-
if ($parseMode == 'mariadb' && \str_starts_with($line, self::TIME))
204-
{
205-
$timeLineFound = true;
206-
$previousTimeLine = $line;
207-
}
208-
}
209-
while (\strlen($line = $this->getNextLine()) > 0 && '#' === $line[0]);
210-
if ($parseMode == 'mariadb' && !$timeLineFound && $previousTimeLine)
208+
$timeLineFound = false;
209+
// Parse any following comment lines, and interpret the next
210+
// non-comment line as a query line.
211+
do
212+
{
213+
$entry->setFromLine($line);
214+
215+
if ('mariadb' == $parseMode && \str_starts_with($line, self::TIME))
211216
{
212-
// Always add the Time property. Assume that if it is not in the
213-
// log, it's the same as the previous logged query, and that the
214-
// line contains no other properties we don't want to add.
215-
$entry->setFromLine($previousTimeLine);
217+
$timeLineFound = true;
218+
$previousTimeLine = $line;
216219
}
217-
$query[] = \trim($line);
220+
}
221+
while ((\strlen($line = $this->getNextLine()) > 0) && ('#' === $line[0]));
222+
223+
if ('mariadb' == $parseMode && ! $timeLineFound && $previousTimeLine)
224+
{
225+
// Always add the Time property. Assume that if it is not in the
226+
// log, it's the same as the previous logged query, and that the
227+
// line contains no other properties we don't want to add.
228+
$entry->setFromLine($previousTimeLine);
229+
}
230+
$query[] = \trim($line);
218231
}
219232

220233
// gather (more) query lines until a non-query line is reached
@@ -265,12 +278,4 @@ private function pushLine(string $line) : self
265278

266279
return $this;
267280
}
268-
269-
/**
270-
* Derive a string value that determines how the log is parsed.
271-
*/
272-
private function getParseMode(string $sessionHeaderFirstLine) : string
273-
{
274-
return \stripos($sessionHeaderFirstLine, 'MariaDB') ? 'mariadb' : '';
275-
}
276281
}

src/PHPFUI/MySQLSlowQuery/Session.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public function __construct(array $sessionData = [], $parseMode = '')
2929
$this->Server = \trim(\str_replace('. started with:', '', $sessionData[0] ?? 'unknown'));
3030
$this->Version = \substr(\strstr($this->Server, 'Version: ') ?: '', 9);
3131

32-
$delimiter = $parseMode == 'mariadb' ? ' ' : ', ';
32+
$delimiter = 'mariadb' == $parseMode ? ' ' : ', ';
33+
3334
if (\strpos($sessionData[1] ?? '', $delimiter))
3435
{
3536
[$this->Port, $this->Transport] = \explode($delimiter, \trim($sessionData[1]));

tests/UnitTest.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,46 @@ public function testMissingFile() : void
5454
$parser->getSessions();
5555
}
5656

57+
public function testMysqlVsMariadb() : void
58+
{
59+
$parser = new \PHPFUI\MySQLSlowQuery\Parser(__DIR__ . '/logs/ignoredData.log');
60+
$sessions = $parser->getSessions();
61+
$entries = $parser->getEntries();
62+
$this->assertCount(9, $sessions);
63+
// See comments in logfile for why session 3-6 are buggy and 7-9 are not.
64+
$this->assertEquals('c:\wamp64\bin\mysql\mysql8.0.21\bin\mysqld.exe, Version: 8.0.21 (MySQL Community Server - GPL)', $sessions[0]->Server);
65+
$this->assertEquals('c:\wamp64\bin\mysql\mysql8.0.21\bin\mysqld.exe, Version: 8.0.21 (MySQL Community Server - GPL)', $sessions[1]->Server);
66+
$this->assertEquals('Tcp Port: 3306, Named Pipe: /tmp/mysql.sock', $sessions[2]->Server);
67+
$this->assertEquals('Tcp port: 0 Unix socket: /run/mysqld/mysqld.sock', $sessions[3]->Server);
68+
$this->assertEquals('Tcp port: 0 Unix socket: /run/mysqld/mysqld.sock', $sessions[4]->Server);
69+
$this->assertEquals('Tcp port: 0 Unix socket: /run/mysqld/mysqld.sock', $sessions[5]->Server);
70+
$this->assertEquals('mysqld, Version: 10.7.1-MariaDB-1:10.7.1+maria~focal-log (mariadb.org binary distribution)', $sessions[6]->Server);
71+
// It took until the previous session for mariadb to kick in, but going
72+
// forward "sessions without query entries" are parsed OK.
73+
$this->assertEquals('mysqld, Version: 10.7.1-MariaDB-1:10.7.1+maria~focal-log (mariadb.org binary distribution)', $sessions[7]->Server);
74+
$this->assertEquals('mysqld, Version: 10.7.1-MariaDB-1:10.7.1+maria~focal-log (mariadb.org binary distribution)', $sessions[8]->Server);
75+
$this->assertEquals('Unix socket: /run/mysqld/mysqld.sock', $sessions[8]->Transport);
76+
// See comments in logfile for why the query is not found in the first/third
77+
// entry (backward compatible style parsing). The comments are swept up into
78+
// a seventh fake entry.
79+
$this->assertCount(7, $entries);
80+
$this->assertEmpty($entries[0]->Query);
81+
$this->assertNotEmpty($entries[1]->Query);
82+
$this->assertEmpty($entries[2]->Query);
83+
$this->assertNotEmpty($entries[3]->Query);
84+
$this->assertNotEmpty($entries[4]->Query);
85+
$this->assertNotEmpty($entries[5]->Query);
86+
// Done on Mariadb only:
87+
// Comments above "Time: " are parsed:
88+
$this->assertEquals('0.001519', $entries[4]->Query_time);
89+
// Extra properties:
90+
$this->assertEquals('1', $entries[4]->Rows_affected);
91+
// "Time: 220907 18:55:33" is reformatted to be the same as mysql:
92+
$this->assertEquals('2022-09-07T18:55:33.000000Z', $entries[4]->Time);
93+
// Time is copied into next entry if not present in comment header:
94+
$this->assertEquals('2022-09-07T18:55:33.000000Z', $entries[5]->Time);
95+
}
96+
5797
public function testSingleSession() : void
5898
{
5999
$parser = new \PHPFUI\MySQLSlowQuery\Parser(__DIR__ . '/logs/singleSession.log');
@@ -120,44 +160,4 @@ public function testTripleSession() : void
120160
$this->assertEquals('0', $entry->Rows_sent);
121161
$this->assertEquals('6', $entry->Rows_examined);
122162
}
123-
124-
public function testMysqlVsMariadb() : void
125-
{
126-
$parser = new \PHPFUI\MySQLSlowQuery\Parser(__DIR__ . '/logs/ignoredData.log');
127-
$sessions = $parser->getSessions();
128-
$entries = $parser->getEntries();
129-
$this->assertCount(9, $sessions);
130-
// See comments in logfile for why session 3-6 are buggy and 7-9 are not.
131-
$this->assertEquals('c:\wamp64\bin\mysql\mysql8.0.21\bin\mysqld.exe, Version: 8.0.21 (MySQL Community Server - GPL)', $sessions[0]->Server);
132-
$this->assertEquals('c:\wamp64\bin\mysql\mysql8.0.21\bin\mysqld.exe, Version: 8.0.21 (MySQL Community Server - GPL)', $sessions[1]->Server);
133-
$this->assertEquals('Tcp Port: 3306, Named Pipe: /tmp/mysql.sock', $sessions[2]->Server);
134-
$this->assertEquals('Tcp port: 0 Unix socket: /run/mysqld/mysqld.sock', $sessions[3]->Server);
135-
$this->assertEquals('Tcp port: 0 Unix socket: /run/mysqld/mysqld.sock', $sessions[4]->Server);
136-
$this->assertEquals('Tcp port: 0 Unix socket: /run/mysqld/mysqld.sock', $sessions[5]->Server);
137-
$this->assertEquals('mysqld, Version: 10.7.1-MariaDB-1:10.7.1+maria~focal-log (mariadb.org binary distribution)', $sessions[6]->Server);
138-
// It took until the previous session for mariadb to kick in, but going
139-
// forward "sessions without query entries" are parsed OK.
140-
$this->assertEquals('mysqld, Version: 10.7.1-MariaDB-1:10.7.1+maria~focal-log (mariadb.org binary distribution)', $sessions[7]->Server);
141-
$this->assertEquals('mysqld, Version: 10.7.1-MariaDB-1:10.7.1+maria~focal-log (mariadb.org binary distribution)', $sessions[8]->Server);
142-
$this->assertEquals('Unix socket: /run/mysqld/mysqld.sock', $sessions[8]->Transport);
143-
// See comments in logfile for why the query is not found in the first/third
144-
// entry (backward compatible style parsing). The comments are swept up into
145-
// a seventh fake entry.
146-
$this->assertCount(7, $entries);
147-
$this->assertEmpty($entries[0]->Query);
148-
$this->assertNotEmpty($entries[1]->Query);
149-
$this->assertEmpty($entries[2]->Query);
150-
$this->assertNotEmpty($entries[3]->Query);
151-
$this->assertNotEmpty($entries[4]->Query);
152-
$this->assertNotEmpty($entries[5]->Query);
153-
// Done on Mariadb only:
154-
// Comments above "Time: " are parsed:
155-
$this->assertEquals('0.001519', $entries[4]->Query_time);
156-
// Extra properties:
157-
$this->assertEquals('1', $entries[4]->Rows_affected);
158-
// "Time: 220907 18:55:33" is reformatted to be the same as mysql:
159-
$this->assertEquals('2022-09-07T18:55:33.000000Z', $entries[4]->Time);
160-
// Time is copied into next entry if not present in comment header:
161-
$this->assertEquals('2022-09-07T18:55:33.000000Z', $entries[5]->Time);
162-
}
163163
}

0 commit comments

Comments
 (0)