tcpconnect.c revision 150970
1/*-
2 * Copyright (c) 2005 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/tools/tools/netrate/tcpconnect/tcpconnect.c 150970 2005-10-05 12:10:35Z rwatson $
27 */
28
29#include <sys/types.h>
30#include <sys/fcntl.h>
31#include <sys/ioctl.h>
32#include <sys/select.h>
33#include <sys/socket.h>
34
35#include <netinet/in.h>
36
37#include <arpa/inet.h>
38
39#include <err.h>
40#include <errno.h>
41#include <signal.h>
42#include <stdio.h>
43#include <string.h>
44#include <unistd.h>
45
46/*
47 * Simple micro-benchmark to see how many connections/second can be created
48 * in a serialized fashion against a given server.  A timer signal is used
49 * to interrupt the loop and assess the cost.
50 */
51#define	SECONDS	60
52#define	PORT	6060
53
54static int	timer_fired;
55
56/*
57 * Signal timer, which both interrupts the in-progress socket operation, and
58 * flags the timer as having fired so the main loop can exit.
59 */
60static void
61alarm_handler(__unused int signum)
62{
63
64	timer_fired = 1;
65}
66
67/*
68 * Build a connection.  In order to make sure the signal interrupts us as
69 * we wait, use non-blocking sockets and select().  Return 0 on success, or
70 * -1 if we were interrupted.  Exit with a failure if we get an unspected
71 * error.
72 */
73static int
74try_connect(struct sockaddr_in *sin)
75{
76	struct
77	fd_set read_set;
78	int i, s;
79
80	s = socket(PF_INET, SOCK_STREAM, 0);
81	if (s < 0)
82		err(-1, "socket(PF_INET, SOCK_STREAM)");
83
84	i = 1;
85	if (fcntl(s, F_SETFL, FIONBIO, &i) < 0)
86		err(-1, "fcntl(s, FIOBIO, 1)");
87
88	FD_ZERO(&read_set);
89	FD_SET(s, &read_set);
90
91	if (connect(s, (struct sockaddr *)sin, sizeof(*sin)) < 0 &&
92	    errno != EINPROGRESS)
93		err(-1, "connect(%s)", inet_ntoa(sin->sin_addr));
94
95	if (select(s + 1, &read_set, &read_set, &read_set, NULL) < 0) {
96		if ((errno == EINTR && !timer_fired) || (errno != EINTR))
97			err(-1, "select");
98		return (-1);
99	}
100
101	close(s);
102	return (0);
103}
104
105
106int
107main(int argc, char *argv[])
108{
109	struct sockaddr_in sin;
110	u_int64_t counter;
111
112	if (argc != 2)
113		err(-1, "usage: tcpconnect [ip]");
114
115	bzero(&sin, sizeof(sin));
116	sin.sin_family = AF_INET;
117	sin.sin_len = sizeof(sin);
118	sin.sin_addr.s_addr = inet_addr(argv[1]);
119	sin.sin_port = htons(PORT);
120
121	if (signal(SIGALRM, alarm_handler) == SIG_ERR)
122		err(-1, "signal(SIGALRM)");
123
124	alarm(SECONDS);
125
126	counter = 0;
127	while (!timer_fired) {
128		if (try_connect(&sin) == 0)
129			counter++;
130	}
131	printf("%llu count\n", counter);
132	printf("%llu connections/second\n", counter / SECONDS);
133
134	return (0);
135}
136