12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
17 #include <mLib/alloc.h>
18 #include <mLib/conn.h>
19 #include <mLib/macros.h>
20 #include <mLib/mdwopt.h>
21 #include <mLib/quis.h>
22 #include <mLib/report.h>
26 typedef struct blast {
32 static struct sockaddr_in sin;
34 static struct timeval ctv = { 0, 500000 };
36 static unsigned count = 0;
38 static void IGNORABLE timers(void);
40 static void stats(struct timeval *tv, void *p)
42 static char baton[5] = "/-\\|";
43 static char *bt = baton;
45 printf("\r%c %u connections/second", *bt++, count);
50 TV_ADDL(&ttv, tv, 1, 0);
51 sel_addtimer(&sel, &sec, &ttv, stats, p);
54 static void newconn(blast *b);
56 static void retry(struct timeval *tv, void *p)
62 static void backoff(blast *b)
66 double q = RAND_MAX / 5;
68 TV_ADDL(&tv, &tv, r / q, (r % (unsigned)q) * (MILLION / q));
69 sel_addtimer(&sel, &b->t, &tv, retry, b);
72 static void connected(int fd, void *p)
85 static void timeout(struct timeval *tv, void *p)
92 static void timers(void)
94 struct tab { void (*func)(struct timeval *tv, void *p); const char *name; }
95 tab[] = { { retry, "retry" }, {timeout, "timeout"}, { stats, "stats" }, { 0,
97 sel_timer *t = sel.timers;
99 struct tab *q; for (q = tab; q->func != t->func; q++) ;
100 /* assert(t->prev->next == t); */
101 printf("%lu.%06lu %p %s\n", t->tv.tv_sec, t->tv.tv_usec, t->p, q->name);
102 assert(t != t->next);
108 static void newconn(blast *b)
110 int fd = socket(PF_INET, SOCK_STREAM, 0);
114 gettimeofday(&tv, 0);
115 TV_ADD(&tv, &tv, &ctv);
116 sel_addtimer(&sel, &b->t, &tv, timeout, b);
117 if (conn_init(&b->c, &sel, fd, (struct sockaddr *)&sin, sizeof(sin),
127 int main(int argc, char *argv[])
135 int i = getopt(argc, argv, "t:n:");
140 double t = strtod(optarg, 0);
141 double s = modf(t, &t);
143 ctv.tv_usec = s * MILLION;
156 pquis(stderr, "Usage: $ [-t TIME] [-n COUNT] HOST PORT\n");
161 sin.sin_family = AF_INET;
164 struct hostent *h = gethostbyname(argv[0]);
166 die(1, "bad hostname `%s'", argv[0]);
167 memcpy(&sin.sin_addr, h->h_addr, sizeof(struct in_addr));
170 if (isdigit((unsigned char)argv[1][0]))
171 sin.sin_port = htons(atoi(argv[1]));
173 struct servent *s = getservbyname(argv[1], "tcp");
175 die(1, "bad service name `%s'", argv[1]);
176 sin.sin_port = s->s_port;
179 b = xmalloc(n * sizeof(blast));
183 for (i = 0; i < n; i++)
189 gettimeofday(&tv, 0);
191 sel_addtimer(&sel, &sec, &tv, stats, 0);