1150970Srwatson/*- 2150970Srwatson * Copyright (c) 2005 Robert N. M. Watson 3150970Srwatson * All rights reserved. 4150970Srwatson * 5150970Srwatson * Redistribution and use in source and binary forms, with or without 6150970Srwatson * modification, are permitted provided that the following conditions 7150970Srwatson * are met: 8150970Srwatson * 1. Redistributions of source code must retain the above copyright 9150970Srwatson * notice, this list of conditions and the following disclaimer. 10150970Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11150970Srwatson * notice, this list of conditions and the following disclaimer in the 12150970Srwatson * documentation and/or other materials provided with the distribution. 13150970Srwatson * 14150970Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15150970Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16150970Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17150970Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18150970Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19150970Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20150970Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21150970Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22150970Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23150970Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24150970Srwatson * SUCH DAMAGE. 25150970Srwatson * 26150970Srwatson * $FreeBSD$ 27150970Srwatson */ 28150970Srwatson 29150970Srwatson#include <sys/types.h> 30150970Srwatson#include <sys/fcntl.h> 31150970Srwatson#include <sys/ioctl.h> 32150970Srwatson#include <sys/select.h> 33150970Srwatson#include <sys/socket.h> 34213574Spluknet#include <sys/stdint.h> 35150970Srwatson 36150970Srwatson#include <netinet/in.h> 37150970Srwatson 38150970Srwatson#include <arpa/inet.h> 39150970Srwatson 40150970Srwatson#include <err.h> 41150970Srwatson#include <errno.h> 42150970Srwatson#include <signal.h> 43150970Srwatson#include <stdio.h> 44150970Srwatson#include <string.h> 45150970Srwatson#include <unistd.h> 46150970Srwatson 47150970Srwatson/* 48150970Srwatson * Simple micro-benchmark to see how many connections/second can be created 49150970Srwatson * in a serialized fashion against a given server. A timer signal is used 50150970Srwatson * to interrupt the loop and assess the cost. 51150970Srwatson */ 52150970Srwatson#define SECONDS 60 53150970Srwatson#define PORT 6060 54150970Srwatson 55150970Srwatsonstatic int timer_fired; 56150970Srwatson 57150970Srwatson/* 58150970Srwatson * Signal timer, which both interrupts the in-progress socket operation, and 59150970Srwatson * flags the timer as having fired so the main loop can exit. 60150970Srwatson */ 61150970Srwatsonstatic void 62150970Srwatsonalarm_handler(__unused int signum) 63150970Srwatson{ 64150970Srwatson 65150970Srwatson timer_fired = 1; 66150970Srwatson} 67150970Srwatson 68150970Srwatson/* 69150970Srwatson * Build a connection. In order to make sure the signal interrupts us as 70150970Srwatson * we wait, use non-blocking sockets and select(). Return 0 on success, or 71150970Srwatson * -1 if we were interrupted. Exit with a failure if we get an unspected 72150970Srwatson * error. 73150970Srwatson */ 74150970Srwatsonstatic int 75150970Srwatsontry_connect(struct sockaddr_in *sin) 76150970Srwatson{ 77150970Srwatson struct 78150970Srwatson fd_set read_set; 79150970Srwatson int i, s; 80150970Srwatson 81150970Srwatson s = socket(PF_INET, SOCK_STREAM, 0); 82150970Srwatson if (s < 0) 83150970Srwatson err(-1, "socket(PF_INET, SOCK_STREAM)"); 84150970Srwatson 85150970Srwatson i = 1; 86150970Srwatson if (fcntl(s, F_SETFL, FIONBIO, &i) < 0) 87150970Srwatson err(-1, "fcntl(s, FIOBIO, 1)"); 88150970Srwatson 89150970Srwatson FD_ZERO(&read_set); 90150970Srwatson FD_SET(s, &read_set); 91150970Srwatson 92150970Srwatson if (connect(s, (struct sockaddr *)sin, sizeof(*sin)) < 0 && 93150970Srwatson errno != EINPROGRESS) 94150970Srwatson err(-1, "connect(%s)", inet_ntoa(sin->sin_addr)); 95150970Srwatson 96150970Srwatson if (select(s + 1, &read_set, &read_set, &read_set, NULL) < 0) { 97150970Srwatson if ((errno == EINTR && !timer_fired) || (errno != EINTR)) 98150970Srwatson err(-1, "select"); 99150970Srwatson return (-1); 100150970Srwatson } 101150970Srwatson 102150970Srwatson close(s); 103150970Srwatson return (0); 104150970Srwatson} 105150970Srwatson 106150970Srwatson 107150970Srwatsonint 108150970Srwatsonmain(int argc, char *argv[]) 109150970Srwatson{ 110150970Srwatson struct sockaddr_in sin; 111150970Srwatson u_int64_t counter; 112150970Srwatson 113150970Srwatson if (argc != 2) 114213574Spluknet errx(-1, "usage: tcpconnect [ip]"); 115150970Srwatson 116150970Srwatson bzero(&sin, sizeof(sin)); 117150970Srwatson sin.sin_family = AF_INET; 118150970Srwatson sin.sin_len = sizeof(sin); 119150970Srwatson sin.sin_addr.s_addr = inet_addr(argv[1]); 120150970Srwatson sin.sin_port = htons(PORT); 121150970Srwatson 122150970Srwatson if (signal(SIGALRM, alarm_handler) == SIG_ERR) 123150970Srwatson err(-1, "signal(SIGALRM)"); 124150970Srwatson 125150970Srwatson alarm(SECONDS); 126150970Srwatson 127150970Srwatson counter = 0; 128150970Srwatson while (!timer_fired) { 129150970Srwatson if (try_connect(&sin) == 0) 130150970Srwatson counter++; 131150970Srwatson } 132213574Spluknet printf("%ju count\n", (uintmax_t)counter); 133213574Spluknet printf("%ju connections/second\n", (uintmax_t)(counter / SECONDS)); 134150970Srwatson 135150970Srwatson return (0); 136150970Srwatson} 137