Skip to content

Commit

Permalink
Added test for compression level variable
Browse files Browse the repository at this point in the history
  • Loading branch information
yashwantsahu20 committed Nov 29, 2024
1 parent a0db6c3 commit 34ef47c
Showing 1 changed file with 56 additions and 25 deletions.
81 changes: 56 additions & 25 deletions test/tap/tests/mysql-protocol_compression_level-t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,46 @@ 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) {
MYSQL* mysql = mysql_init(NULL);
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));
Expand All @@ -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);
Expand All @@ -111,18 +123,24 @@ 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");

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++) {
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);
Expand All @@ -133,21 +151,34 @@ 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);

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();
}

0 comments on commit 34ef47c

Please sign in to comment.