~jan-kneschke/mysql-proxy/packet-tracking-assertions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* $%BEGINLICENSE%$
 Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
 published by the Free Software Foundation; version 2 of the
 License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 02110-1301  USA

 $%ENDLICENSE%$ */
 

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define C(x) x, sizeof(x) - 1

#include <mysql.h>
#include <glib.h>

#include "sys-pedantic.h"

#define TIME_DIFF_US(t2, t1) \
	        ((t2->tv_sec - t1->tv_sec) * 1000000.0 + (t2->tv_usec - t1->tv_usec))

#define START_TIMING(fmt, ...) do { GTimeVal start_ts, stop_ts; GString *ts = g_string_new(NULL); gchar *group = g_strdup_printf(fmt, __VA_ARGS__); g_get_current_time(&start_ts);
#define STOP_TIMING(fmt, ...) g_get_current_time(&stop_ts); g_message("INSERT INTO bench VALUES (\"%s\", \""fmt"\", %s);", group, __VA_ARGS__, get_time_diff_scaled(&stop_ts, &start_ts, ts)); g_string_free(ts, TRUE); g_free(group);} while(0)

#define TEST_QUERY_LARGE    "SELECT REPEAT('x', POW(2, 16)) FROM dual"
#define TEST_QUERY_SMALL    "SELECT 1 FROM dual WHERE 1 = 1"
#define TEST_QUERY_SMALL_PS "SELECT 1 FROM dual WHERE 1 = ?"

gchar * get_time_diff_scaled(GTimeVal *t1, GTimeVal *t2, GString *str) {
	double query_time_us;
	double query_time;
	char *query_time_format = NULL;

	query_time_us = TIME_DIFF_US(t1, t2);

	query_time = query_time_us / 1000.0;
	query_time_format = "%0.3f";

	g_string_printf(str, query_time_format, query_time);

	return str->str;
}
static void log_func(const gchar *UNUSED_PARAM(log_domain), GLogLevelFlags UNUSED_PARAM(log_level), const gchar *message, gpointer UNUSED_PARAM(user_data)) {
	write(STDERR_FILENO, message, strlen(message));
	write(STDERR_FILENO, "\n", 1);
}

int test_prep_stmt(MYSQL *mysql) {
	MYSQL_STMT *stmt;
	long one = 1, two = 0;
	MYSQL_BIND params[1];
	MYSQL_RES *res;
	int round;

	if (NULL == (stmt = mysql_stmt_init(mysql))) {
	       	g_error("%s.%d: mysql_stmt_init() failed: %s", 
				__FILE__, __LINE__, mysql_error(mysql));
	}

	START_TIMING("%s:%d", mysql->host, mysql->port);
	if (mysql_stmt_prepare(stmt, C(TEST_QUERY_SMALL_PS))) {
	       g_error("%s.%d: mysql_stmt_prepare(%s) failed: %s", 
				__FILE__, __LINE__, 
				TEST_QUERY_SMALL_PS,
				mysql_stmt_error(stmt));
	}
	STOP_TIMING("  mysql_stmt_prepare(%s)", "");

	g_assert(mysql_stmt_param_count(stmt) == 1);

	res = mysql_stmt_result_metadata(stmt);
	g_assert(res);
	g_assert(mysql_num_fields(res) == 1);

	params[0].buffer_type = MYSQL_TYPE_LONG;
	params[0].buffer = (char *)&one;
	params[0].is_null = 0;
	params[0].length = 0;

	if (mysql_stmt_bind_param(stmt, params)) {
	       g_error("%s.%d: mysql_stmt_bind_param() failed: %s", 
			       __FILE__, __LINE__, mysql_stmt_error(stmt));
	}

	for (round = 0; round < 2; round++) {
		MYSQL_BIND result[1];
		my_bool is_null;
		my_bool error;
		unsigned long length;

		START_TIMING("%s:%d", mysql->host, mysql->port);
		if (mysql_stmt_execute(stmt)) {
		       	g_error("%s.%d: mysql_stmt_execute() failed: %s", 
					__FILE__, __LINE__, mysql_stmt_error(stmt));
		}
		STOP_TIMING("  mysql_execute(%s)", "");
	
		result[0].buffer_type = MYSQL_TYPE_LONG;
		result[0].buffer = (char *)&two;
		result[0].is_null = &is_null;
		result[0].length = &length;
#if MYSQL_VERSION_ID >= 50000
		result[0].error = &error;
#endif
	
		START_TIMING("%s:%d", mysql->host, mysql->port);
		if (mysql_stmt_bind_result(stmt, result)) {
		       	g_error("%s.%d: mysql_stmt_bind_result() failed: %s", 
					__FILE__, __LINE__, mysql_stmt_error(stmt));
		}
		STOP_TIMING("  mysql_stmt_bind_result(%s)", "");
	
		START_TIMING("%s:%d", mysql->host, mysql->port);
		while (mysql_stmt_fetch(stmt)) {
		}
		STOP_TIMING("  mysql_stmt_fetch(%s)", "");
	}

	mysql_free_result(res);

	if (mysql_stmt_close(stmt)) {
	       	g_error("%s.%d: mysql_stmt_execute() failed: %s", __FILE__, __LINE__, mysql_stmt_error(stmt));
	}

	return 0;
}

