Skip to content

Commit

Permalink
1. Changed name to protocol_compression_level.
Browse files Browse the repository at this point in the history
2. Added TAP test
  • Loading branch information
yashwantsahu20 committed Nov 29, 2024
1 parent a846447 commit a0db6c3
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 12 deletions.
2 changes: 1 addition & 1 deletion include/MySQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ class MySQL_Threads_Handler
bool parse_failure_logs_digest;
bool default_reconnect;
bool have_compress;
int compression_level;
int protocol_compression_level;
bool have_ssl;
bool multiplexing;
// bool stmt_multiplexing;
Expand Down
4 changes: 2 additions & 2 deletions include/proxysql_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ __thread int mysql_thread___poll_timeout;
__thread int mysql_thread___poll_timeout_on_failure;
__thread bool mysql_thread___connection_warming;
__thread bool mysql_thread___have_compress;
__thread int mysql_thread___compression_level;
__thread int mysql_thread___protocol_compression_level;
__thread bool mysql_thread___have_ssl;
__thread bool mysql_thread___multiplexing;
__thread bool mysql_thread___log_unhealthy_connections;
Expand Down Expand Up @@ -1464,7 +1464,7 @@ extern __thread int mysql_thread___poll_timeout;
extern __thread int mysql_thread___poll_timeout_on_failure;
extern __thread bool mysql_thread___connection_warming;
extern __thread bool mysql_thread___have_compress;
extern __thread int mysql_thread___compression_level;
extern __thread int mysql_thread___protocol_compression_level;
extern __thread bool mysql_thread___have_ssl;
extern __thread bool mysql_thread___multiplexing;
extern __thread bool mysql_thread___log_unhealthy_connections;
Expand Down
10 changes: 5 additions & 5 deletions lib/MySQL_Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ static char * mysql_thread_variables_names[]= {
(char *)"handle_warnings",
(char *)"evaluate_replication_lag_on_servers_load",
(char *)"proxy_protocol_networks",
(char *)"compression_level",
(char *)"protocol_compression_level",
NULL
};

Expand Down Expand Up @@ -1138,7 +1138,7 @@ MySQL_Threads_Handler::MySQL_Threads_Handler() {
variables.enable_load_data_local_infile=false;
variables.log_mysql_warnings_enabled=false;
variables.data_packets_history_size=0;
variables.compression_level=3;
variables.protocol_compression_level=3;
// status variables
status_variables.mirror_sessions_current=0;
__global_MySQL_Thread_Variables_version=1;
Expand Down Expand Up @@ -2266,7 +2266,7 @@ char ** MySQL_Threads_Handler::get_variables_list() {
VariablesPointers_int["client_host_error_counts"] = make_tuple(&variables.client_host_error_counts, 0, 1024*1024, false);
VariablesPointers_int["handle_warnings"] = make_tuple(&variables.handle_warnings, 0, 1, false);
VariablesPointers_int["evaluate_replication_lag_on_servers_load"] = make_tuple(&variables.evaluate_replication_lag_on_servers_load, 0, 1, false);
VariablesPointers_int["compression_level"] = make_tuple(&variables.compression_level, 0, 9, false);
VariablesPointers_int["protocol_compression_level"] = make_tuple(&variables.protocol_compression_level, 0, 9, false);

// logs
VariablesPointers_int["auditlog_filesize"] = make_tuple(&variables.auditlog_filesize, 1024*1024, 1*1024*1024*1024, false);
Expand Down Expand Up @@ -4183,7 +4183,7 @@ void MySQL_Thread::refresh_variables() {
REFRESH_VARIABLE_INT(poll_timeout);
REFRESH_VARIABLE_INT(poll_timeout_on_failure);
REFRESH_VARIABLE_BOOL(have_compress);
REFRESH_VARIABLE_INT(compression_level);
REFRESH_VARIABLE_INT(protocol_compression_level);
REFRESH_VARIABLE_BOOL(have_ssl);
REFRESH_VARIABLE_BOOL(multiplexing);
REFRESH_VARIABLE_BOOL(log_unhealthy_connections);
Expand Down Expand Up @@ -4268,7 +4268,7 @@ MySQL_Thread::MySQL_Thread() {
mysql_thread___ssl_p2s_crl=NULL;
mysql_thread___ssl_p2s_crlpath=NULL;

mysql_thread___compression_level=3;
mysql_thread___protocol_compression_level=3;

last_maintenance_time=0;
last_move_to_idle_thread_time=0;
Expand Down
6 changes: 3 additions & 3 deletions lib/mysql_data_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ void MySQL_Data_Stream::generate_compressed_packet() {
total_size+=p2.size;
l_free(p2.size,p2.ptr);
}
int rc=compress2(dest, &destLen, source, sourceLen, GloMTH->variables.compression_level);
int rc=compress2(dest, &destLen, source, sourceLen, GloMTH->variables.protocol_compression_level);
assert(rc==Z_OK);
l_free(total_size, source);
queueOUT.pkt.size=destLen+7;
Expand Down Expand Up @@ -1353,9 +1353,9 @@ void MySQL_Data_Stream::generate_compressed_packet() {
dest1=(Bytef *)malloc(destLen1+7);
destLen2=len2*120/100+12;
dest2=(Bytef *)malloc(destLen2+7);
rc=compress2(dest1+7, &destLen1, (const unsigned char *)p2.ptr, len1, GloMTH->variables.compression_level);
rc=compress2(dest1+7, &destLen1, (const unsigned char *)p2.ptr, len1, GloMTH->variables.protocol_compression_level);
assert(rc==Z_OK);
rc=compress2(dest2+7, &destLen2, (const unsigned char *)p2.ptr+len1, len2, GloMTH->variables.compression_level);
rc=compress2(dest2+7, &destLen2, (const unsigned char *)p2.ptr+len1, len2, GloMTH->variables.protocol_compression_level);
assert(rc==Z_OK);

hdr.pkt_length=destLen1;
Expand Down
3 changes: 2 additions & 1 deletion test/tap/groups/groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,6 @@
"eof_fast_forward-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ],
"eof_mixed_flags_queries-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ],
"eof_packet_mixed_queries-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ],
"ok_packet_mixed_queries-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ]
"ok_packet_mixed_queries-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ],
"mysql-protocol_compression_level-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ]
}
153 changes: 153 additions & 0 deletions test/tap/tests/mysql-protocol_compression_level-t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>

#include <string>
#include <sstream>
#include <fstream>
#include "mysql.h"

#include "tap.h"
#include "command_line.h"
#include "utils.h"

inline unsigned long long monotonic_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
}

unsigned long calculate_query_execution_time(MYSQL* mysql, const std::string& query) {
MYSQL_RES *res;
MYSQL_ROW row;
unsigned long long begin = monotonic_time();
unsigned long long row_count = 0;
MYSQL_QUERY(mysql, query.c_str());
res = mysql_use_result(mysql);
unsigned long long num_rows = mysql_num_rows(res);
unsigned int num_fields = mysql_num_fields(res);
while ((row = mysql_fetch_row(res))) {
for (unsigned int i = 1; i < num_fields; i++) {
char *field = row[i];
}
row_count++;
}
mysql_free_result(res);
unsigned long long end = monotonic_time();
fprintf(stderr, "Row count: %lld\n", row_count);
return (end - begin);
}

void set_compression_level(std::string level) {
CommandLine cl;

if(cl.getEnv())
return;

MYSQL* proxysql = mysql_init(NULL);
if (!proxysql) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql));
return;
}
if (!mysql_real_connect(proxysql, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql));
return;
}
std::string query = "UPDATE global_variables SET variable_value = '" + level + "' WHERE variable_name = 'mysql-protocol_compression_level';";
mysql_query(proxysql, query.c_str());
query = "LOAD MYSQL VARIABLES TO RUNTIME;";
mysql_query(proxysql, query.c_str());
mysql_close(proxysql);
}

