mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
Merge branch 'MDL-78212-MYSQLI_CLIENT_COMPRESS' of https://github.com/catalyst/moodle
This commit is contained in:
commit
28b2c57c82
@ -91,6 +91,10 @@ $CFG->dboptions = array(
|
||||
// set to zero if you are using pg_bouncer in
|
||||
// 'transaction' mode (it is fine in 'session'
|
||||
// mode).
|
||||
// 'clientcompress' => true // Use compression protocol to communicate with the database server.
|
||||
// Decreases traffic from the database server.
|
||||
// Not needed if the databse is on the same host.
|
||||
// Currently supported only with mysqli driver.
|
||||
/*
|
||||
'connecttimeout' => null, // Set connect timeout in seconds. Not all drivers support it.
|
||||
'readonly' => [ // Set to read-only slave details, to get safe reads
|
||||
|
@ -573,11 +573,16 @@ class mysqli_native_moodle_database extends moodle_database {
|
||||
$this->mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, $this->dboptions['connecttimeout']);
|
||||
}
|
||||
|
||||
$flags = 0;
|
||||
if ($this->dboptions['clientcompress'] ?? false) {
|
||||
$flags |= MYSQLI_CLIENT_COMPRESS;
|
||||
}
|
||||
|
||||
$conn = null;
|
||||
$dberr = null;
|
||||
try {
|
||||
// real_connect() is doing things we don't expext.
|
||||
$conn = @$this->mysqli->real_connect($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);
|
||||
$conn = @$this->mysqli->real_connect($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket, $flags);
|
||||
} catch (\Exception $e) {
|
||||
$dberr = "$e";
|
||||
}
|
||||
|
109
lib/dml/tests/mysqli_native_moodle_database_test.php
Normal file
109
lib/dml/tests/mysqli_native_moodle_database_test.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core;
|
||||
|
||||
use ReflectionClass;
|
||||
use mysqli;
|
||||
use moodle_database, mysqli_native_moodle_database;
|
||||
use moodle_exception;
|
||||
|
||||
/**
|
||||
* Test specific features of the MySql dml.
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2023 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \mysqli_native_moodle_database
|
||||
*/
|
||||
class mysqli_native_moodle_database_test extends \advanced_testcase {
|
||||
|
||||
/**
|
||||
* Set up.
|
||||
*/
|
||||
public function setUp(): void {
|
||||
global $DB;
|
||||
parent::setUp();
|
||||
// Skip tests if not using Postgres.
|
||||
if (!($DB instanceof mysqli_native_moodle_database)) {
|
||||
$this->markTestSkipped('MySql-only test');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SSL connection helper.
|
||||
*
|
||||
* @param bool|null $compress
|
||||
* @return mysqli
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public function new_connection(?bool $compress = false): mysqli {
|
||||
global $DB;
|
||||
|
||||
// Open new connection.
|
||||
$cfg = $DB->export_dbconfig();
|
||||
if (!isset($cfg->dboptions)) {
|
||||
$cfg->dboptions = [];
|
||||
}
|
||||
|
||||
$cfg->dboptions['clientcompress'] = $compress;
|
||||
|
||||
// Get a separate disposable db connection handle with guaranteed 'readonly' config.
|
||||
$db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
|
||||
$db2->raw_connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
|
||||
|
||||
$reflector = new ReflectionClass($db2);
|
||||
$rp = $reflector->getProperty('mysqli');
|
||||
$rp->setAccessible(true);
|
||||
return $rp->getValue($db2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test client compression helper.
|
||||
*
|
||||
* @param mysqli $mysqli
|
||||
* @return array
|
||||
*/
|
||||
public function connection_status($mysqli): array {
|
||||
$mysqli->query("SELECT * FROM INFORMATION_SCHEMA.TABLES");
|
||||
|
||||
$stats = [];
|
||||
foreach ($mysqli->query('SHOW SESSION STATUS')->fetch_all(MYSQLI_ASSOC) as $r) {
|
||||
$stats[$r['Variable_name']] = $r['Value'];
|
||||
}
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test client compression.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_client_compression(): void {
|
||||
$mysqli = $this->new_connection();
|
||||
$stats = $this->connection_status($mysqli);
|
||||
$this->assertEquals('OFF', $stats['Compression']);
|
||||
$sent = $stats['Bytes_sent'];
|
||||
|
||||
$mysqlic = $this->new_connection(true);
|
||||
$stats = $this->connection_status($mysqlic);
|
||||
$this->assertEquals('ON', $stats['Compression']);
|
||||
$sentc = $stats['Bytes_sent'];
|
||||
|
||||
$this->assertLessThan($sent, $sentc);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user