int test_prep_stmt_noparam(MYSQL *mysql) {
	MYSQL_STMT *stmt;
	long one = 1, two = 0;
	MYSQL_BIND params[1];
	MYSQL_RES *res;
	int round;

	if (NULL == (stmt = mysql_stmt_init(mysql))) {
	       	g_error("%s.%d: mysql_stmt_init() failed: %s", __FILE__, __LINE__, mysql_error(mysql));
	}

	START_TIMING("%s:%d", mysql->host, mysql->port);
	if (mysql_stmt_prepare(stmt, C(TEST_QUERY_SMALL))) {
	       g_error("%s.%d: mysql_stmt_prepare(%s) failed: %s", 
				__FILE__, __LINE__, 
				TEST_QUERY_SMALL,
				mysql_stmt_error(stmt));
	}
	STOP_TIMING("  mysql_stmt_prepare(%s)", "");

	g_assert(mysql_stmt_param_count(stmt) == 0);

	res = mysql_stmt_result_metadata(stmt);
	g_assert(res);
	g_assert(mysql_num_fields(res) == 1);

	params[0].buffer_type = MYSQL_TYPE_LONG;
	params[0].buffer = (char *)&one;
	params[0].is_null = 0;
	params[0].length = 0;

	if (mysql_stmt_bind_param(stmt, params)) {
	       g_error("%s.%d: mysql_stmt_bind_param() failed: %s", __FILE__, __LINE__, mysql_stmt_error(stmt));
	}

	for (round = 0; round < 2; round++) {
		MYSQL_BIND result[1];
		my_bool is_null;
		my_bool error;
		unsigned long length;

		START_TIMING("%s:%d", mysql->host, mysql->port);
		if (mysql_stmt_execute(stmt)) {
		       	g_error("%s.%d: mysql_stmt_execute() failed: %s", __FILE__, __LINE__, mysql_stmt_error(stmt));
		}
		STOP_TIMING("  mysql_execute(%s)", "");
	
		result[0].buffer_type = MYSQL_TYPE_LONG;
		result[0].buffer = (char *)&two;
		result[0].is_null = &is_null;
		result[0].length = &length;
#if MYSQL_VERSION_ID >= 50000
		result[0].error = &error;
#endif
	
		START_TIMING("%s:%d", mysql->host, mysql->port);
		if (mysql_stmt_bind_result(stmt, result)) {
		       	g_error("%s.%d: mysql_stmt_bind_result() failed: %s", __FILE__, __LINE__, mysql_stmt_error(stmt));
		}
		STOP_TIMING("  mysql_stmt_bind_result(%s)", "");
	
		START_TIMING("%s:%d", mysql->host, mysql->port);
		while (mysql_stmt_fetch(stmt)) {
		}
		STOP_TIMING("  mysql_stmt_fetch(%s)", "");
	}

	mysql_free_result(res);

	if (mysql_stmt_close(stmt)) {
	       	g_error("%s.%d: mysql_stmt_execute() failed: %s", __FILE__, __LINE__, mysql_stmt_error(stmt));
	}

	return 0;
}


