From a846447a2245c12172853a088176541987f56302 Mon Sep 17 00:00:00 2001 From: Yashwant Sahu Date: Wed, 27 Nov 2024 13:28:37 +0530 Subject: [PATCH 1/7] Added MySQL compression level --- include/MySQL_Thread.h | 1 + include/proxysql_structs.h | 2 ++ lib/MySQL_Thread.cpp | 6 ++++++ lib/mysql_data_stream.cpp | 6 +++--- test/tap/tests/proxysql_reference_select_config_file.cnf | 1 + 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index b3d1b83649..837767ea03 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -470,6 +470,7 @@ class MySQL_Threads_Handler bool parse_failure_logs_digest; bool default_reconnect; bool have_compress; + int compression_level; bool have_ssl; bool multiplexing; // bool stmt_multiplexing; diff --git a/include/proxysql_structs.h b/include/proxysql_structs.h index 51fb41bb20..ea7b7ec8fd 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -1167,6 +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 bool mysql_thread___have_ssl; __thread bool mysql_thread___multiplexing; __thread bool mysql_thread___log_unhealthy_connections; @@ -1463,6 +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 bool mysql_thread___have_ssl; extern __thread bool mysql_thread___multiplexing; extern __thread bool mysql_thread___log_unhealthy_connections; diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 5fca5cf23b..140cc1661c 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -504,6 +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", NULL }; @@ -1137,6 +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; // status variables status_variables.mirror_sessions_current=0; __global_MySQL_Thread_Variables_version=1; @@ -2264,6 +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); // logs VariablesPointers_int["auditlog_filesize"] = make_tuple(&variables.auditlog_filesize, 1024*1024, 1*1024*1024*1024, false); @@ -4180,6 +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_BOOL(have_ssl); REFRESH_VARIABLE_BOOL(multiplexing); REFRESH_VARIABLE_BOOL(log_unhealthy_connections); @@ -4264,6 +4268,8 @@ MySQL_Thread::MySQL_Thread() { mysql_thread___ssl_p2s_crl=NULL; mysql_thread___ssl_p2s_crlpath=NULL; + mysql_thread___compression_level=3; + last_maintenance_time=0; last_move_to_idle_thread_time=0; maintenance_loop=true; diff --git a/lib/mysql_data_stream.cpp b/lib/mysql_data_stream.cpp index 594bc617fc..c4008a4c2b 100644 --- a/lib/mysql_data_stream.cpp +++ b/lib/mysql_data_stream.cpp @@ -1321,7 +1321,7 @@ void MySQL_Data_Stream::generate_compressed_packet() { total_size+=p2.size; l_free(p2.size,p2.ptr); } - int rc=compress(dest, &destLen, source, sourceLen); + int rc=compress2(dest, &destLen, source, sourceLen, GloMTH->variables.compression_level); assert(rc==Z_OK); l_free(total_size, source); queueOUT.pkt.size=destLen+7; @@ -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=compress(dest1+7, &destLen1, (const unsigned char *)p2.ptr, len1); + rc=compress2(dest1+7, &destLen1, (const unsigned char *)p2.ptr, len1, GloMTH->variables.compression_level); assert(rc==Z_OK); - rc=compress(dest2+7, &destLen2, (const unsigned char *)p2.ptr+len1, len2); + rc=compress2(dest2+7, &destLen2, (const unsigned char *)p2.ptr+len1, len2, GloMTH->variables.compression_level); assert(rc==Z_OK); hdr.pkt_length=destLen1; diff --git a/test/tap/tests/proxysql_reference_select_config_file.cnf b/test/tap/tests/proxysql_reference_select_config_file.cnf index 811fe356aa..8aef5bad63 100644 --- a/test/tap/tests/proxysql_reference_select_config_file.cnf +++ b/test/tap/tests/proxysql_reference_select_config_file.cnf @@ -222,6 +222,7 @@ mysql_variables = use_tcp_keepalive="mysql" verbose_query_error="mysql" wait_timeout="mysql" + compression_level="mysql" } mysql_users: ( From a0db6c3a1f2cdc23aa1bf110adb85092ae9bdfe8 Mon Sep 17 00:00:00 2001 From: Yashwant Sahu Date: Fri, 29 Nov 2024 13:09:26 +0530 Subject: [PATCH 2/7] 1. Changed name to protocol_compression_level. 2. Added TAP test --- include/MySQL_Thread.h | 2 +- include/proxysql_structs.h | 4 +- lib/MySQL_Thread.cpp | 10 +- lib/mysql_data_stream.cpp | 6 +- test/tap/groups/groups.json | 3 +- .../mysql-protocol_compression_level-t.cpp | 153 ++++++++++++++++++ 6 files changed, 166 insertions(+), 12 deletions(-) create mode 100644 test/tap/tests/mysql-protocol_compression_level-t.cpp diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 837767ea03..c62f4154a2 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -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; diff --git a/include/proxysql_structs.h b/include/proxysql_structs.h index ea7b7ec8fd..69c1e0586d 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -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; @@ -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; diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 140cc1661c..e79a19fb0f 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -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 }; @@ -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; @@ -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); @@ -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); @@ -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; diff --git a/lib/mysql_data_stream.cpp b/lib/mysql_data_stream.cpp index c4008a4c2b..f51df38e02 100644 --- a/lib/mysql_data_stream.cpp +++ b/lib/mysql_data_stream.cpp @@ -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; @@ -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; diff --git a/test/tap/groups/groups.json b/test/tap/groups/groups.json index 64c1fa8d55..9661cba497 100644 --- a/test/tap/groups/groups.json +++ b/test/tap/groups/groups.json @@ -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" ] } diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp new file mode 100644 index 0000000000..6a6103c8e4 --- /dev/null +++ b/test/tap/tests/mysql-protocol_compression_level-t.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include + +#include +#include +#include +#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(); +} From 34ef47c2990d82c4a407fa68f1cbe2499da3a4a5 Mon Sep 17 00:00:00 2001 From: Yashwant Sahu Date: Fri, 29 Nov 2024 15:56:00 +0530 Subject: [PATCH 3/7] Added test for compression level variable --- .../mysql-protocol_compression_level-t.cpp | 81 +++++++++++++------ 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp index 6a6103c8e4..3e6520819e 100644 --- a/test/tap/tests/mysql-protocol_compression_level-t.cpp +++ b/test/tap/tests/mysql-protocol_compression_level-t.cpp @@ -39,26 +39,38 @@ unsigned long calculate_query_execution_time(MYSQL* mysql, const std::string& qu return (end - begin); } -void set_compression_level(std::string level) { - CommandLine cl; - - if(cl.getEnv()) - return; +void set_compression_level(MYSQL* proxysql_admin, std::string level) { + std::string query = "UPDATE global_variables SET variable_value = '" + level + "' WHERE variable_name = 'mysql-protocol_compression_level';"; + mysql_query(proxysql_admin, query.c_str()); + query = "LOAD MYSQL VARIABLES TO RUNTIME;"; + mysql_query(proxysql_admin, query.c_str()); +} - MYSQL* proxysql = mysql_init(NULL); - if (!proxysql) { - fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql)); - return; +int32_t get_compression_level(MYSQL* proxysql_admin) { + int32_t compression_level = -1; + const uint32_t SERVERS_COUNT = 10; + + int err = mysql_query(proxysql_admin, "SELECT * FROM global_variables WHERE variable_name='mysql-protocol_compression_level'"); + if (err != EXIT_SUCCESS) { + diag( + "Query for retrieving value of 'mysql-protocol_compression_level' failed with error: (%d, %s)", + mysql_errno(proxysql_admin), mysql_error(proxysql_admin) + ); + return compression_level; } - 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; + + MYSQL_RES* res = mysql_store_result(proxysql_admin); + if (res != nullptr) { + int num_rows = mysql_num_rows(res); + MYSQL_ROW row = mysql_fetch_row(res); + + if (num_rows && row != nullptr) { + char* endptr = nullptr; + compression_level = strtol(row[1], &endptr, SERVERS_COUNT); + } } - 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_free_result(res); + return compression_level; } MYSQL* initilize_mysql_connection(char* host, char* username, char* password, int port, bool compression) { @@ -66,7 +78,7 @@ MYSQL* initilize_mysql_connection(char* host, char* username, char* password, in if (!mysql) return nullptr; - fprintf(stderr, "Connection details: %s %s %d\n", username, password, port); + fprintf(stderr, "MySQL 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)); @@ -91,7 +103,7 @@ int main(int argc, char** argv) { if(cl.getEnv()) return exit_status(); - plan(2); + plan(5); // ProxySQL connection without compression MYSQL* proxysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, false); @@ -111,6 +123,12 @@ int main(int argc, char** argv) { return exit_status(); } + // ProxySQL admin connection + MYSQL* proxysql_admin = initilize_mysql_connection(cl.host, cl.admin_username, cl.admin_password, cl.admin_port, false); + if (!proxysql_admin) { + return exit_status(); + } + MYSQL_QUERY(proxysql, "CREATE DATABASE IF NOT EXISTS test"); MYSQL_QUERY(proxysql, "DROP TABLE IF EXISTS test.sbtest1"); @@ -118,11 +136,11 @@ int main(int argc, char** argv) { MYSQL_QUERY(proxysql, "USE test"); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < 100; 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"; + 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 90000000"; unsigned long time_proxy = calculate_query_execution_time(proxysql, query); diag("Time taken for query with proxysql without compression: %ld", time_proxy); @@ -133,8 +151,7 @@ int main(int argc, char** argv) { 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); + ok((performance_diff < 10), "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); @@ -142,12 +159,26 @@ int main(int argc, char** argv) { 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); + ok((performance_diff < 20), "proxysql with compression performed well compared to mysql with compression. Performance difference: %d percentage", performance_diff); + + int32_t compression_level = get_compression_level(proxysql_admin); + ok(compression_level == 3, "Default compression level is correct: %d", compression_level); + + set_compression_level(proxysql_admin, "8"); + compression_level = get_compression_level(proxysql_admin); + ok(compression_level == 8, "Compression level set correctly: %d", compression_level); + + time_proxy_compressed = calculate_query_execution_time(proxysql_compression, query); + diag("Time taken for query with proxysql with compression level 8: %ld", time_proxy_compressed); + + set_compression_level(proxysql_admin, "3"); + compression_level = get_compression_level(proxysql_admin); + ok(compression_level == 3, "Compression level set correctly: %d", compression_level); mysql_close(proxysql); mysql_close(proxysql_compression); mysql_close(mysql_compression); + mysql_close(proxysql_admin); return exit_status(); } From 4a17a9a76d9c4fb2bbce5c2eb222e229723e261f Mon Sep 17 00:00:00 2001 From: Yashwant Sahu Date: Tue, 10 Dec 2024 17:10:34 +0530 Subject: [PATCH 4/7] Changes done: 1. Moved monotonic_time to util 2. Now calculate_query_execution_time will return -1 incase of error. 3. Using util function for setting and getting compression variable values. 4. Added mysql_query_rules --- microbench/PR1977_bench.cpp | 7 - test/tap/tap/utils.cpp | 6 + test/tap/tap/utils.h | 2 + .../tests/multiple_prepared_statements-t.cpp | 6 - test/tap/tests/mysql-init_connect-1-t.cpp | 6 - test/tap/tests/mysql-init_connect-2-t.cpp | 6 - test/tap/tests/mysql-last_insert_id-t.cpp | 6 - .../mysql-protocol_compression_level-t.cpp | 168 ++++++++++-------- test/tap/tests/mysql-test_ssl_CA-t.cpp | 6 - .../tests/reg_test_3434-text_stmt_mix-t.cpp | 6 - test/tap/tests/savepoint-3749-t.cpp | 8 - test/tap/tests/set_testing-240.h | 7 - test/tap/tests/set_testing.h | 7 - test/tap/tests/test_query_rules_routing-t.cpp | 6 - test/tap/tests/test_query_timeout-t.cpp | 6 - ...test_sqlite3_server_and_fast_routing-t.cpp | 6 - ...ottle_max_bytes_per_second_to_client-t.cpp | 7 - 17 files changed, 106 insertions(+), 160 deletions(-) diff --git a/microbench/PR1977_bench.cpp b/microbench/PR1977_bench.cpp index 6efae45072..5a7f4c13ba 100644 --- a/microbench/PR1977_bench.cpp +++ b/microbench/PR1977_bench.cpp @@ -9,13 +9,6 @@ inline int fastrand() { return (g_seed>>16)&0x7FFF; } -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); -} - - #define NSRV 24 #define NLOOP 10000000 diff --git a/test/tap/tap/utils.cpp b/test/tap/tap/utils.cpp index 6b5865f1bf..a154110338 100644 --- a/test/tap/tap/utils.cpp +++ b/test/tap/tap/utils.cpp @@ -399,6 +399,12 @@ int create_table_test_sbtest1(int num_rows, MYSQL *mysql) { return add_more_rows_test_sbtest1(num_rows, mysql); } +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); +} + int create_table_test_sqlite_sbtest1(int num_rows, MYSQL *mysql) { MYSQL_QUERY(mysql, "DROP TABLE IF EXISTS sbtest1"); MYSQL_QUERY(mysql, "CREATE TABLE IF NOT EXISTS sbtest1 (id INTEGER PRIMARY KEY AUTOINCREMENT, `k` int(10) NOT NULL DEFAULT '0', `c` char(120) NOT NULL DEFAULT '', `pad` char(60) NOT NULL DEFAULT '')"); diff --git a/test/tap/tap/utils.h b/test/tap/tap/utils.h index e2b71a4a94..29eeb4c73f 100644 --- a/test/tap/tap/utils.h +++ b/test/tap/tap/utils.h @@ -144,6 +144,8 @@ int create_table_test_sbtest1(int num_rows, MYSQL *mysql); int create_table_test_sqlite_sbtest1(int num_rows, MYSQL *mysql); // as above, but for SQLite3 server int add_more_rows_test_sbtest1(int num_rows, MYSQL *mysql, bool sqlite=false); +unsigned long long monotonic_time(); + using mysql_res_row = std::vector; /** diff --git a/test/tap/tests/multiple_prepared_statements-t.cpp b/test/tap/tests/multiple_prepared_statements-t.cpp index d88fad720b..209de605bb 100644 --- a/test/tap/tests/multiple_prepared_statements-t.cpp +++ b/test/tap/tests/multiple_prepared_statements-t.cpp @@ -36,12 +36,6 @@ inline int fastrand() { return (g_seed>>16)&0x7FFF; } -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); -} - #define NTHREADS 5 #define NCONNS 6 #define NPREP 15000 diff --git a/test/tap/tests/mysql-init_connect-1-t.cpp b/test/tap/tests/mysql-init_connect-1-t.cpp index 65dc2202b4..18561c429b 100644 --- a/test/tap/tests/mysql-init_connect-1-t.cpp +++ b/test/tap/tests/mysql-init_connect-1-t.cpp @@ -17,12 +17,6 @@ It uses 2 valid init_connect, and 2 invalid ones that trigger PMC-10003. It also sets a value that causes a syntax error */ -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); -} - int main(int argc, char** argv) { CommandLine cl; diff --git a/test/tap/tests/mysql-init_connect-2-t.cpp b/test/tap/tests/mysql-init_connect-2-t.cpp index 705869c70c..63d9e27d98 100644 --- a/test/tap/tests/mysql-init_connect-2-t.cpp +++ b/test/tap/tests/mysql-init_connect-2-t.cpp @@ -21,12 +21,6 @@ mysql-init_connect We configure both hostgroup 0 and 1 */ -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); -} - int main(int argc, char** argv) { CommandLine cl; diff --git a/test/tap/tests/mysql-last_insert_id-t.cpp b/test/tap/tests/mysql-last_insert_id-t.cpp index 90549afa4c..660f1264d9 100644 --- a/test/tap/tests/mysql-last_insert_id-t.cpp +++ b/test/tap/tests/mysql-last_insert_id-t.cpp @@ -11,12 +11,6 @@ #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); -} - std::string queries[5] = { "SELECT LAST_INSERT_ID() LIMIT 1", diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp index 3e6520819e..3f479498dc 100644 --- a/test/tap/tests/mysql-protocol_compression_level-t.cpp +++ b/test/tap/tests/mysql-protocol_compression_level-t.cpp @@ -12,18 +12,19 @@ #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()); + unsigned long ret_query = 0; + + ret_query = mysql_query(mysql, query.c_str()); + if (ret_query != 0) { + fprintf(stderr, "Failed to execute query: Error: %s\n", mysql_error(mysql)); + return -1; + } + res = mysql_use_result(mysql); unsigned long long num_rows = mysql_num_rows(res); unsigned int num_fields = mysql_num_fields(res); @@ -39,40 +40,6 @@ unsigned long calculate_query_execution_time(MYSQL* mysql, const std::string& qu return (end - begin); } -void set_compression_level(MYSQL* proxysql_admin, std::string level) { - std::string query = "UPDATE global_variables SET variable_value = '" + level + "' WHERE variable_name = 'mysql-protocol_compression_level';"; - mysql_query(proxysql_admin, query.c_str()); - query = "LOAD MYSQL VARIABLES TO RUNTIME;"; - mysql_query(proxysql_admin, query.c_str()); -} - -int32_t get_compression_level(MYSQL* proxysql_admin) { - int32_t compression_level = -1; - const uint32_t SERVERS_COUNT = 10; - - int err = mysql_query(proxysql_admin, "SELECT * FROM global_variables WHERE variable_name='mysql-protocol_compression_level'"); - if (err != EXIT_SUCCESS) { - diag( - "Query for retrieving value of 'mysql-protocol_compression_level' failed with error: (%d, %s)", - mysql_errno(proxysql_admin), mysql_error(proxysql_admin) - ); - return compression_level; - } - - MYSQL_RES* res = mysql_store_result(proxysql_admin); - if (res != nullptr) { - int num_rows = mysql_num_rows(res); - MYSQL_ROW row = mysql_fetch_row(res); - - if (num_rows && row != nullptr) { - char* endptr = nullptr; - compression_level = strtol(row[1], &endptr, SERVERS_COUNT); - } - } - mysql_free_result(res); - return compression_level; -} - MYSQL* initilize_mysql_connection(char* host, char* username, char* password, int port, bool compression) { MYSQL* mysql = mysql_init(NULL); if (!mysql) @@ -82,14 +49,12 @@ MYSQL* initilize_mysql_connection(char* host, char* username, char* password, in 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; } } @@ -99,6 +64,18 @@ MYSQL* initilize_mysql_connection(char* host, char* username, char* password, in int main(int argc, char** argv) { CommandLine cl; + 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 90000000"; + unsigned long time_proxy = 0; + unsigned long time_proxy_compressed = 0; + unsigned long diff = 0; + unsigned long time_mysql_compressed = 0; + std::string compression_level = {""}; + int32_t ret = 0; + MYSQL* proxysql = nullptr; + MYSQL* proxysql_compression = nullptr; + MYSQL* proxysql_admin = nullptr; + MYSQL* mysql_compression = nullptr; + int performance_diff = 0; if(cl.getEnv()) return exit_status(); @@ -106,29 +83,33 @@ int main(int argc, char** argv) { plan(5); // ProxySQL connection without compression - MYSQL* proxysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, false); + proxysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, false); if (!proxysql) { - return exit_status(); + goto cleanup; } // ProxySQL connection with compression - MYSQL* proxysql_compression = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, true); + proxysql_compression = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, true); if (!proxysql_compression) { - return exit_status(); + goto cleanup; } // MySQL connection with compression - MYSQL* mysql_compression = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.mysql_port, true); + mysql_compression = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.mysql_port, true); if (!mysql_compression) { - return exit_status(); + goto cleanup; } // ProxySQL admin connection - MYSQL* proxysql_admin = initilize_mysql_connection(cl.host, cl.admin_username, cl.admin_password, cl.admin_port, false); + proxysql_admin = initilize_mysql_connection(cl.host, cl.admin_username, cl.admin_password, cl.admin_port, false); if (!proxysql_admin) { - return exit_status(); + goto cleanup; } + // Change default query rules to avoid replication issues; This test only requires the default hostgroup + MYSQL_QUERY(proxysql_admin, "UPDATE mysql_query_rules SET active=0"); + MYSQL_QUERY(proxysql_admin, "LOAD MYSQL QUERY RULES TO RUNTIME"); + MYSQL_QUERY(proxysql, "CREATE DATABASE IF NOT EXISTS test"); MYSQL_QUERY(proxysql, "DROP TABLE IF EXISTS test.sbtest1"); @@ -140,45 +121,92 @@ int main(int argc, char** argv) { 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 90000000"; - - unsigned long time_proxy = calculate_query_execution_time(proxysql, query); + time_proxy = calculate_query_execution_time(proxysql, query); diag("Time taken for query with proxysql without compression: %ld", time_proxy); + if (time_proxy == -1) { + goto cleanup; + } - unsigned long time_proxy_compressed = calculate_query_execution_time(proxysql_compression, query); + time_proxy_compressed = calculate_query_execution_time(proxysql_compression, query); diag("Time taken for query with proxysql with compression: %ld", time_proxy_compressed); + if (time_proxy_compressed == -1) { + goto cleanup; + } - unsigned long diff = abs(time_proxy - time_proxy_compressed); - int performance_diff = (diff * 100) / time_proxy; + diff = abs(time_proxy - time_proxy_compressed); + performance_diff = (diff * 100) / time_proxy; ok((performance_diff < 10), "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); + time_mysql_compressed = calculate_query_execution_time(mysql_compression, query); diag("Time taken for query with mysql with compression: %ld", time_mysql_compressed); + if (time_mysql_compressed == -1) { + goto cleanup; + } diff = abs(time_mysql_compressed - time_proxy_compressed); performance_diff = (diff * 100) / time_mysql_compressed; ok((performance_diff < 20), "proxysql with compression performed well compared to mysql with compression. Performance difference: %d percentage", performance_diff); - int32_t compression_level = get_compression_level(proxysql_admin); - ok(compression_level == 3, "Default compression level is correct: %d", compression_level); + ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level, true); + if (ret == EXIT_SUCCESS) { + ok(compression_level == "3", "Default compression level is correct: %s", compression_level.c_str()); + } + else { + diag("Failed to get the default compression level."); + goto cleanup; + } - set_compression_level(proxysql_admin, "8"); - compression_level = get_compression_level(proxysql_admin); - ok(compression_level == 8, "Compression level set correctly: %d", compression_level); + set_admin_global_variable(proxysql_admin, "mysql-protocol_compression_level", "8"); + if (mysql_query(proxysql_admin, "load mysql variables to runtime")) { + diag("Failed to load mysql variables to runtime."); + goto cleanup; + } + ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level, true); + if (ret == EXIT_SUCCESS) { + ok(compression_level == "8", "Compression level is set correctly: %s", compression_level.c_str()); + } + else { + diag("Failed to set the Compression level is set correctly:"); + goto cleanup; + } time_proxy_compressed = calculate_query_execution_time(proxysql_compression, query); diag("Time taken for query with proxysql with compression level 8: %ld", time_proxy_compressed); + if (time_proxy_compressed == -1) { + goto cleanup; + } - set_compression_level(proxysql_admin, "3"); - compression_level = get_compression_level(proxysql_admin); - ok(compression_level == 3, "Compression level set correctly: %d", compression_level); + set_admin_global_variable(proxysql_admin, "mysql-protocol_compression_level", "3"); + if (mysql_query(proxysql_admin, "load mysql variables to runtime")) { + diag("Failed to load mysql variables to runtime."); + goto cleanup; + } + ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level, true); + if (ret == EXIT_SUCCESS) { + ok(compression_level == "3", "Compression level set correctly: %s", compression_level.c_str()); + } + else { + diag("Failed to set the Compression level set correctly:"); + goto cleanup; + } + +cleanup: + // Recover default query rules + if (proxysql_admin) { + MYSQL_QUERY(proxysql_admin, "UPDATE mysql_query_rules SET active=1"); + MYSQL_QUERY(proxysql_admin, "LOAD MYSQL QUERY RULES TO RUNTIME"); + } - mysql_close(proxysql); - mysql_close(proxysql_compression); - mysql_close(mysql_compression); - mysql_close(proxysql_admin); + if (proxysql) + mysql_close(proxysql); + if (proxysql_compression) + mysql_close(proxysql_compression); + if (mysql_compression) + mysql_close(mysql_compression); + if (proxysql_admin) + mysql_close(proxysql_admin); return exit_status(); } diff --git a/test/tap/tests/mysql-test_ssl_CA-t.cpp b/test/tap/tests/mysql-test_ssl_CA-t.cpp index b09d659e03..837e54b56e 100644 --- a/test/tap/tests/mysql-test_ssl_CA-t.cpp +++ b/test/tap/tests/mysql-test_ssl_CA-t.cpp @@ -19,12 +19,6 @@ This TAP test: - creates new connections */ -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); -} - int main(int argc, char** argv) { CommandLine cl; diff --git a/test/tap/tests/reg_test_3434-text_stmt_mix-t.cpp b/test/tap/tests/reg_test_3434-text_stmt_mix-t.cpp index 6fc4bdac3d..603aadb128 100644 --- a/test/tap/tests/reg_test_3434-text_stmt_mix-t.cpp +++ b/test/tap/tests/reg_test_3434-text_stmt_mix-t.cpp @@ -35,12 +35,6 @@ inline int fastrand() { return (g_seed>>16)&0x7FFF; } -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); -} - void gen_random_str(char *s, const int len) { g_seed = monotonic_time() ^ getpid() ^ pthread_self(); static const char alphanum[] = diff --git a/test/tap/tests/savepoint-3749-t.cpp b/test/tap/tests/savepoint-3749-t.cpp index e8202d900d..fa1c1ca95d 100644 --- a/test/tap/tests/savepoint-3749-t.cpp +++ b/test/tap/tests/savepoint-3749-t.cpp @@ -19,14 +19,6 @@ #include "utils.h" #include "command_line.h" - -unsigned long long monotonic_time() { - struct timespec ts; - //clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); // this is faster, but not precise - clock_gettime(CLOCK_MONOTONIC, &ts); - return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); -} - struct cpu_timer { cpu_timer() { diff --git a/test/tap/tests/set_testing-240.h b/test/tap/tests/set_testing-240.h index 5375648430..d385ef3c50 100644 --- a/test/tap/tests/set_testing-240.h +++ b/test/tap/tests/set_testing-240.h @@ -115,13 +115,6 @@ int readTestCasesJSON(const std::string& fileName) { return 1; } -unsigned long long monotonic_time() { - struct timespec ts; - //clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); // this is faster, but not precise - clock_gettime(CLOCK_MONOTONIC, &ts); - return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); -} - struct cpu_timer { cpu_timer() { diff --git a/test/tap/tests/set_testing.h b/test/tap/tests/set_testing.h index f6c8e65309..045ba84fb3 100644 --- a/test/tap/tests/set_testing.h +++ b/test/tap/tests/set_testing.h @@ -109,13 +109,6 @@ int readTestCasesJSON(const std::string& fileName) { return 1; } -unsigned long long monotonic_time() { - struct timespec ts; - //clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); // this is faster, but not precise - clock_gettime(CLOCK_MONOTONIC, &ts); - return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); -} - struct cpu_timer { cpu_timer() { diff --git a/test/tap/tests/test_query_rules_routing-t.cpp b/test/tap/tests/test_query_rules_routing-t.cpp index 792ef90ae9..a83bb78818 100644 --- a/test/tap/tests/test_query_rules_routing-t.cpp +++ b/test/tap/tests/test_query_rules_routing-t.cpp @@ -34,12 +34,6 @@ inline int fastrand() { return (g_seed>>16)&0x7FFF; } -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); -} - void gen_random_str(char *s, const int len) { g_seed = monotonic_time() ^ getpid() ^ pthread_self(); static const char alphanum[] = diff --git a/test/tap/tests/test_query_timeout-t.cpp b/test/tap/tests/test_query_timeout-t.cpp index 9f4af5f6b5..47127aab39 100644 --- a/test/tap/tests/test_query_timeout-t.cpp +++ b/test/tap/tests/test_query_timeout-t.cpp @@ -11,12 +11,6 @@ #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); -} - using std::string; int main(int argc, char** argv) { diff --git a/test/tap/tests/test_sqlite3_server_and_fast_routing-t.cpp b/test/tap/tests/test_sqlite3_server_and_fast_routing-t.cpp index c53732373d..fb8180e57b 100644 --- a/test/tap/tests/test_sqlite3_server_and_fast_routing-t.cpp +++ b/test/tap/tests/test_sqlite3_server_and_fast_routing-t.cpp @@ -24,12 +24,6 @@ const int ST = 5; #include "modules_server_test.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); -} - int benchmark_query_rules_fast_routing(CommandLine& cl, MYSQL* proxysql_admin, MYSQL* proxysql_mysql) { std::string s; std::pair host_port {}; diff --git a/test/tap/tests/test_throttle_max_bytes_per_second_to_client-t.cpp b/test/tap/tests/test_throttle_max_bytes_per_second_to_client-t.cpp index d031f3c561..73fb23f381 100644 --- a/test/tap/tests/test_throttle_max_bytes_per_second_to_client-t.cpp +++ b/test/tap/tests/test_throttle_max_bytes_per_second_to_client-t.cpp @@ -11,13 +11,6 @@ #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); -} - - int main(int argc, char** argv) { CommandLine cl; From af7d4744ee56a762166ba14fd72d2522c2d8180d Mon Sep 17 00:00:00 2001 From: Yashwant Sahu Date: Thu, 12 Dec 2024 11:36:37 +0530 Subject: [PATCH 5/7] added more checks --- .../mysql-protocol_compression_level-t.cpp | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp index 3f479498dc..b606802123 100644 --- a/test/tap/tests/mysql-protocol_compression_level-t.cpp +++ b/test/tap/tests/mysql-protocol_compression_level-t.cpp @@ -80,7 +80,7 @@ int main(int argc, char** argv) { if(cl.getEnv()) return exit_status(); - plan(5); + plan(8); // ProxySQL connection without compression proxysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, false); @@ -144,12 +144,21 @@ int main(int argc, char** argv) { goto cleanup; } - diff = abs(time_mysql_compressed - time_proxy_compressed); + diff = time_proxy_compressed - time_mysql_compressed; performance_diff = (diff * 100) / time_mysql_compressed; ok((performance_diff < 20), "proxysql with compression performed well compared to mysql with compression. Performance difference: %d percentage", performance_diff); ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level, true); + if (ret == EXIT_SUCCESS) { + ok(compression_level == "3", "Run-time default compression level is correct: %s", compression_level.c_str()); + } + else { + diag("Failed to get the default compression level."); + goto cleanup; + } + + ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level); if (ret == EXIT_SUCCESS) { ok(compression_level == "3", "Default compression level is correct: %s", compression_level.c_str()); } @@ -164,6 +173,15 @@ int main(int argc, char** argv) { goto cleanup; } ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level, true); + if (ret == EXIT_SUCCESS) { + ok(compression_level == "8", "Run-time Compression level is set correctly: %s", compression_level.c_str()); + } + else { + diag("Failed to set the Compression level is set correctly:"); + goto cleanup; + } + + ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level); if (ret == EXIT_SUCCESS) { ok(compression_level == "8", "Compression level is set correctly: %s", compression_level.c_str()); } @@ -184,6 +202,15 @@ int main(int argc, char** argv) { goto cleanup; } ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level, true); + if (ret == EXIT_SUCCESS) { + ok(compression_level == "3", "Run-time Compression level set correctly: %s", compression_level.c_str()); + } + else { + diag("Failed to set the Compression level set correctly:"); + goto cleanup; + } + + ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level); if (ret == EXIT_SUCCESS) { ok(compression_level == "3", "Compression level set correctly: %s", compression_level.c_str()); } From 292eddcd69c29006f737d2df3f12b93e108d2b78 Mon Sep 17 00:00:00 2001 From: Yashwant Sahu Date: Fri, 13 Dec 2024 09:15:10 +0530 Subject: [PATCH 6/7] Added time calculations for MySQL --- .../mysql-protocol_compression_level-t.cpp | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp index b606802123..5e364efb35 100644 --- a/test/tap/tests/mysql-protocol_compression_level-t.cpp +++ b/test/tap/tests/mysql-protocol_compression_level-t.cpp @@ -69,18 +69,20 @@ int main(int argc, char** argv) { unsigned long time_proxy_compressed = 0; unsigned long diff = 0; unsigned long time_mysql_compressed = 0; + unsigned long time_mysql_without_compressed = 0; std::string compression_level = {""}; int32_t ret = 0; MYSQL* proxysql = nullptr; MYSQL* proxysql_compression = nullptr; MYSQL* proxysql_admin = nullptr; MYSQL* mysql_compression = nullptr; + MYSQL* mysql = nullptr; int performance_diff = 0; if(cl.getEnv()) return exit_status(); - plan(8); + plan(7); // ProxySQL connection without compression proxysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, false); @@ -100,6 +102,12 @@ int main(int argc, char** argv) { goto cleanup; } + // MySQL connection without compression + mysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.mysql_port, false); + if (!mysql) { + goto cleanup; + } + // ProxySQL admin connection proxysql_admin = initilize_mysql_connection(cl.host, cl.admin_username, cl.admin_password, cl.admin_port, false); if (!proxysql_admin) { @@ -144,10 +152,16 @@ int main(int argc, char** argv) { goto cleanup; } - diff = time_proxy_compressed - time_mysql_compressed; - performance_diff = (diff * 100) / time_mysql_compressed; + time_mysql_without_compressed = calculate_query_execution_time(mysql, query); + diag("Time taken for query with mysql without compression: %ld", time_mysql_without_compressed); + if (time_mysql_without_compressed == -1) { + goto cleanup; + } + + diff = time_mysql_without_compressed - time_mysql_compressed; + performance_diff = (diff * 100) / time_mysql_without_compressed; - ok((performance_diff < 20), "proxysql with compression performed well compared to mysql with compression. Performance difference: %d percentage", performance_diff); + diag("Time difference for mysql, compression and without compression is: %d", performance_diff); ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level, true); if (ret == EXIT_SUCCESS) { @@ -232,6 +246,8 @@ int main(int argc, char** argv) { mysql_close(proxysql_compression); if (mysql_compression) mysql_close(mysql_compression); + if (mysql) + mysql_close(mysql); if (proxysql_admin) mysql_close(proxysql_admin); From bab1dbc78ac8cd5fe67a3a456841a5c0bdd9f05b Mon Sep 17 00:00:00 2001 From: Yashwant Sahu Date: Thu, 19 Dec 2024 11:22:31 +0530 Subject: [PATCH 7/7] Added tests for MySQL with and without compression. --- test/tap/tests/mysql-protocol_compression_level-t.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp index 5e364efb35..f295de712c 100644 --- a/test/tap/tests/mysql-protocol_compression_level-t.cpp +++ b/test/tap/tests/mysql-protocol_compression_level-t.cpp @@ -82,7 +82,7 @@ int main(int argc, char** argv) { if(cl.getEnv()) return exit_status(); - plan(7); + plan(8); // ProxySQL connection without compression proxysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, false); @@ -158,10 +158,10 @@ int main(int argc, char** argv) { goto cleanup; } - diff = time_mysql_without_compressed - time_mysql_compressed; + diff = abs(time_mysql_without_compressed - time_mysql_compressed); performance_diff = (diff * 100) / time_mysql_without_compressed; - diag("Time difference for mysql, compression and without compression is: %d", performance_diff); + ok((performance_diff < 10), "MySQL with compression performed well compared to without compression. Performance difference: %d percentage", performance_diff); ret = get_variable_value(proxysql_admin, "mysql-protocol_compression_level", compression_level, true); if (ret == EXIT_SUCCESS) {