"Fossies" - the Fresh Open Source Software Archive 
Member "httperf-0.9.0/src/idleconn.c" (7 Apr 2007, 5401 Bytes) of package /linux/www/old/httperf-0.9.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "idleconn.c" see the
Fossies "Dox" file reference documentation.
1 /*
2 httperf -- a tool for measuring web server performance
3 Copyright 2000-2007 Hewlett-Packard Company and Contributors listed in
4 AUTHORS file. Originally contributed by David Mosberger-Tang
5
6 This file is part of httperf, a web server performance measurment
7 tool.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 In addition, as a special exception, the copyright holders give
15 permission to link the code of this work with the OpenSSL project's
16 "OpenSSL" library (or with modified versions of it that use the same
17 license as the "OpenSSL" library), and distribute linked combinations
18 including the two. You must obey the GNU General Public License in
19 all respects for all of the code used other than "OpenSSL". If you
20 modify this file, you may extend this exception to your version of the
21 file, but you are not obligated to do so. If you do not wish to do
22 so, delete this exception statement from your version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 02110-1301, USA
33 */
34
35 #include <errno.h>
36 #include <netdb.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45
46 #include <sys/time.h>
47 #include <sys/types.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50
51 const char *prog_name;
52 unsigned long num_conn, num_closed;
53 struct timeval start_time;
54
55 void
56 sigint_handler (int signal)
57 {
58 struct timeval stop_time;
59 double delta_t;
60
61 gettimeofday (&stop_time, NULL);
62
63 delta_t = ((stop_time.tv_sec - start_time.tv_sec)
64 + 1e-6*(stop_time.tv_usec - start_time.tv_usec));
65
66 printf ("%s: total # conn. created = %lu, close() rate = %g conn/sec\n",
67 prog_name, num_conn, num_closed / delta_t);
68 exit (0);
69 }
70
71 int
72 main (int argc, char **argv)
73 {
74 int desired, current = 0, port, sd, max_sd = 0, n, i;
75 struct sockaddr_in sin, server_addr;
76 fd_set readable, rdfds;
77 struct rlimit rlimit;
78 struct hostent *he;
79 char *server;
80
81 signal (SIGINT, sigint_handler);
82
83 prog_name = strrchr (argv[0], '/');
84 if (prog_name)
85 ++prog_name;
86 else
87 prog_name = argv[0];
88
89 memset (&rdfds, 0, sizeof (rdfds));
90
91 if (argc != 4)
92 {
93 fprintf (stderr, "Usage: %s server port numidle\n", prog_name);
94 exit (-1);
95 }
96
97 server = argv[1];
98 port = atoi (argv[2]);
99 desired = atoi (argv[3]);
100
101 /* boost open file limit to the max: */
102 if (getrlimit (RLIMIT_NOFILE, &rlimit) < 0)
103 {
104 fprintf (stderr, "%s: failed to get number of open file limit: %s",
105 prog_name, strerror (errno));
106 exit (1);
107 }
108
109 if (rlimit.rlim_max > FD_SETSIZE)
110 {
111 fprintf (stderr, "%s: warning: open file limit > FD_SETSIZE; "
112 "limiting max. # of open files to FD_SETSIZE\n", prog_name);
113 rlimit.rlim_max = FD_SETSIZE;
114 }
115
116 rlimit.rlim_cur = rlimit.rlim_max;
117 if (setrlimit (RLIMIT_NOFILE, &rlimit) < 0)
118 {
119 fprintf (stderr, "%s: failed to increase number of open file limit: %s",
120 prog_name, strerror (errno));
121 exit (1);
122 }
123
124 printf ("%s: creating and maintaining %d idle connections\n",
125 prog_name, desired);
126
127 memset (&server_addr, 0, sizeof (server_addr));
128 server_addr.sin_family = AF_INET;
129 server_addr.sin_port = htons (port);
130
131 he = gethostbyname (server);
132 if (he)
133 {
134 if (he->h_addrtype != AF_INET || he->h_length != sizeof (sin.sin_addr))
135 {
136 perror (server);
137 exit (-1);
138 }
139 memcpy (&server_addr.sin_addr, he->h_addr_list[0],
140 sizeof (server_addr.sin_addr));
141 }
142 else
143 if (!inet_aton (server, &server_addr.sin_addr))
144 {
145 fprintf (stderr, "%s: invalid server address %s\n", prog_name, server);
146 exit (-1);
147 }
148
149 gettimeofday (&start_time, NULL);
150
151 while (1)
152 {
153 while (current < desired)
154 {
155 /* create more idle connections */
156 sd = socket (AF_INET, SOCK_STREAM, 0);
157 if (sd < 0)
158 {
159 perror ("socket");
160 exit (-1);
161 }
162
163 sin = server_addr;
164 if (connect (sd, &sin, sizeof (sin)) < 0)
165 {
166 printf("connect: %s\n", strerror (errno));
167 switch (errno)
168 {
169 case ECONNREFUSED:
170 /* wait for server to start up... */
171 usleep (1000000);
172 case ETIMEDOUT:
173 close (sd);
174 continue;
175
176 default:
177 perror ("connect");
178 exit (-1);
179 }
180 }
181
182 if (sd > max_sd)
183 max_sd = sd;
184
185 #if DEBUG > 1
186 printf ("created %d\n", sd);
187 #endif
188 ++num_conn;
189 ++current;
190 FD_SET(sd, &rdfds);
191 }
192
193 readable = rdfds;
194 n = select (max_sd + 1, &readable, NULL, NULL, NULL);
195 for (i = 0; i <= max_sd; ++i)
196 {
197 if (FD_ISSET (i, &readable))
198 {
199 #if DEBUG > 1
200 printf ("closed %d\n", i);
201 #endif
202 close (i);
203 --current;
204 --n;
205 ++num_closed;
206 FD_CLR(i, &rdfds);
207 }
208 if (n <= 0)
209 break;
210 }
211 }
212 }