diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp index 6a6103c8e..3e6520819 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(); }