-
Notifications
You must be signed in to change notification settings - Fork 5
/
query_hyrise.c
151 lines (123 loc) · 4.06 KB
/
query_hyrise.c
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
#include "http.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "dbg.h"
int num_threads = 1;
int num_queries = 1;
#define BUFFER_SIZE 16384
unsigned timediff(struct timeval start, struct timeval stop) {
return (stop.tv_usec - start.tv_usec) + (stop.tv_sec - start.tv_sec) * 1000 * 1000;
}
struct query_hyrise_arg {
struct Host *host;
struct HttpRequest *request;
};
void *query_hyrise(void *arg) {
int i;
int success_counter = 0;
struct query_hyrise_arg *qha = (struct query_hyrise_arg *)arg;
int socket = http_open_connection(qha->host->url, qha->host->port);
struct HttpResponse *response;
int http_error = HTTP_SUCCESS;
char buf[BUFFERSIZE];
char http_post[] = "POST %s HTTP/1.1\r\n\
Content-Length: %d\r\n\r\n\
%s";
char *buf_s;
int allocatedBytes = asprintf(&buf_s, http_post, "/query", qha->request->content_length, qha->request->payload);
if (allocatedBytes == -1) {
log_err("An error occurred while creating response.");
exit(-1);
}
for (i = 0; i < num_queries/num_threads; ++i) {
// if ((http_error = http_send_request(socket, qha->request)) != HTTP_SUCCESS) {
// log_err("Error on send request\n");
// if (http_error == ERR_EOF || http_error == ERR_BROKEN_PIPE || http_error == ERR_CONNECTION_RESET) {
// i -= 1;
// close(socket);
// socket = http_open_connection(qha->host->url, qha->host->port);
// continue;
// }
// exit(-1);
// }
send(socket, buf_s, strlen(buf_s), 0);
read(socket, buf, BUFFERSIZE);
// if ((http_error = http_receive_response(socket, &response)) != HTTP_SUCCESS) {
// log_err("http error on response %d\n", http_error);
// if (http_error == ERR_EOF || http_error == ERR_BROKEN_PIPE || http_error == ERR_CONNECTION_RESET) {
// debug("Unexpected Connection close. Retry..");
// i -= 1;
// close(socket);
// socket = http_open_connection(qha->host->url, qha->host->port);
// continue;
// }
// exit(-1);
// } else {
// debug("Received: %s", response->payload);
// success_counter += 1;
// HttpResponse_free(response);
// }
}
free(buf_s);
close(socket);
return NULL;
}
int main(int argc, char *argv[]) {
signal(SIGPIPE, SIG_IGN);
if (argc != 6) {
printf("USAGE: ./query_hyrise HOST PORT NUM_QUERIES NUM_THREADS FILE\n");
return -1;
}
char *url = argv[1];
char *port = argv[2];
num_queries = atoi(argv[3]);
num_threads = atoi(argv[4]);
char *file_name = argv[5];
char query[BUFFER_SIZE];
FILE *f = fopen(file_name, "r");
if (f == NULL) {
printf("%s\n", strerror(errno));
exit(-1);
}
strncpy(query, "query=", strlen("query="));
size_t s = fread(&query[6], sizeof(char), BUFFER_SIZE-6, f);
if (ferror(f)) {
printf("ERROR reading query file\n");
return -1;
} else {
query[s + 6] = '\0';
}
fclose(f);
struct Host h;
h.url = url;
h.port = atoi(port);
struct HttpRequest r;
r.method = "POST";
r.resource = "/qu";
r.content_length = strlen(query);
r.payload = query;
struct query_hyrise_arg arg;
arg.host = &h;
arg.request = &r;
int i;
pthread_t *threads = malloc(num_threads * sizeof(pthread_t));
struct timeval query_start, query_end;
gettimeofday(&query_start, NULL);
for (i = 0; i < num_threads; ++i) {
pthread_create(threads + i, NULL, &query_hyrise, (void *)&arg);
}
for (i = 0; i < num_threads; ++i) {
pthread_join(*(threads + i), NULL);
}
free(threads);
gettimeofday(&query_end, NULL);
printf("%f queries/s\n", (num_queries * 1000 * 1000.0)/timediff(query_start, query_end));
return 0;
}