int test_select(MYSQL *mysql) {
	MYSQL_RES *res;
	int round;

	for (round = 0; round < 5; round++) {
		START_TIMING("%s:%d", mysql->host, mysql->port);
		if (mysql_real_query(mysql, C(TEST_QUERY_SMALL))) {
		       g_error("%s.%d: mysql_real_query(%s) failed: %s", 
					__FILE__, __LINE__, 
					TEST_QUERY_SMALL,
					mysql_error(mysql));
		}
		STOP_TIMING("  mysql_real_query(%s)", "small");
	
		START_TIMING("%s:%d", mysql->host, mysql->port);
		if (NULL == (res = mysql_store_result(mysql))) {
	 		g_error("%s.%d: mysql_store_result() failed: %s", 
					__FILE__, __LINE__, 
					mysql_error(mysql));
		}
		STOP_TIMING("  mysql_store_result(%s)", "small");

		mysql_free_result(res);
	}
		
	return 0;
}

int test_select_large(MYSQL *mysql) {
	MYSQL_RES *res;
	int round;

	for (round = 0; round < 5; round++) {
		START_TIMING("%s:%d", mysql->host, mysql->port);
		if (mysql_real_query(mysql, C(TEST_QUERY_LARGE))) {
		       g_error("%s.%d: mysql_real_query(%s) failed: %s", 
					__FILE__, __LINE__, 
					TEST_QUERY_LARGE,
					mysql_error(mysql));
		}
		STOP_TIMING("  mysql_real_query(%s)", "large");
	
		START_TIMING("%s:%d", mysql->host, mysql->port);
		if (NULL == (res = mysql_store_result(mysql))) {
	 		g_error("%s.%d: mysql_store_result() failed: %s", 
					__FILE__, __LINE__, 
					mysql_error(mysql));
		}
		STOP_TIMING("  mysql_store_result(%s)", "large");

		mysql_free_result(res);
	}
		
	return 0;
}

int test_server(const gchar *host, guint port, const gchar *user, const gchar *password, const gchar *db) {
	MYSQL *mysql;
	char *indent = " ";

	mysql = mysql_init(NULL);

	START_TIMING("%s:%d", host, port);
	if (!mysql_real_connect(mysql, host, user, password, db, port, NULL, 0)) {
	       	g_error("%s.%d: mysql_real_connect() failed: %s", __FILE__, __LINE__, mysql_error(mysql));
	}
	STOP_TIMING("%smysql_real_connect()", indent);

	START_TIMING("%s:%d", mysql->host, mysql->port);
	test_prep_stmt(mysql);
	STOP_TIMING("%stest_prep_stmt()", indent);

	START_TIMING("%s:%d", mysql->host, mysql->port);
	test_prep_stmt_noparam(mysql);
	STOP_TIMING("%stest_prep_stmt_noparam()", indent);

	START_TIMING("%s:%d", mysql->host, mysql->port);
	test_select(mysql);
	STOP_TIMING("%stest_select()", indent);

	START_TIMING("%s:%d", mysql->host, mysql->port);
	test_select_large(mysql);
	STOP_TIMING("%stest_select_large()", indent);

	mysql->free_me = 1;
	mysql_close(mysql);

	return 0;
}

const gchar *getenv_def(const gchar *key, const gchar *def) {
	const char *p;

	p = getenv(key);

	return p ? p : def;
}

int main(int argc, char **argv) {
	int rounds;
	const gchar *user;
	const gchar *password;
	const gchar *db;
	const gchar *host;

	user     = getenv_def("MYSQL_TEST_USER", "root");	
	password = getenv_def("MYSQL_TEST_PASSWORD", "");	
	db       = getenv_def("MYSQL_TEST_DB",   "test");	
	host     = getenv_def("MYSQL_TEST_HOST", "127.0.0.1");	

	g_log_set_default_handler(log_func, NULL);
#if 0
	/* warm up */
	START_TIMING("%s:%d", "127.0.0.1", 3306);
	test_server("127.0.0.1", 3306);
	STOP_TIMING("warm-up %s", "done");
#endif
	for (rounds = 0; rounds < 10; rounds++) {
		/* real benchmarks */

		START_TIMING("%s:%d", host, 3306);
		test_server(host, 3306, user, password, db);
		STOP_TIMING("benchmark %s", "done");
	
		START_TIMING("%s:%d", host, 4040);
		test_server(host, 4040, user, password, db);
		STOP_TIMING("benchmark %s", "done");
	}

	return 0;
}