-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_benchmark.c
More file actions
158 lines (116 loc) · 3.87 KB
/
sql_benchmark.c
File metadata and controls
158 lines (116 loc) · 3.87 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mysql/mysql.h>
#include "include/generator.h"
#include "include/tsc_x86.h"
#include <unistd.h>
/*
USAGE GUIDE
$sudo apt install default-libmysqlclient-dev
$make sql
$sudo ./sql_benchmark S_rows R_rows
*/
void finish_with_error(MYSQL *con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
// void insert_tuple()
int main(int argc, char **argv) {
if(argc != 3){
printf("Expected S_rows and R_rows!\n");
exit(1);
}
size_t S_rows = atoi(argv[1]);
size_t R_rows = atoi(argv[2]);
srand(time(NULL));
printf("MySQL client version: %s\n", mysql_get_client_info());
MYSQL *con = mysql_init(NULL);
if (con == NULL){
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "root", "root_passwd",
"benchmarking", 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if (mysql_query(con, "DROP TABLE IF EXISTS r")) {
finish_with_error(con);
}
if (mysql_query(con, "CREATE TABLE r(a INT, b INT, c INT, d INT) ENGINE = MEMORY")) {
finish_with_error(con);
}
if (mysql_query(con, "DROP TABLE IF EXISTS s")) {
finish_with_error(con);
}
if (mysql_query(con, "CREATE TABLE s(a INT, b INT, c INT, d INT) ENGINE = MEMORY")) {
finish_with_error(con);
}
// for(size_t i = 0; i < S_rows;++i){
// // for(size_t j = 0; j < 4; ++j);
// uint32_t a = rand();
// uint32_t b = rand();
// uint32_t c = rand();
// uint32_t d = rand();
// char query_buffer[1024];
// snprintf(query_buffer, sizeof(query_buffer), "INSERT INTO s VALUES (%u, %u,%u,%u)", a, b,c,d);
// if (mysql_query(con, query_buffer)) {
// finish_with_error(con);
// }
// }
for(size_t i = 0; i < R_rows;++i){
// for(size_t j = 0; j < 4; ++j);
uint32_t a = rand();
uint32_t b = rand();
uint32_t c = rand();
uint32_t d = rand();
char query_buffer[1024];
snprintf(query_buffer, sizeof(query_buffer), "INSERT INTO r VALUES (%u, %u,%u,%u)", a, b,c,d);
if (mysql_query(con, query_buffer)) {
finish_with_error(con);
}
}
printf("Finished writing data!\n");
MYSQL_RES *result;
myInt64 start,end;
//Warumup
for(size_t i = 0; i < 1;++i){
start = start_tsc();
if (mysql_query(con, "SELECT r.a, s.a FROM r,s WHERE r.a \% s.b = s.c")) {
finish_with_error(con);
}
// if (mysql_query(con, "SELECT r.a, s.a FROM r,s WHERE r.a = s.a")) {
// finish_with_error(con);
// }
// if (mysql_query(con, "SELECT r.a FROM r WHERE r.a > r.b")) {
// finish_with_error(con);
// }
end = stop_tsc(start);
printf("%llu\n",end);
result = mysql_store_result(con);
mysql_free_result(result);
}
for(size_t i = 0; i < 10;++i){
start = start_tsc();
if (mysql_query(con, "SELECT r.a FROM r,s WHERE r.a \% s.b = s.c")) {
finish_with_error(con);
}
// if (mysql_query(con, "SELECT r.a, s.a FROM r,s WHERE r.a = s.a")) {
// finish_with_error(con);
// }
// if (mysql_query(con, "SELECT r.a FROM r WHERE r.a > r.b")) {
// finish_with_error(con);
// }
end = stop_tsc(start);
printf("%llu\n",end);
result = mysql_store_result(con);
mysql_free_result(result);
}
mysql_close(con);
return 0;
}