mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-78212 dml: Added MYSQLI_CLIENT_COMPRESS option for mysqli
Config dboptions key 'clientcompress'. Decreases traffic between the application server and the database host.
- Loading branch information
1 parent
1b1a15a
commit 9fcba75
Showing
3 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |