-
Notifications
You must be signed in to change notification settings - Fork 4
/
aurora_bench.cpp
443 lines (428 loc) · 11.9 KB
/
aurora_bench.cpp
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
//#include <mysql/mysql.h>
#include <mysql.h>
//#include <mysql/my_config.h>
//#include <my_config.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <boost/histogram.hpp>
#include <iostream>
#if MYSQL_VERSION_MAJOR > 5
#define MYSQL_WITH_SSL
#endif
#ifndef MYSQL_WITH_SSL
#if MYSQL_VERSION_MAJOR == 5 && MYSQL_VERSION_MINOR == 7 && MYSQL_VERSION_PATCH > 10
#define MYSQL_WITH_SSL
#endif
#endif
// using histogram from https://github.com/HDembinski/histogram
namespace bh = boost::histogram;
using namespace bh::literals;
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);
}
auto gh0 = bh::make_static_histogram(bh::axis::regular<>(20, 0, 2000, "us"));
auto gh1 = bh::make_static_histogram(bh::axis::regular<>(20, 0, 40000, "us"));
auto gh2 = bh::make_static_histogram(bh::axis::regular<>(25, 0, 1000, "ms"));
auto gh3 = bh::make_static_histogram(bh::axis::regular<>(10, 0, 10, "s"));
pthread_mutex_t mutex;
int interval_us=-1;
int num_threads=1;
int num_users=0;
int count=0;
char *username=NULL;
char *password=NULL;
char *host=(char *)"localhost";
int port=3306;
char *schema=(char *)"information_schema";
int silent;
int keep_open=0;
int local=0;
int queries=0;
int histograms=-1;
unsigned int g_connect_OK=0;
unsigned int g_connect_ERR=0;
unsigned int g_select_OK=0;
unsigned int g_select_ERR=0;
#ifdef MYSQL_WITH_SSL
char *ssl = NULL;
#endif
char *auth = NULL;
void * my_conn_thread(void *arg) {
auto h0 = bh::make_static_histogram(bh::axis::regular<>(20, 0, 2000, "us"));
auto h1 = bh::make_static_histogram(bh::axis::regular<>(20, 0, 40000, "us"));
auto h2 = bh::make_static_histogram(bh::axis::regular<>(25, 0, 1000, "ms"));
auto h3 = bh::make_static_histogram(bh::axis::regular<>(10, 0, 10, "s"));
unsigned int connect_OK=0;
unsigned int connect_ERR=0;
unsigned int select_OK=0;
unsigned int select_ERR=0;
char * _username = NULL;
char * _password = NULL;
char arg_on=1;
int i, j;
char query[128];
unsigned long long b, e, ce;
MYSQL **mysqlconns = NULL;
if (keep_open) {
mysqlconns=(MYSQL **)malloc(sizeof(MYSQL *)*count);
}
for (i=0; i<count; i++) {
MYSQL *mysql=mysql_init(NULL);
b=monotonic_time(); // begin
#ifdef MYSQL_WITH_SSL
unsigned int ssl_arg = SSL_MODE_DISABLED;
if (ssl) {
if (strcmp(ssl,"disabled")==0) {
ssl_arg = SSL_MODE_DISABLED;
}
if (strcmp(ssl,"preferred")==0) {
ssl_arg = SSL_MODE_PREFERRED;
}
if (strcmp(ssl,"required")==0) {
ssl_arg = SSL_MODE_REQUIRED;
}
}
mysql_options(mysql, MYSQL_OPT_SSL_MODE, &ssl_arg);
#endif
if (auth) {
int rc = 0;
rc = mysql_options(mysql, MYSQL_DEFAULT_AUTH, auth);
//rc = mysql_options(mysql, MYSQL_DEFAULT_AUTH, "mysql_native_password");
if (rc) {
fprintf(stderr, "Unable to set auth plugin %s\n", mysql_error(mysql));
exit(EXIT_FAILURE);
}
}
if (num_users) {
free(_username);
free(_password);
_username = (char *)malloc(strlen(username)+128);
_password = (char *)malloc(strlen(password)+128);
int id = rand() % num_users;
id++;
sprintf(_username,"%s%d",username,id);
sprintf(_password,"%s%d",password,id);
} else {
_username = username;
_password = password;
}
mysql_options(mysql, MYSQL_DEFAULT_AUTH, "mysql_native_password");
MYSQL *rc=mysql_real_connect(mysql, host, _username, _password, schema, (local ? 0 : port), NULL, 0);
if (keep_open) {
mysqlconns[i] = mysql;
}
if (rc) {
if (num_users==0) {
mysql_set_character_set(mysql, "utf8");
}
}
if (queries==0) {
// we computed this only if queries==0
ce=monotonic_time(); // connection established
}
if (rc) {
connect_OK++;
setsockopt(mysql->net.fd, SOL_SOCKET, SO_REUSEADDR, (char *)&arg_on, sizeof(arg_on));
if (queries) {
int n_qu = queries;
if (keep_open) {
if (i < count/2) {
n_qu = 0;
} else {
n_qu *=2 ;
}
}
MYSQL *my = NULL;
for (j=0; j<n_qu; j++) {
if (keep_open) {
if (j%100==0) {
int randc = rand()%(i+1);
//mysql = mysqlconns[randc];
my = mysqlconns[randc];
}
mysql = my;
/*
if (rand()%100 == 0) { // every 20 queries, disconnect and reconnect
MYSQL * mysql_new = mysql_init(NULL);
#ifdef MYSQL_WITH_SSL
mysql_options(mysql_new, MYSQL_OPT_SSL_MODE, &ssl_arg);
#endif
if (auth) {
int rc = 0;
rc = mysql_options(mysql_new, MYSQL_DEFAULT_AUTH, auth);
//rc = mysql_options(mysql, MYSQL_DEFAULT_AUTH, "mysql_native_password");
if (rc) {
fprintf(stderr, "Unable to set auth plugin %s\n", mysql_error(mysql_new));
exit(EXIT_FAILURE);
}
}
rc = mysql_real_connect(mysql_new, host, _username, _password, schema, (local ? 0 : port), NULL, 0);
if (rc) {
connect_OK++;
if (num_users==0) {
mysql_set_character_set(mysql, "utf8");
}
mysql_close(mysql);
mysql = mysql_new;
mysqlconns[randc] = mysql;
}
}
*/
}
int r1=rand()%100;
//sprintf(query,"SELECT /* max_lag_ms=%d */ %d", 1+r1, r1);
//sprintf(query,"SELECT %d", r1);
sprintf(query,"INSERT INTO test1 VALUES (NULL, 0)");
if (mysql_query(mysql,query)) {
if (silent==0) {
fprintf(stderr,"%s\n", mysql_error(mysql));
}
select_ERR++;
} else {
select_OK++;
}
sprintf(query,"DELETE FROM test1 WHERE id>= %d ORDER BY id LIMIT 1", r1);
if (mysql_query(mysql,query)) {
if (silent==0) {
fprintf(stderr,"%s\n", mysql_error(mysql));
}
select_ERR++;
} else {
select_OK++;
}
sprintf(query,"SELECT * FROM test1 LIMIT %d", r1);
if (mysql_query(mysql,query)) {
if (silent==0) {
fprintf(stderr,"%s\n", mysql_error(mysql));
}
select_ERR++;
} else {
MYSQL_RES *result = mysql_store_result(mysql);
mysql_free_result(result);
select_OK++;
}
if (interval_us) {
usleep(interval_us/100 * (1+rand()%3));
}
}
}
if (keep_open==0)
mysql_close(mysql);
} else {
connect_ERR++;
if (silent==0) {
fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(mysql));
}
}
e=monotonic_time(); // connection ended
unsigned long long l;
if (queries==0) {
l=(ce-b);
} else {
l=(e-b);
}
if (histograms >= 0 && histograms <= 3) {
if (l < 40000 && histograms < 2) {
if (l<2000 && histograms == 0) {
h0.fill(l);
} else {
h1.fill(l);
}
} else {
if (l < 1000000 && histograms < 3) {
h2.fill(((float)l)/1000);
} else {
h3.fill(((float)l)/1000000);
}
}
}
l=e-b;
if (l < interval_us) {
usleep(interval_us-(e-b));
}
}
if (keep_open) {
for (i=0; i<count; i++) {
MYSQL *my = mysqlconns[i];
mysql_close(my);
}
}
pthread_mutex_lock(&mutex);
for (const auto& bin : h0.axis(0_c)) {
if (h0.value(bin.idx)) {
gh0.fill((bin.left+bin.right)/2, bh::weight(h0.value(bin.idx)));
}
}
for (const auto& bin : h1.axis(0_c)) {
if (h1.value(bin.idx)) {
gh1.fill((bin.left+bin.right)/2, bh::weight(h1.value(bin.idx)));
}
}
for (const auto& bin : h2.axis(0_c)) {
if (h2.value(bin.idx)) {
gh2.fill((bin.left+bin.right)/2, bh::weight(h2.value(bin.idx)));
}
}
for (const auto& bin : h3.axis(0_c)) {
if (h3.value(bin.idx)) {
gh3.fill((bin.left+bin.right)/2, bh::weight(h3.value(bin.idx)));
}
}
pthread_mutex_unlock(&mutex);
__sync_fetch_and_add(&g_connect_OK,connect_OK);
__sync_fetch_and_add(&g_connect_ERR,connect_ERR);
__sync_fetch_and_add(&g_select_OK,select_OK);
__sync_fetch_and_add(&g_select_ERR,select_ERR);
return NULL;
}
int main(int argc, char *argv[]) {
pthread_mutex_init(&mutex,NULL);
int opt;
#ifdef MYSQL_WITH_SSL
while ((opt = getopt(argc, argv, "H:kst:i:c:u:p:h:P:D:q:U:S:a:")) != -1) {
#else
while ((opt = getopt(argc, argv, "H:kst:i:c:u:p:h:P:D:q:U:a:")) != -1) {
#endif
switch (opt) {
case 'H':
histograms = atoi(optarg);
if (histograms > 3) {
fprintf(stderr,"Incorrect number of histograms\n");
exit(EXIT_FAILURE);
}
break;
case 't':
num_threads = atoi(optarg);
break;
case 'U':
num_users = atoi(optarg);
break;
case 'i':
interval_us = atoi(optarg);
break;
case 'c':
count = atoi(optarg);
break;
case 'q':
queries = atoi(optarg);
break;
case 'u':
username = strdup(optarg);
break;
case 'p':
password = strdup(optarg);
break;
#ifdef MYSQL_WITH_SSL
case 'S':
ssl = strdup(optarg);
break;
#endif
case 'a':
auth = strdup(optarg);
break;
case 'h':
host = strdup(optarg);
break;
case 'D':
schema = strdup(optarg);
break;
case 'P':
port = atoi(optarg);
break;
case 'k':
keep_open = 1;
break;
case 's':
silent = 1;
break;
default: /* '?' */
#ifdef MYSQL_WITH_SSL
fprintf(stderr, "Usage: %s -i interval_us -c count -u username -p password [ -U num_users ] [ -h host ] [ -P port ] [ -D schema ] [ -q queries ] [ -H {0|1|2|3} ] [ -s ] [ -k ] [ -t threads ][ -S {disabled|preferred|required} ][ -a auth_plugin ]\n", argv[0]);
#else
fprintf(stderr, "Usage: %s -i interval_us -c count -u username -p password [ -U num_users ] [ -h host ] [ -P port ] [ -D schema ] [ -q queries ] [ -H {0|1|2|3} ] [ -s ] [ -k ] [ -t threads ]\n", argv[0]);
#endif
exit(EXIT_FAILURE);
}
}
if (
(interval_us == -1) ||
(count == 0) ||
(username == NULL) ||
(password == NULL)
) {
#ifdef MYSQL_WITH_SSL
fprintf(stderr, "Usage: %s -i interval_us -c count -u username -p password [ -U num_users ] [ -h host ] [ -P port ] [ -D schema ] [ -q queries ] [ -H {0|1|2|3} ] [ -s ] [ -k ] [ -t threads ][ -S {disabled|preferred|required} ][ -a auth_plugin ]\n", argv[0]);
#else
fprintf(stderr, "Usage: %s -i interval_us -c count -u username -p password [ -U num_users ] [ -h host ] [ -P port ] [ -D schema ] [ -q queries ] [ -H {0|1|2|3} ] [ -s ] [ -k ] [ -t threads ]\n", argv[0]);
#endif
exit(EXIT_FAILURE);
}
#ifdef MYSQL_WITH_SSL
if (ssl) {
if (
strcmp(ssl,(char *)"disabled")
&&
strcmp(ssl,(char *)"preferred")
&&
strcmp(ssl,(char *)"required")
) {
fprintf(stderr,"Invalid SSL setting\n");
exit(EXIT_FAILURE);
}
}
#endif
int i;
int rc;
if (strcmp(host,"localhost")==0) {
local = 1;
}
mysql_library_init(0, NULL, NULL);
pthread_t *thi=(pthread_t *)malloc(sizeof(pthread_t)*num_threads);
if (thi==NULL) exit(EXIT_FAILURE);
unsigned long long start_time=monotonic_time();
for (i=0; i<num_threads; i++) {
if ( pthread_create(&thi[i], NULL, my_conn_thread , NULL) != 0 )
perror("Thread creation");
}
for (i=0; i<num_threads; i++) {
pthread_join(thi[i], NULL);
}
unsigned long long end_time=monotonic_time();
free(thi);
if (queries) {
fprintf(stderr,"Connections[OK/ERR]: (%u,%u) . Queries: (%u,%u) . Clock time: %llums\n", g_connect_OK, g_connect_ERR, g_select_OK, g_select_ERR, (end_time-start_time)/1000);
} else {
fprintf(stderr,"Connections[OK/ERR]: (%u,%u) . Clock time: %llums\n", g_connect_OK, g_connect_ERR, (end_time-start_time)/1000);
}
for (const auto& bin : gh0.axis(0_c)) {
if (gh0.value(bin.idx)) {
//fprintf(stdout,"[%.1fms, %.1fms): %u\n", (bin.left/1000), (bin.right/1000), (unsigned int)(gh0.value(bin.idx)));
std::cout << "[" << bin.left/1000 << "ms, " << bin.right/1000 << "ms): " << gh0.value(bin.idx) << std::endl;
}
}
for (const auto& bin : gh1.axis(0_c)) {
if (gh1.value(bin.idx)) {
std::cout << "[" << bin.left/1000 << "ms, " << bin.right/1000 << "ms): " << gh1.value(bin.idx) << std::endl;
}
}
for (const auto& bin : gh2.axis(0_c)) {
if (gh2.value(bin.idx)) {
std::cout << "[" << bin.left << "ms, " << bin.right << "ms): " << gh2.value(bin.idx) << std::endl;
}
}
for (const auto& bin : gh3.axis(0_c)) {
if (gh3.value(bin.idx)) {
std::cout << "[" << bin.left << "ms, " << bin.right << "ms): " << gh3.value(bin.idx) << std::endl;
}
}
exit(EXIT_SUCCESS);
}