Skip to content

Commit fc41aee

Browse files
committed
Create mysqli_real_connect_compression.phpt
1 parent 5a12b78 commit fc41aee

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
MYSQLI_CLIENT_COMPRESS compresses the data in both directions
3+
--EXTENSIONS--
4+
mysqli
5+
--SKIPIF--
6+
<?php
7+
require_once 'skipifconnectfailure.inc';
8+
ob_start();
9+
(new ReflectionExtension('mysqlnd'))->info();
10+
if (!str_contains(ob_get_clean(), 'Compression => supported')) {
11+
die('skip mysqlnd is built without compression support');
12+
}
13+
?>
14+
--FILE--
15+
<?php
16+
require_once 'connect.inc';
17+
18+
function server_bytes(mysqli $link): array
19+
{
20+
$result = $link->query("SHOW SESSION STATUS WHERE Variable_name IN ('Bytes_received', 'Bytes_sent')");
21+
return array_map(intval(...), array_column($result->fetch_all(), 1, 0));
22+
}
23+
24+
$size = 100000;
25+
26+
foreach ([0 => 'Uncompressed', MYSQLI_CLIENT_COMPRESS => 'Compressed'] as $flags => $label) {
27+
$link = mysqli_init();
28+
if (!my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags, false)) {
29+
printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
30+
continue;
31+
}
32+
33+
$before = server_bytes($link);
34+
$link->query("DO '" . str_repeat('x', $size) . "'");
35+
$after_send = server_bytes($link);
36+
$row = $link->query("SELECT REPEAT('x', $size)")->fetch_row();
37+
$after_receive = server_bytes($link);
38+
39+
$client_to_server = $after_send['Bytes_received'] - $before['Bytes_received'];
40+
$server_to_client = $after_receive['Bytes_sent'] - $after_send['Bytes_sent'];
41+
42+
echo "$label:\n";
43+
printf(" client to server is compressed: %s\n", $client_to_server < $size / 10 ? 'yes' : 'no');
44+
printf(" server to client is compressed: %s\n", $server_to_client < $size / 10 ? 'yes' : 'no');
45+
printf(" result is correct: %s\n", $row[0] === str_repeat('x', $size) ? 'yes' : 'no');
46+
}
47+
?>
48+
--EXPECT--
49+
Uncompressed:
50+
client to server is compressed: no
51+
server to client is compressed: no
52+
result is correct: yes
53+
Compressed:
54+
client to server is compressed: yes
55+
server to client is compressed: yes
56+
result is correct: yes

0 commit comments

Comments
 (0)