MYSQL* initilize_mysql_connection(char* host, char* username, char* password, int port, bool compression) {
MYSQL* mysql = mysql_init(NULL);
if (!mysql)
return nullptr;

fprintf(stderr, "Connection details: %s %s %d\n", username, password, port);
if (!mysql_real_connect(mysql, host, username, password, NULL, port, NULL, 0)) {
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(mysql));
mysql_close(mysql);
return nullptr;
}
if (compression) {
if (mysql_options(mysql, MYSQL_OPT_COMPRESS, nullptr) != 0) {
fprintf(stderr, "Failed to set mysql compression option: Error: %s\n",
mysql_error(mysql));
mysql_close(mysql);
return nullptr;
}
}
return mysql;
}

int main(int argc, char** argv) {

CommandLine cl;

if(cl.getEnv())
return exit_status();

plan(2);

// ProxySQL connection without compression
MYSQL* proxysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, false);
if (!proxysql) {
return exit_status();
}

// ProxySQL connection with compression
MYSQL* proxysql_compression = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, true);
if (!proxysql_compression) {
return exit_status();
}

// MySQL connection with compression
MYSQL* mysql_compression = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.mysql_port, true);
if (!mysql_compression) {
return exit_status();
}

MYSQL_QUERY(proxysql, "CREATE DATABASE IF NOT EXISTS test");
MYSQL_QUERY(proxysql, "DROP TABLE IF EXISTS test.sbtest1");

mysql_query(proxysql, "CREATE TABLE IF NOT EXISTS test.sbtest1 (id INT UNSIGNED NOT NULL AUTO_INCREMENT, k INT UNSIGNED NOT NULL DEFAULT 0, c CHAR(120) NOT NULL DEFAULT '', pad CHAR(60) NOT NULL DEFAULT '', PRIMARY KEY (id), KEY k_1 (k));");

MYSQL_QUERY(proxysql, "USE test");

for (int i = 0; i < 1000; i++) {
MYSQL_QUERY(proxysql, "INSERT INTO sbtest1 (k, c, pad) SELECT FLOOR(RAND() * 10000), REPEAT('a', 120), REPEAT('b', 60) FROM information_schema.tables LIMIT 1000;");
}

std::string query = "SELECT t1.id id1, t1.k k1, t1.c c1, t1.pad pad1, t2.id id2, t2.k k2, t2.c c2, t2.pad pad2 FROM test.sbtest1 t1 JOIN test.sbtest1 t2 LIMIT 50000000";

unsigned long time_proxy = calculate_query_execution_time(proxysql, query);
diag("Time taken for query with proxysql without compression: %ld", time_proxy);

unsigned long time_proxy_compressed = calculate_query_execution_time(proxysql_compression, query);
diag("Time taken for query with proxysql with compression: %ld", time_proxy_compressed);

unsigned long diff = abs(time_proxy - time_proxy_compressed);
int performance_diff = (diff * 100) / time_proxy;

bool accepted_performance = (performance_diff < 10) ? true : false;
ok((performance_diff < 20), "proxysql with compression performed well compared to without compression. Performance difference: %d percentage", performance_diff);

unsigned long time_mysql_compressed = calculate_query_execution_time(mysql_compression, query);
diag("Time taken for query with mysql with compression: %ld", time_mysql_compressed);

diff = abs(time_mysql_compressed - time_proxy_compressed);
performance_diff = (diff * 100) / time_mysql_compressed;

accepted_performance = (performance_diff < 20) ? true : false;
ok((performance_diff < 10), "proxysql with compression performed well compared to mysql with compression. Performance difference: %d percentage", performance_diff);

mysql_close(proxysql);
mysql_close(proxysql_compression);
mysql_close(mysql_compression);

return exit_status();
}

0 comments on commit a0db6c3

Please sign in to comment.