12
12
#include " command_line.h"
13
13
#include " utils.h"
14
14
15
- inline unsigned long long monotonic_time () {
16
- struct timespec ts;
17
- clock_gettime (CLOCK_MONOTONIC, &ts);
18
- return (((unsigned long long ) ts.tv_sec ) * 1000000 ) + (ts.tv_nsec / 1000 );
19
- }
20
-
21
15
unsigned long calculate_query_execution_time (MYSQL* mysql, const std::string& query) {
22
16
MYSQL_RES *res;
23
17
MYSQL_ROW row;
24
18
unsigned long long begin = monotonic_time ();
25
19
unsigned long long row_count = 0 ;
26
- MYSQL_QUERY (mysql, query.c_str ());
20
+ unsigned long ret_query = 0 ;
21
+
22
+ ret_query = mysql_query (mysql, query.c_str ());
23
+ if (ret_query != 0 ) {
24
+ fprintf (stderr, " Failed to execute query: Error: %s\n " , mysql_error (mysql));
25
+ return -1 ;
26
+ }
27
+
27
28
res = mysql_use_result (mysql);
28
29
unsigned long long num_rows = mysql_num_rows (res);
29
30
unsigned int num_fields = mysql_num_fields (res);
@@ -39,40 +40,6 @@ unsigned long calculate_query_execution_time(MYSQL* mysql, const std::string& qu
39
40
return (end - begin);
40
41
}
41
42
42
- void set_compression_level (MYSQL* proxysql_admin, std::string level) {
43
- std::string query = " UPDATE global_variables SET variable_value = '" + level + " ' WHERE variable_name = 'mysql-protocol_compression_level';" ;
44
- mysql_query (proxysql_admin, query.c_str ());
45
- query = " LOAD MYSQL VARIABLES TO RUNTIME;" ;
46
- mysql_query (proxysql_admin, query.c_str ());
47
- }
48
-
49
- int32_t get_compression_level (MYSQL* proxysql_admin) {
50
- int32_t compression_level = -1 ;
51
- const uint32_t SERVERS_COUNT = 10 ;
52
-
53
- int err = mysql_query (proxysql_admin, " SELECT * FROM global_variables WHERE variable_name='mysql-protocol_compression_level'" );
54
- if (err != EXIT_SUCCESS) {
55
- diag (
56
- " Query for retrieving value of 'mysql-protocol_compression_level' failed with error: (%d, %s)" ,
57
- mysql_errno (proxysql_admin), mysql_error (proxysql_admin)
58
- );
59
- return compression_level;
60
- }
61
-
62
- MYSQL_RES* res = mysql_store_result (proxysql_admin);
63
- if (res != nullptr ) {
64
- int num_rows = mysql_num_rows (res);
65
- MYSQL_ROW row = mysql_fetch_row (res);
66
-
67
- if (num_rows && row != nullptr ) {
68
- char * endptr = nullptr ;
69
- compression_level = strtol (row[1 ], &endptr, SERVERS_COUNT);
70
- }
71
- }
72
- mysql_free_result (res);
73
- return compression_level;
74
- }
75
-
76
43
MYSQL* initilize_mysql_connection (char * host, char * username, char * password, int port, bool compression) {
77
44
MYSQL* mysql = mysql_init (NULL );
78
45
if (!mysql)
@@ -82,14 +49,12 @@ MYSQL* initilize_mysql_connection(char* host, char* username, char* password, in
82
49
if (!mysql_real_connect (mysql, host, username, password, NULL , port, NULL , 0 )) {
83
50
fprintf (stderr, " Failed to connect to database: Error: %s\n " ,
84
51
mysql_error (mysql));
85
- mysql_close (mysql);
86
52
return nullptr ;
87
53
}
88
54
if (compression) {
89
55
if (mysql_options (mysql, MYSQL_OPT_COMPRESS, nullptr ) != 0 ) {
90
56
fprintf (stderr, " Failed to set mysql compression option: Error: %s\n " ,
91
57
mysql_error (mysql));
92
- mysql_close (mysql);
93
58
return nullptr ;
94
59
}
95
60
}
@@ -99,36 +64,52 @@ MYSQL* initilize_mysql_connection(char* host, char* username, char* password, in
99
64
int main (int argc, char ** argv) {
100
65
101
66
CommandLine cl;
67
+ 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" ;
68
+ unsigned long time_proxy = 0 ;
69
+ unsigned long time_proxy_compressed = 0 ;
70
+ unsigned long diff = 0 ;
71
+ unsigned long time_mysql_compressed = 0 ;
72
+ std::string compression_level = {" " };
73
+ int32_t ret = 0 ;
74
+ MYSQL* proxysql = nullptr ;
75
+ MYSQL* proxysql_compression = nullptr ;
76
+ MYSQL* proxysql_admin = nullptr ;
77
+ MYSQL* mysql_compression = nullptr ;
78
+ int performance_diff = 0 ;
102
79
103
80
if (cl.getEnv ())
104
81
return exit_status ();
105
82
106
83
plan (5 );
107
84
108
85
// ProxySQL connection without compression
109
- MYSQL* proxysql = initilize_mysql_connection (cl.host , cl.username , cl.password , cl.port , false );
86
+ proxysql = initilize_mysql_connection (cl.host , cl.username , cl.password , cl.port , false );
110
87
if (!proxysql) {
111
- return exit_status () ;
88
+ goto cleanup ;
112
89
}
113
90
114
91
// ProxySQL connection with compression
115
- MYSQL* proxysql_compression = initilize_mysql_connection (cl.host , cl.username , cl.password , cl.port , true );
92
+ proxysql_compression = initilize_mysql_connection (cl.host , cl.username , cl.password , cl.port , true );
116
93
if (!proxysql_compression) {
117
- return exit_status () ;
94
+ goto cleanup ;
118
95
}
119
96
120
97
// MySQL connection with compression
121
- MYSQL* mysql_compression = initilize_mysql_connection (cl.host , cl.username , cl.password , cl.mysql_port , true );
98
+ mysql_compression = initilize_mysql_connection (cl.host , cl.username , cl.password , cl.mysql_port , true );
122
99
if (!mysql_compression) {
123
- return exit_status () ;
100
+ goto cleanup ;
124
101
}
125
102
126
103
// ProxySQL admin connection
127
- MYSQL* proxysql_admin = initilize_mysql_connection (cl.host , cl.admin_username , cl.admin_password , cl.admin_port , false );
104
+ proxysql_admin = initilize_mysql_connection (cl.host , cl.admin_username , cl.admin_password , cl.admin_port , false );
128
105
if (!proxysql_admin) {
129
- return exit_status () ;
106
+ goto cleanup ;
130
107
}
131
108
109
+ // Change default query rules to avoid replication issues; This test only requires the default hostgroup
110
+ MYSQL_QUERY (proxysql_admin, " UPDATE mysql_query_rules SET active=0" );
111
+ MYSQL_QUERY (proxysql_admin, " LOAD MYSQL QUERY RULES TO RUNTIME" );
112
+
132
113
MYSQL_QUERY (proxysql, " CREATE DATABASE IF NOT EXISTS test" );
133
114
MYSQL_QUERY (proxysql, " DROP TABLE IF EXISTS test.sbtest1" );
134
115
@@ -140,45 +121,92 @@ int main(int argc, char** argv) {
140
121
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;" );
141
122
}
142
123
143
- 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" ;
144
-
145
- unsigned long time_proxy = calculate_query_execution_time (proxysql, query);
124
+ time_proxy = calculate_query_execution_time (proxysql, query);
146
125
diag (" Time taken for query with proxysql without compression: %ld" , time_proxy);
126
+ if (time_proxy == -1 ) {
127
+ goto cleanup;
128
+ }
147
129
148
- unsigned long time_proxy_compressed = calculate_query_execution_time (proxysql_compression, query);
130
+ time_proxy_compressed = calculate_query_execution_time (proxysql_compression, query);
149
131
diag (" Time taken for query with proxysql with compression: %ld" , time_proxy_compressed);
132
+ if (time_proxy_compressed == -1 ) {
133
+ goto cleanup;
134
+ }
150
135
151
- unsigned long diff = abs (time_proxy - time_proxy_compressed);
152
- int performance_diff = (diff * 100 ) / time_proxy;
136
+ diff = abs (time_proxy - time_proxy_compressed);
137
+ performance_diff = (diff * 100 ) / time_proxy;
153
138
154
139
ok ((performance_diff < 10 ), " proxysql with compression performed well compared to without compression. Performance difference: %d percentage" , performance_diff);
155
140
156
- unsigned long time_mysql_compressed = calculate_query_execution_time (mysql_compression, query);
141
+ time_mysql_compressed = calculate_query_execution_time (mysql_compression, query);
157
142
diag (" Time taken for query with mysql with compression: %ld" , time_mysql_compressed);
143
+ if (time_mysql_compressed == -1 ) {
144
+ goto cleanup;
145
+ }
158
146
159
147
diff = abs (time_mysql_compressed - time_proxy_compressed);
160
148
performance_diff = (diff * 100 ) / time_mysql_compressed;
161
149
162
150
ok ((performance_diff < 20 ), " proxysql with compression performed well compared to mysql with compression. Performance difference: %d percentage" , performance_diff);
163
151
164
- int32_t compression_level = get_compression_level (proxysql_admin);
165
- ok (compression_level == 3 , " Default compression level is correct: %d" , compression_level);
152
+ ret = get_variable_value (proxysql_admin, " mysql-protocol_compression_level" , compression_level, true );
153
+ if (ret == EXIT_SUCCESS) {
154
+ ok (compression_level == " 3" , " Default compression level is correct: %s" , compression_level.c_str ());
155
+ }
156
+ else {
157
+ diag (" Failed to get the default compression level." );
158
+ goto cleanup;
159
+ }
166
160
167
- set_compression_level (proxysql_admin, " 8" );
168
- compression_level = get_compression_level (proxysql_admin);
169
- ok (compression_level == 8 , " Compression level set correctly: %d" , compression_level);
161
+ set_admin_global_variable (proxysql_admin, " mysql-protocol_compression_level" , " 8" );
162
+ if (mysql_query (proxysql_admin, " load mysql variables to runtime" )) {
163
+ diag (" Failed to load mysql variables to runtime." );
164
+ goto cleanup;
165
+ }
166
+ ret = get_variable_value (proxysql_admin, " mysql-protocol_compression_level" , compression_level, true );
167
+ if (ret == EXIT_SUCCESS) {
168
+ ok (compression_level == " 8" , " Compression level is set correctly: %s" , compression_level.c_str ());
169
+ }
170
+ else {
171
+ diag (" Failed to set the Compression level is set correctly:" );
172
+ goto cleanup;
173
+ }
170
174
171
175
time_proxy_compressed = calculate_query_execution_time (proxysql_compression, query);
172
176
diag (" Time taken for query with proxysql with compression level 8: %ld" , time_proxy_compressed);
177
+ if (time_proxy_compressed == -1 ) {
178
+ goto cleanup;
179
+ }
173
180
174
- set_compression_level (proxysql_admin, " 3" );
175
- compression_level = get_compression_level (proxysql_admin);
176
- ok (compression_level == 3 , " Compression level set correctly: %d" , compression_level);
181
+ set_admin_global_variable (proxysql_admin, " mysql-protocol_compression_level" , " 3" );
182
+ if (mysql_query (proxysql_admin, " load mysql variables to runtime" )) {
183
+ diag (" Failed to load mysql variables to runtime." );
184
+ goto cleanup;
185
+ }
186
+ ret = get_variable_value (proxysql_admin, " mysql-protocol_compression_level" , compression_level, true );
187
+ if (ret == EXIT_SUCCESS) {
188
+ ok (compression_level == " 3" , " Compression level set correctly: %s" , compression_level.c_str ());
189
+ }
190
+ else {
191
+ diag (" Failed to set the Compression level set correctly:" );
192
+ goto cleanup;
193
+ }
194
+
195
+ cleanup:
196
+ // Recover default query rules
197
+ if (proxysql_admin) {
198
+ MYSQL_QUERY (proxysql_admin, " UPDATE mysql_query_rules SET active=1" );
199
+ MYSQL_QUERY (proxysql_admin, " LOAD MYSQL QUERY RULES TO RUNTIME" );
200
+ }
177
201
178
- mysql_close (proxysql);
179
- mysql_close (proxysql_compression);
180
- mysql_close (mysql_compression);
181
- mysql_close (proxysql_admin);
202
+ if (proxysql)
203
+ mysql_close (proxysql);
204
+ if (proxysql_compression)
205
+ mysql_close (proxysql_compression);
206
+ if (mysql_compression)
207
+ mysql_close (mysql_compression);
208
+ if (proxysql_admin)
209
+ mysql_close (proxysql_admin);
182
210
183
211
return exit_status ();
184
212
}
0 commit comments