]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/tcp-banger2.c
2 #define FD_SETSIZE 1024
10 #include <sys/types.h>
11 #include <sys/select.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
20 #define PROXY_PORT 3128
21 #define PROXY_ADDR "127.0.0.1"
23 #define READ_BUF_SZ 4096
25 static int proxy_port
= PROXY_PORT
;
26 static char *proxy_addr
= PROXY_ADDR
;
27 static char *progname
;
30 static int opt_ims
= 0;
31 static int max_connections
= 64;
32 static time_t lifetime
= 60;
33 static struct timeval now
;
35 typedef void (CB
) (int, void *);
43 struct _f FD
[MAX_FDS
];
53 struct tm
*gmt
= gmtime(t
);
55 (void) strftime(buf
, 127, "%A, %d-%b-%y %H:%M:%S GMT", gmt
);
67 while (FD
[fd
].cb
== NULL
)
74 fd_open(int fd
, CB
* cb
, void *data
)
78 FD
[fd
].start
= now
.tv_sec
;
88 printf("\rWaiting for open connections to finish...\n");
93 read_reply(int fd
, void *data
)
95 static char buf
[READ_BUF_SZ
];
96 if (read(fd
, buf
, READ_BUF_SZ
) <= 0) {
111 struct sockaddr_in S
;
112 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
116 memset(&S
, '\0', sizeof(struct sockaddr_in
));
117 S
.sin_family
= AF_INET
;
118 S
.sin_port
= htons(proxy_port
);
119 S
.sin_addr
.s_addr
= inet_addr(proxy_addr
);
120 if (connect(s
, (struct sockaddr
*) &S
, sizeof(S
)) < 0) {
128 strcat(buf
, " HTTP/1.0\r\n");
129 strcat(buf
, "Accept: */*\r\n");
130 if (opt_ims
&& (lrand48() & 0x03) == 0) {
131 w
= time(NULL
) - (lrand48() & 0x3FFFF);
132 strcat(buf
, "If-Modified-Since: ");
133 strcat(buf
, mkrfc850(&w
));
138 if (write(s
, buf
, len
) < 0) {
144 * if (fcntl(s, F_SETFL, O_NDELAY) < 0)
145 * perror("fcntl O_NDELAY");
151 read_url(int fd
, void *junk
)
153 static char buf
[8192];
156 if (fgets(buf
, 8191, stdin
) == NULL
) {
157 printf("Done Reading URLS\n");
161 if ((t
= strchr(buf
, '\n')))
165 max_connections
= nfds
- 1;
166 printf("NOTE: max_connections set at %d\n", max_connections
);
168 fd_open(s
, read_reply
, NULL
);
174 fprintf(stderr
, "usage: %s: -p port -h host -n max\n", progname
);
186 struct timeval start
;
189 setbuf(stdout
, NULL
);
190 setbuf(stderr
, NULL
);
191 progname
= strdup(argv
[0]);
192 gettimeofday(&start
, NULL
);
194 while ((c
= getopt(argc
, argv
, "p:h:n:il:")) != -1) {
197 proxy_port
= atoi(optarg
);
200 proxy_addr
= strdup(optarg
);
203 max_connections
= atoi(optarg
);
209 lifetime
= (time_t) atoi(optarg
);
216 fd_open(0, read_url
, NULL
);
217 signal(SIGINT
, sig_intr
);
222 if (nfds
< max_connections
&& FD
[0].cb
)
224 for (i
= 1; i
<= maxfd
; i
++) {
225 if (FD
[i
].cb
== NULL
)
227 if (now
.tv_sec
- FD
[i
].start
> lifetime
) {
233 if (select(maxfd
+ 1, &R
, NULL
, NULL
, &to
) < 0) {
238 for (i
= 0; i
<= maxfd
; i
++) {
239 if (!FD_ISSET(i
, &R
))
241 FD
[i
].cb(i
, FD
[i
].data
);
243 gettimeofday(&now
, NULL
);
244 if (now
.tv_sec
> last
.tv_sec
) {
246 dt
= (int) (now
.tv_sec
- start
.tv_sec
);
247 printf("T+ %6d: %9d req (%+4d), %4d conn, %3d/sec avg\n",
252 (int) (nrequests
/ dt
));