-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench_spike.cpp
265 lines (250 loc) · 7.88 KB
/
bench_spike.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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <mysql/mysql.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <boost/histogram.hpp>
#include <iostream>
// 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);
}
struct cpu_timer
{
cpu_timer() {
begin = monotonic_time();
}
~cpu_timer()
{
unsigned long long end = monotonic_time();
std::cerr << double( end - begin ) / 1000000 << " secs.\n" ;
begin=end-begin;
};
unsigned long long begin;
};
/*
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 queries_per_connections=1;
int num_threads=1;
int count=0;
char *username=NULL;
char *password=NULL;
char *host=(char *)"localhost";
int port=3306;
int multiport=1;
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;
unsigned int status_connections = 0;
unsigned int connect_phase_completed = 0;
unsigned int query_phase_completed = 0;
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 arg_on=1;
int i, j;
char query[128];
unsigned long long b, e, ce;
MYSQL **mysqlconns=(MYSQL **)malloc(sizeof(MYSQL *)*count);
if (mysqlconns==NULL) {
exit(EXIT_FAILURE);
}
for (i=0; i<count; i++) {
MYSQL *mysql=mysql_init(NULL);
if (mysql==NULL) {
exit(EXIT_FAILURE);
}
MYSQL *rc=mysql_real_connect(mysql, host, username, password, schema, (local ? 0 : ( port + rand()%multiport ) ), NULL, 0);
if (rc==NULL) {
if (silent==0) {
fprintf(stderr,"%s\n", mysql_error(mysql));
}
exit(EXIT_FAILURE);
}
mysqlconns[i]=mysql;
__sync_add_and_fetch(&status_connections,1);
}
__sync_fetch_and_add(&connect_phase_completed,1);
while(__sync_fetch_and_add(&connect_phase_completed,0) != num_threads) {
}
MYSQL *mysql;
for (j=0; j<queries; j++) {
int r1=rand()%count;
//sprintf(query,"SELECT %d", r1);
sprintf(query,"SELECT LAST_INSERT_ID()");
if (j%queries_per_connections==0) {
mysql=mysqlconns[r1];
}
if (mysql_query(mysql,query)) {
if (silent==0) {
fprintf(stderr,"%s\n", mysql_error(mysql));
}
select_ERR++;
__sync_fetch_and_add(&g_select_ERR,1);
} else {
MYSQL_RES *result = mysql_store_result(mysql);
mysql_free_result(result);
select_OK++;
__sync_fetch_and_add(&g_select_OK,1);
}
}
__sync_fetch_and_add(&query_phase_completed,1);
return NULL;
}
int main(int argc, char *argv[]) {
pthread_mutex_init(&mutex,NULL);
int opt;
while ((opt = getopt(argc, argv, "H:kst:b:c:u:p:h:P:D:q:M:")) != -1) {
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 'b':
queries_per_connections = 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;
case 'h':
host = strdup(optarg);
break;
case 'D':
schema = strdup(optarg);
break;
case 'P':
port = atoi(optarg);
break;
case 'M':
multiport = atoi(optarg);
break;
case 'k':
keep_open = 1;
break;
case 's':
silent = 1;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s -c count -u username -p password [ -h host ] [ -P port ] [ -D schema ] [ -q queries ] [ -H {0|1|2|3} ] [ -s ] [ -k ] [ -t threads ] [ -b queries_per_connection ]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (
(queries_per_connections == 0) ||
(count == 0) ||
(username == NULL) ||
(password == NULL)
) {
fprintf(stderr, "Usage: %s -c count -u username -p password [ -h host ] [ -P port ] [ -D schema ] [ -q queries ] [ -H {0|1|2|3} ] [ -s ] [ -k ] [ -t threads ] [ -b queries_per_connections ]\n", argv[0]);
exit(EXIT_FAILURE);
}
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();
unsigned long long begin_conn;
unsigned long long end_conn;
unsigned long long begin_query;
unsigned long long end_query;
for (i=0; i<num_threads; i++) {
if ( pthread_create(&thi[i], NULL, my_conn_thread , NULL) != 0 )
perror("Thread creation");
}
{
i=0;
begin_conn = monotonic_time();
unsigned long long prev_time = begin_conn;
unsigned long long prev_conn = __sync_fetch_and_add(&status_connections,0);
while(__sync_fetch_and_add(&connect_phase_completed,0) != num_threads) {
usleep(10000);
i++;
if (i==50) {
unsigned long long curr_conn = __sync_fetch_and_add(&status_connections,0);
unsigned long long curr_time = monotonic_time();
//fprintf(stderr,"Connections: %d\n",__sync_fetch_and_add(&status_connections,0));
std::cerr << "Status : Created " << curr_conn << " total , new " << curr_conn - prev_conn << " connections in " << double( curr_time - prev_time ) / 1000 << " millisecs. : " << double((curr_conn-prev_conn)*1000000/(curr_time - prev_time) ) << " Conn/s\n" ;
i=0;
prev_conn = curr_conn;
prev_time = curr_time;
}
}
end_conn = monotonic_time();
std::cerr << "Created " << __sync_fetch_and_add(&status_connections,0) << " connections in " << double( end_conn - begin_conn ) / 1000 << " millisecs.\n" ;
}
{
i=0;
begin_query = monotonic_time();
unsigned long long prev_time = begin_query;
unsigned long long prev_conn = __sync_fetch_and_add(&g_select_OK,0);
while(__sync_fetch_and_add(&query_phase_completed,0) != num_threads) {
usleep(10000);
i++;
if (i==100) {
unsigned long long curr_conn = __sync_fetch_and_add(&g_select_OK,0);
unsigned long long curr_time = monotonic_time();
std::cerr << "Status : Executed " << curr_conn << " total , new " << curr_conn - prev_conn << " queries in " << double( curr_time - prev_time ) / 1000 << " millisecs. : " << double((curr_conn-prev_conn)*1000000/(curr_time - prev_time) ) << " QPS\n" ;
//fprintf(stderr,"Queries [OK/ERR]: %d / %d\n", __sync_fetch_and_add(&g_select_OK,0), __sync_fetch_and_add(&g_select_ERR,0));
i=0;
prev_conn = curr_conn;
prev_time = curr_time;
}
}
end_query = monotonic_time();
std::cerr << "Executed " << __sync_fetch_and_add(&g_select_OK,0) << " queries in " << double( end_query - begin_query ) / 1000 << " millisecs.\n" ;
}
for (i=0; i<num_threads; i++) {
pthread_join(thi[i], NULL);
}
std::cerr << "Created " << __sync_fetch_and_add(&status_connections,0) << " connections in " << double( end_conn - begin_conn ) / 1000 << " millisecs.\n" ;
std::cerr << "Executed " << __sync_fetch_and_add(&g_select_OK,0) << " queries in " << double( end_query - begin_query ) / 1000 << " millisecs.\n" ;
exit(EXIT_SUCCESS);
}