-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdboom.c
232 lines (196 loc) · 5.9 KB
/
dboom.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
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
/* dboom is an HTTP load generator written in C using libdill coroutines */
#include <libdill.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include "dboom.h"
#include "req.h"
#define DEFAULT_REQUESTS 10
#define DEFAULT_CONCURR 5
#define DEFAULT_TIMEOUT 5000 // ms
static unsigned int getRequests(const char*);
static unsigned int getConcurrentReqs(const char*);
static int getTimeout(const char*);
static void usage();
static struct reqstats reqstats_new();
coroutine void boom(const char*, unsigned int, int, int[2]);
coroutine void stats(int[2], int);
int main(int argc, char **argv) {
char *requests = NULL;
char *concurr = NULL;
char *timeout = NULL;
int verbose = 0;
int c;
while((c = getopt(argc, argv, "n:c:t:v")) != -1) {
switch(c) {
case 'n':
requests = optarg;
break;
case 'c':
concurr = optarg;
break;
case 't':
timeout = optarg;
break;
case 'v':
verbose = 1;
break;
default:
usage();
break; // Unreachable
}
}
/* Exit if no url provided */
if(optind == argc) usage();
/* Grab URL. TODO: Accept > 1 URL */
const char* url = argv[optind];
/* Validate program args */
unsigned int nreqs = getRequests(requests);
unsigned int nconcurr = getConcurrentReqs(concurr);
int ntimeout = getTimeout(timeout);
if(nreqs == 0 || nconcurr == 0) {
fprintf(stderr,
"The number of requests (%d) and the number of"
"concurrent requests (%d) must be greater than 0.\n",
nreqs, nconcurr);
exit(EXIT_FAILURE);
}
/* The number of requests cannot be less than the number of concurrent
requests. */
if(nreqs < nconcurr) {
fprintf(stderr,
"The number of requests (%d) cannot be less than the number of "
"concurrent requests (%d)\n", nreqs, nconcurr);
exit(EXIT_FAILURE);
}
printf("Running dboom\n\
Url: %s\n\
Total Requests: %d\n\
Concurrent Requests: %d\n\
Timeout: %d ms\n", url, nreqs, nconcurr, ntimeout);
/* Record start time */
time_t start_t, end_t;
time(&start_t);
int rc = 0;
/* Each boom() coroutine uses this channel to record statistics. */
int stats_ch[2];
rc = chmake(stats_ch);
if(rc != 0) {
perror("Failed to make stats channel");
exit(EXIT_FAILURE);
}
/* Launch coroutine for recording statistics */
int stats_bun = go(stats(stats_ch, verbose));
if(stats_bun < 0) {
perror("Could not start stats coroutine");
exit(EXIT_FAILURE);
}
/* Launch nconcurr coroutines, each one sending nreqs/nconcurr requests. */
int boom_bun = bundle();
if(boom_bun < 0) {
perror("Could not create bundle for boom coroutins.");
exit(EXIT_FAILURE);
}
int i;
for(i = 0; i < nconcurr; ++i) {
rc = bundle_go(boom_bun, boom(url, nreqs/nconcurr, ntimeout, stats_ch));
if(rc != 0) {
perror("Could not launch boom coroutine");
exit(EXIT_FAILURE);
}
}
/* Wait for boom() coroutines to end */
rc = bundle_wait(boom_bun, -1);
if(rc != 0) {
perror("Failed when waiting for boom coroutins to complete");
exit(EXIT_FAILURE);
}
/* Close stats channel, causing the stats coroutine to end */
rc = chdone(stats_ch[1]);
if(rc != 0) {
perror("Failed to close stats channel");
exit(EXIT_FAILURE);
}
/* Wait for stats() coroutine to end */
rc = bundle_wait(stats_bun, -1);
if(rc != 0) perror("Failed while waiting for stats coroutine to end");
/* Print run time */
time(&end_t);
printf("Run time: %fs\n", difftime(end_t, start_t));
exit(EXIT_SUCCESS);
}
coroutine void boom(const char* url, unsigned int nreqs, int timeout,
int stats_ch[2]) {
int rc = 0;
/* Send requests until no more requests */
int i;
for(i = nreqs; i > 0; --i) {
struct reqstats rs = reqstats_new();
if(MakeRequest(url, timeout, &rs) == 0) {
rc = chsend(stats_ch[1], &rs, sizeof(rs), -1);
if(rc != 0) {
perror("Failed to send request stats");
return;
}
}
}
}
coroutine void stats(int stats_ch[2], int verbose)
{
int rc = 0;
int nrequests = 0;
struct reqstats rs;
unsigned int total = 0;
while(1) {
rc = chrecv(stats_ch[0], &rs, sizeof(rs), -1);
if(rc != 0) {
if(errno != EPIPE) {
/* We expect to get EPIPE when main closes stats channel */
perror("Unexpected error reading stats channel");
}
/* Break out for any error */
break;
}
/* Request stats available */
nrequests++;
total += rs.tm;
if(verbose)
printf("%d,%ld\n", rs.http_code, rs.tm);
}
/* Display stats if we have something to display */
if(nrequests > 0)
printf("Avg response time for %d requests: %d ms\n", nrequests, total/nrequests);
}
/* Create and initialize a new reqstat struct */
static struct reqstats
reqstats_new()
{
struct reqstats rs;
rs.tm = 0;
rs.http_code = 0;
return rs;
}
static
void usage()
{
fprintf(stderr, "Usage:\n\tdboom [-n nreqs] [-c nconcurr] [-t timeoutms] [-v] URL.\n");
fprintf(stderr, "\t -v,\tOutput each request's stats.\n");
exit(EXIT_FAILURE);
}
static
unsigned int getRequests(const char *requests)
{
return requests ? atoi(requests) : DEFAULT_REQUESTS;
}
static
unsigned int getConcurrentReqs(const char *concurr)
{
return concurr ? atoi(concurr) : DEFAULT_CONCURR;
}
static
int getTimeout(const char *timeout)
{
return timeout ? atoi(timeout) : DEFAULT_TIMEOUT;
}