Vaguely useful tool for measuring the rate at which a server accepts
[fwd] / blast.c
CommitLineData
a8ceabf6 1#include <assert.h>
2#include <ctype.h>
3#include <errno.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include <sys/types.h>
9#include <sys/time.h>
10#include <unistd.h>
11#include <sys/socket.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
15
16#include <mLib/alloc.h>
17#include <mLib/conn.h>
18#include <mLib/mdwopt.h>
19#include <mLib/quis.h>
20#include <mLib/report.h>
21#include <mLib/sel.h>
22#include <mLib/tv.h>
23
24typedef struct blast {
25 conn c;
26 sel_timer t;
27} blast;
28
29static struct sockaddr_in sin;
30static sel_state sel;
31static struct timeval ctv = { 0, 500000 };
32static sel_timer sec;
33static unsigned count = 0;
34
35static void timers(void);
36
37static void stats(struct timeval *tv, void *p)
38{
39 static char baton[5] = "/-\\|";
40 static char *bt = baton;
41 struct timeval ttv;
42 printf("\r%c %u connections/second", *bt++, count);
43 if (!*bt)
44 bt = baton;
45 fflush(stdout);
46 count = 0;
47 TV_ADDL(&ttv, tv, 1, 0);
48 sel_addtimer(&sel, &sec, &ttv, stats, p);
49}
50
51static void newconn(blast *b);
52
53static void retry(struct timeval *tv, void *p)
54{
55 blast *b = p;
56 newconn(b);
57}
58
59static void backoff(blast *b)
60{
61 struct timeval tv;
62 unsigned r = rand();
63 double q = RAND_MAX / 5;
64 gettimeofday(&tv, 0);
65 TV_ADDL(&tv, &tv, r / q, (r % (unsigned)q) * (MILLION / q));
66 sel_addtimer(&sel, &b->t, &tv, retry, b);
67}
68
69static void connected(int fd, void *p)
70{
71 blast *b = p;
72 sel_rmtimer(&b->t);
73 if (fd == -1)
74 backoff(b);
75 else {
76 count++;
77 close(fd);
78 newconn(b);
79 }
80}
81
82static void timeout(struct timeval *tv, void *p)
83{
84 blast *b = p;
85 conn_kill(&b->c);
86 newconn(b);
87}
88
89static void timers(void)
90{
91 struct tab { void (*func)(struct timeval *tv, void *p); const char *name; }
92 tab[] = { { retry, "retry" }, {timeout, "timeout"}, { stats, "stats" }, { 0,
93 0 }};
94 sel_timer *t = sel.timers;
95 while (t) {
96 struct tab *q; for (q = tab; q->func != t->func; q++) ;
97 assert(t->prev->next == t);
98 printf("%i.%06i %p %s\n", t->tv.tv_sec, t->tv.tv_usec, t->p, q->name);
99 assert(t != t->next);
100 t = t->next;
101 }
102 puts("");
103}
104
105static void newconn(blast *b)
106{
107 int fd = socket(PF_INET, SOCK_STREAM, 0);
108 struct timeval tv;
109 if (fd < 0)
110 goto fail;
111 gettimeofday(&tv, 0);
112 TV_ADD(&tv, &tv, &ctv);
113 sel_addtimer(&sel, &b->t, &tv, timeout, b);
114 conn_init(&b->c, &sel, fd, (struct sockaddr *)&sin, sizeof(sin),
115 connected, b);
116 return;
117
118fail:
119 backoff(b);
120}
121
122int main(int argc, char *argv[])
123{
124 blast *b;
125 size_t n = 256;
126
127 ego(argv[0]);
128
129 for (;;) {
130 int i = getopt(argc, argv, "t:n:");
131 if (i < 0)
132 break;
133 switch (i) {
134 case 't': {
135 double t = strtod(optarg, 0);
136 double s = modf(t, &t);
137 ctv.tv_sec = t;
138 ctv.tv_usec = s * MILLION;
139 } break;
140 case 'n':
141 n = atoi(optarg);
142 break;
143 default:
144 exit(1);
145 }
146 }
147
148 argv += optind;
149 argc -= optind;
150 if (argc != 2) {
151 pquis(stderr, "Usage: $ [-t time] [-n count] host port\n");
152 exit(1);
153 }
154
155 sel_init(&sel);
156 sin.sin_family = AF_INET;
157
158 {
159 struct hostent *h = gethostbyname(argv[0]);
160 if (!h)
161 die(1, "bad hostname `%s'", argv[0]);
162 memcpy(&sin.sin_addr, h->h_addr, sizeof(struct in_addr));
163 }
164
165 if (isdigit((unsigned char)argv[1][0]))
166 sin.sin_port = htons(atoi(argv[1]));
167 else {
168 struct servent *s = getservbyname(argv[1], "tcp");
169 if (!s)
170 die(1, "bad service name `%s'", argv[1]);
171 sin.sin_port = s->s_port;
172 }
173
174 b = xmalloc(n * sizeof(blast));
175
176 {
177 int i;
178 for (i = 0; i < n; i++)
179 newconn(&b[i]);
180 }
181
182 {
183 struct timeval tv;
184 gettimeofday(&tv, 0);
185 tv.tv_sec++;
186 sel_addtimer(&sel, &sec, &tv, stats, 0);
187 }
188
189 for (;;) {
190 sel_select(&sel);
191 }
192}