forked from kazuho/manymanythreads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testhttpclient.c
161 lines (142 loc) · 4.04 KB
/
testhttpclient.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
#include <arpa/inet.h>
#include <assert.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include "picoev.h"
static int active_connections = 1000;
static int num_connections = 10000;
static int num_requests = 1000000;
static int fix_clients = 0;
static struct in_addr host;
static unsigned short port = 11111;
static int fds[1048576];
static int next_fd_idx = 0;
static int num_responses;
#define RES_SIZE (sizeof( \
"HTTP\1.0 200 OK\r\n" \
"Connection: keep-alive\r\n" \
"Content-Length: 13\r\n" \
"Content-Type: text/plain\r\n" \
"\r\n" \
"hello world!\n") - 1)
static void write_req(int fd)
{
int r;
#define REQ "GET / HTTP/1.1\r\n" \
"Host: 127.0.0.1\r\n" \
"User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; ja-JP-mac; rv:1.9.1.3) Gecko/20090824\r\n" \
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" \
"Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n" \
"Accept-Encoding: gzip,deflate\r\n" \
"Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n" \
"Keep-Alive: 300\r\n" \
"Connection: keep-alive\r\n" \
"\r\n"
r = write(fd, REQ, sizeof(REQ) - 1);
assert(r == sizeof(REQ) - 1);
#undef REQ
}
static void read_cb(picoev_loop* loop, int fd, int revents, void* cb_arg)
{
char buf[RES_SIZE];
int r;
assert((revents & PICOEV_READ) != 0);
r = read(fd, buf, sizeof(buf));
assert(r == sizeof(buf));
++num_responses;
if (num_responses < num_requests) {
int wfd;
if (fix_clients) {
wfd = fd;
} else {
wfd = fds[next_fd_idx];
next_fd_idx = (next_fd_idx + 1) % num_connections;
}
write_req(wfd);
}
}
int main(int argc, char** argv)
{
int ch, i, r;
picoev_loop* loop;
struct timeval start_at, end_at;
double elapsed;
host.s_addr = htonl(0x7f000001);
while ((ch = getopt(argc, argv, "a:c:n:fh:p:")) != -1) {
switch (ch) {
case 'a':
assert(sscanf(optarg, "%d", &active_connections) == 1);
break;
case 'c':
assert(sscanf(optarg, "%d", &num_connections) == 1);
break;
case 'n':
assert(sscanf(optarg, "%d", &num_requests) == 1);
break;
case 'f':
fix_clients = 1;
break;
case 'h':
if (inet_aton(optarg, &host) == 0) {
struct hostent* h = gethostbyname(optarg);
assert(h != NULL && "host not found");
assert(h->h_addrtype == AF_INET);
assert(h->h_length == sizeof(host));
memcpy(&host, h->h_addr_list[0], sizeof(host));
}
break;
case 'p':
assert(sscanf(optarg, "%hu", &port) == 1);
break;
default:
exit(1);
}
}
picoev_init(num_connections + 10);
loop = picoev_create_loop(60);
/* setup connections */
for (i = 0; i < num_connections; ++i) {
int on;
struct sockaddr_in addr;
int fd = socket(AF_INET, SOCK_STREAM, 0);
assert(fd != -1 && "socket(2) failed");
on = 1;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
memcpy(&addr.sin_addr, &host, sizeof(host));
r = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
if (r == -1) {
perror("could not connect to server");
exit(2);
}
r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
assert(r == 0);
r = fcntl(fd, F_SETFL, O_NONBLOCK);
assert(r == 0);
picoev_add(loop, fd, PICOEV_READ, 0, read_cb, NULL);
fds[i] = fd;
usleep(1000);
}
gettimeofday(&start_at, NULL);
/* fire first active_connections */
for (i = 0; i < active_connections; ++i) {
write_req(fds[next_fd_idx]);
next_fd_idx = (next_fd_idx + 1) % num_connections;
}
/* the main loop */
while (num_responses < num_requests) {
picoev_loop_once(loop, 10);
}
gettimeofday(&end_at, NULL);
elapsed = end_at.tv_sec + end_at.tv_usec / 1000000.0
- (start_at.tv_sec + start_at.tv_usec / 1000000.0);
printf("%f reqs./sec. (%d in %f seconds)\n", num_responses / elapsed,
num_responses, elapsed);
return 0;
}