1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 ** This program is based on ZeusBench V1.0 written by Adam Twiss
61 ** which is Copyright (c) 1996 by Zeus Technology Ltd. http://www.zeustech.net/
63 ** This software is provided "as is" and any express or implied waranties,
64 ** including but not limited to, the implied warranties of merchantability and
65 ** fitness for a particular purpose are disclaimed. In no event shall
66 ** Zeus Technology Ltd. be liable for any direct, indirect, incidental, special,
67 ** exemplary, or consequential damaged (including, but not limited to,
68 ** procurement of substitute good or services; loss of use, data, or profits;
69 ** or business interruption) however caused and on theory of liability. Whether
70 ** in contract, strict liability or tort (including negligence or otherwise)
71 ** arising in any way out of the use of this software, even if advised of the
72 ** possibility of such damage.
78 ** - Originally written by Adam Twiss <adam@zeus.co.uk>, March 1996
79 ** with input from Mike Belshe <mbelshe@netscape.com> and
80 ** Michael Campanella <campanella@stevms.enet.dec.com>
81 ** - Enhanced by Dean Gaudet <dgaudet@apache.org>, November 1997
82 ** - Cleaned up by Ralf S. Engelschall <rse@apache.org>, March 1998
83 ** - POST and verbosity by Kurt Sussman <kls@merlot.com>, August 1998
84 ** - HTML table output added by David N. Welton <davidw@prosa.it>, January 1999
85 ** - Added Cookie, Arbitrary header and auth support. <dirkx@webweaving.org>, April 1999
93 * - has various other poor buffer attacks related to the lazy parsing of
94 * response headers from the server
95 * - doesn't implement much of HTTP/1.x, only accepts certain forms of
97 * - (performance problem) heavy use of strstr shows up top in profile
98 * only an issue for loopback usage
101 #define AB_VERSION "1.3c"
103 /* -------------------------------------------------------------------- */
106 /* Hmmm... This source code isn't being compiled in ASCII.
107 * In order for data that flows over the network to make
108 * sense, we need to translate to/from ASCII.
113 /* affects include files on Solaris */
116 #include "apr_network_io.h"
117 #include "apr_file_io.h"
118 #include "apr_time.h"
119 #include "apr_getopt.h"
120 #include "ap_base64.h"
122 #include "apr_xlate.h"
137 /* ------------------- DEFINITIONS -------------------------- */
139 /* maximum number of requests on a time limited test */
140 #define MAX_REQUESTS 50000
142 /* good old state hostname */
143 #define STATE_UNCONNECTED 0
144 #define STATE_CONNECTING 1
147 #define CBUFFSIZE 2048
150 apr_socket_t *aprsock;
152 int read; /* amount of bytes read */
153 int bread; /* amount of body read */
154 int length; /* Content-Length value used for keep-alive */
155 char cbuff[CBUFFSIZE]; /* a buffer to store server response header */
156 int cbx; /* offset in cbuffer */
157 int keepalive; /* non-zero if a keep-alive request */
158 int gotheader; /* non-zero if we have the entire header in
160 apr_time_t start, connect, done;
165 int read; /* number of bytes read */
166 int ctime; /* time in ms to connect */
167 int time; /* time in ms for connection */
170 #define ap_min(a,b) ((a)<(b))?(a):(b)
171 #define ap_max(a,b) ((a)>(b))?(a):(b)
173 /* --------------------- GLOBALS ---------------------------- */
175 int verbosity = 0; /* no verbosity by default */
176 int posting = 0; /* GET by default */
177 int requests = 1; /* Number of requests to make */
178 int concurrency = 1; /* Number of multiple requests to make */
179 int tlimit = 0; /* time limit in cs */
180 int keepalive = 0; /* try and do keepalive connections */
181 char servername[1024]; /* name that server reports */
182 char hostname[1024]; /* host name */
183 char path[1024]; /* path name */
184 char postfile[1024]; /* name of file containing post data */
185 char *postdata; /* *buffer containing data from postfile */
186 apr_ssize_t postlen = 0; /* length of data to be POSTed */
187 char content_type[1024]; /* content type to put in POST header */
188 char cookie[1024], /* optional cookie line */
189 auth[1024], /* optional (basic/uuencoded)
190 * authentification */
191 hdrs[4096]; /* optional arbitrary headers */
192 int port = 80; /* port number */
193 time_t aprtimeout = 30 * APR_USEC_PER_SEC; /* timeout value */
195 int use_html = 0; /* use html in the report */
200 int doclen = 0; /* the length the document should be */
201 int totalread = 0; /* total number of bytes read */
202 int totalbread = 0; /* totoal amount of entity body read */
203 int totalposted = 0; /* total number of bytes posted, inc. headers */
204 int done = 0; /* number of requests we have done */
205 int doneka = 0; /* number of keep alive connections done */
206 int started = 0; /* number of requests started, so no excess */
207 int good = 0, bad = 0; /* number of good and bad requests */
209 /* store error cases */
210 int err_length = 0, err_conn = 0, err_except = 0;
211 int err_response = 0;
213 apr_time_t start, endtime;
215 /* global request (and its length) */
219 /* one global throw-away buffer to read stuff into */
222 struct connection *con; /* connection array */
223 struct data *stats; /* date for each request */
226 apr_pollfd_t *readbits;
228 apr_xlate_t *from_ascii, *to_ascii;
231 /* --------------------------------------------------------- */
233 /* simple little function to write an error string and exit */
235 static void err(char *s)
237 fprintf(stderr, "%s", s);
241 /* simple little function to write an APR error string and exit */
243 static void apr_err(char *s, apr_status_t rv)
249 s, apr_strerror(rv, buf, sizeof buf), rv);
253 /* --------------------------------------------------------- */
255 /* write out request to a connection - assumes we can write
256 (small) request out in one go into our new socket buffer */
258 static void write_request(struct connection *c)
260 apr_ssize_t len = reqlen;
261 c->connect = apr_now();
262 apr_setsocketopt(c->aprsock, APR_SO_TIMEOUT, 30 * APR_USEC_PER_SEC);
263 if (apr_send(c->aprsock, request, &reqlen) != APR_SUCCESS ||
265 printf("Send request failed!\n");
268 apr_send(c->aprsock, postdata, &postlen);
269 totalposted += (reqlen + postlen);
272 c->state = STATE_READ;
273 apr_add_poll_socket(readbits, c->aprsock, APR_POLLIN);
276 /* --------------------------------------------------------- */
278 /* calculate and output results */
280 static void output_results(void)
285 timetaken = (endtime - start) / 1000;
288 printf("Server Software: %s\n", servername);
289 printf("Server Hostname: %s\n", hostname);
290 printf("Server Port: %d\n", port);
292 printf("Document Path: %s\n", path);
293 printf("Document Length: %d bytes\n", doclen);
295 printf("Concurrency Level: %d\n", concurrency);
296 printf("Time taken for tests: %d.%03d seconds\n",
297 timetaken / 1000, timetaken % 1000);
298 printf("Complete requests: %d\n", done);
299 printf("Failed requests: %d\n", bad);
301 printf(" (Connect: %d, Length: %d, Exceptions: %d)\n",
302 err_conn, err_length, err_except);
304 printf("Non-2xx responses: %d\n", err_response);
306 printf("Keep-Alive requests: %d\n", doneka);
307 printf("Total transferred: %d bytes\n", totalread);
309 printf("Total POSTed: %d\n", totalposted);
310 printf("HTML transferred: %d bytes\n", totalbread);
312 /* avoid divide by zero */
314 printf("Requests per second: %.2f\n", 1000 * (float) (done) / timetaken);
315 printf("Transfer rate: %.2f kb/s received\n",
316 (float) (totalread) / timetaken);
318 printf(" %.2f kb/s sent\n",
319 (float) (totalposted) / timetaken);
320 printf(" %.2f kb/s total\n",
321 (float) (totalread + totalposted) / timetaken);
326 /* work out connection times */
328 int totalcon = 0, total = 0;
329 int mincon = 9999999, mintot = 999999;
330 int maxcon = 0, maxtot = 0;
332 for (i = 0; i < requests; i++) {
333 struct data s = stats[i];
334 mincon = ap_min(mincon, s.ctime);
335 mintot = ap_min(mintot, s.time);
336 maxcon = ap_max(maxcon, s.ctime);
337 maxtot = ap_max(maxtot, s.time);
341 if (requests > 0) { /* avoid division by zero (if 0 requests) */
342 printf("\nConnnection Times (ms)\n");
343 printf(" min avg max\n");
344 printf("Connect: %5d %5d %5d\n", mincon, totalcon / requests, maxcon);
345 printf("Processing: %5d %5d %5d\n",
346 mintot - mincon, (total / requests) - (totalcon / requests),
348 printf("Total: %5d %5d %5d\n", mintot, total / requests, maxtot);
353 /* --------------------------------------------------------- */
355 /* calculate and output results in HTML */
357 static void output_html_results(void)
362 timetaken = (endtime - start) / 1000;
364 printf("\n\n<table %s>\n", tablestring);
365 printf("<tr %s><th colspan=2 %s>Server Software:</th>"
366 "<td colspan=2 %s>%s</td></tr>\n",
367 trstring, tdstring, tdstring, servername);
368 printf("<tr %s><th colspan=2 %s>Server Hostname:</th>"
369 "<td colspan=2 %s>%s</td></tr>\n",
370 trstring, tdstring, tdstring, hostname);
371 printf("<tr %s><th colspan=2 %s>Server Port:</th>"
372 "<td colspan=2 %s>%d</td></tr>\n",
373 trstring, tdstring, tdstring, port);
374 printf("<tr %s><th colspan=2 %s>Document Path:</th>"
375 "<td colspan=2 %s>%s</td></tr>\n",
376 trstring, tdstring, tdstring, path);
377 printf("<tr %s><th colspan=2 %s>Document Length:</th>"
378 "<td colspan=2 %s>%d bytes</td></tr>\n",
379 trstring, tdstring, tdstring, doclen);
380 printf("<tr %s><th colspan=2 %s>Concurrency Level:</th>"
381 "<td colspan=2 %s>%d</td></tr>\n",
382 trstring, tdstring, tdstring, concurrency);
383 printf("<tr %s><th colspan=2 %s>Time taken for tests:</th>"
384 "<td colspan=2 %s>%d.%03d seconds</td></tr>\n",
385 trstring, tdstring, tdstring, timetaken / 1000, timetaken % 1000);
386 printf("<tr %s><th colspan=2 %s>Complete requests:</th>"
387 "<td colspan=2 %s>%d</td></tr>\n",
388 trstring, tdstring, tdstring, done);
389 printf("<tr %s><th colspan=2 %s>Failed requests:</th>"
390 "<td colspan=2 %s>%d</td></tr>\n",
391 trstring, tdstring, tdstring, bad);
393 printf("<tr %s><td colspan=4 %s > (Connect: %d, Length: %d, Exceptions: %d)</td></tr>\n",
394 trstring, tdstring, err_conn, err_length, err_except);
396 printf("<tr %s><th colspan=2 %s>Non-2xx responses:</th>"
397 "<td colspan=2 %s>%d</td></tr>\n",
398 trstring, tdstring, tdstring, err_response);
400 printf("<tr %s><th colspan=2 %s>Keep-Alive requests:</th>"
401 "<td colspan=2 %s>%d</td></tr>\n",
402 trstring, tdstring, tdstring, doneka);
403 printf("<tr %s><th colspan=2 %s>Total transferred:</th>"
404 "<td colspan=2 %s>%d bytes</td></tr>\n",
405 trstring, tdstring, tdstring, totalread);
407 printf("<tr %s><th colspan=2 %s>Total POSTed:</th>"
408 "<td colspan=2 %s>%d</td></tr>\n",
409 trstring, tdstring, tdstring, totalposted);
410 printf("<tr %s><th colspan=2 %s>HTML transferred:</th>"
411 "<td colspan=2 %s>%d bytes</td></tr>\n",
412 trstring, tdstring, tdstring, totalbread);
414 /* avoid divide by zero */
416 printf("<tr %s><th colspan=2 %s>Requests per second:</th>"
417 "<td colspan=2 %s>%.2f</td></tr>\n",
418 trstring, tdstring, tdstring, 1000 * (float) (done) / timetaken);
419 printf("<tr %s><th colspan=2 %s>Transfer rate:</th>"
420 "<td colspan=2 %s>%.2f kb/s received</td></tr>\n",
421 trstring, tdstring, tdstring, (float) (totalread) / timetaken);
423 printf("<tr %s><td colspan=2 %s> </td>"
424 "<td colspan=2 %s>%.2f kb/s sent</td></tr>\n",
425 trstring, tdstring, tdstring,
426 (float) (totalposted) / timetaken);
427 printf("<tr %s><td colspan=2 %s> </td>"
428 "<td colspan=2 %s>%.2f kb/s total</td></tr>\n",
429 trstring, tdstring, tdstring,
430 (float) (totalread + totalposted) / timetaken);
435 /* work out connection times */
437 int totalcon = 0, total = 0;
438 int mincon = 9999999, mintot = 999999;
439 int maxcon = 0, maxtot = 0;
441 for (i = 0; i < requests; i++) {
442 struct data s = stats[i];
443 mincon = ap_min(mincon, s.ctime);
444 mintot = ap_min(mintot, s.time);
445 maxcon = ap_max(maxcon, s.ctime);
446 maxtot = ap_max(maxtot, s.time);
451 if (requests > 0) { /* avoid division by zero (if 0 requests) */
452 printf("<tr %s><th %s colspan=4>Connnection Times (ms)</th></tr>\n",
454 printf("<tr %s><th %s> </th> <th %s>min</th> <th %s>avg</th> <th %s>max</th></tr>\n",
455 trstring, tdstring, tdstring, tdstring, tdstring);
456 printf("<tr %s><th %s>Connect:</th>"
459 "<td %s>%5d</td></tr>\n",
460 trstring, tdstring, tdstring, mincon, tdstring, totalcon / requests, tdstring, maxcon);
461 printf("<tr %s><th %s>Processing:</th>"
464 "<td %s>%5d</td></tr>\n",
465 trstring, tdstring, tdstring, mintot - mincon, tdstring,
466 (total / requests) - (totalcon / requests), tdstring, maxtot - maxcon);
467 printf("<tr %s><th %s>Total:</th>"
470 "<td %s>%5d</td></tr>\n",
471 trstring, tdstring, tdstring, mintot, tdstring, total / requests, tdstring, maxtot);
473 printf("</table>\n");
477 /* --------------------------------------------------------- */
479 /* start asnchronous non-blocking connection */
481 static void start_connect(struct connection *c)
485 if(!(started < requests)) return;
493 if ((rv = apr_create_tcp_socket(&c->aprsock, cntxt)) != APR_SUCCESS) {
494 apr_err("Socket:", rv);
496 if ((rv = apr_set_remote_port(c->aprsock, port)) != APR_SUCCESS) {
497 apr_err("Port:", rv);
499 c->start = apr_now();
500 if ((rv = apr_connect(c->aprsock, hostname)) != APR_SUCCESS) {
501 if (apr_canonical_error(rv) == APR_EINPROGRESS) {
502 c->state = STATE_CONNECTING;
503 apr_add_poll_socket(readbits, c->aprsock, APR_POLLOUT);
507 apr_remove_poll_socket(readbits, c->aprsock);
508 apr_close_socket(c->aprsock);
512 "\nTest aborted after 10 failures\n\n");
513 apr_err("apr_connect()", rv);
520 /* connected first time */
525 /* --------------------------------------------------------- */
527 /* close down connection and save stats */
529 static void close_connection(struct connection *c)
531 if (c->read == 0 && c->keepalive) {
532 /* server has legitimately shut down an idle keep alive request */
533 if (good) good--; /* connection never happened */
537 /* first time here */
540 else if (c->bread != doclen) {
545 if (done < requests) {
549 s.ctime = (c->connect - c->start) / 1000;
550 s.time = (c->done - c->start) / 1000;
555 apr_remove_poll_socket(readbits, c->aprsock);
556 apr_close_socket(c->aprsock);
563 /* --------------------------------------------------------- */
565 /* read data from connection */
567 static void read_connection(struct connection *c)
572 char respcode[4]; /* 3 digits and null */
575 apr_setsocketopt(c->aprsock, APR_SO_TIMEOUT, aprtimeout);
576 status = apr_recv(c->aprsock, buffer, &r);
577 if (r == 0 || (status != 0 && apr_canonical_error(status) != APR_EAGAIN)) {
583 if (apr_canonical_error(status) == APR_EAGAIN)
592 int space = CBUFFSIZE - c->cbx - 1; /* -1 to allow for 0 terminator */
593 int tocopy = (space < r) ? space : r;
595 apr_size_t inbytes_left = space, outbytes_left = space;
597 status = apr_xlate_conv_buffer(from_ascii, buffer, &inbytes_left,
598 c->cbuff + c->cbx, &outbytes_left);
599 if (status || inbytes_left || outbytes_left) {
600 fprintf(stderr, "only simple translation is supported (%d/%u/%u)\n",
601 status, inbytes_left, outbytes_left);
605 memcpy(c->cbuff + c->cbx, buffer, space);
606 #endif /*NOT_ASCII */
609 c->cbuff[c->cbx] = 0; /* terminate for benefit of strstr */
610 if (verbosity >= 4) {
611 printf("LOG: header received:\n%s\n", c->cbuff);
613 s = strstr(c->cbuff, "\r\n\r\n");
614 /* this next line is so that we talk to NCSA 1.5 which blatantly
615 * breaks the http specifaction
618 s = strstr(c->cbuff, "\n\n");
623 /* read rest next time */
628 /* header is in invalid or too big - close connection */
629 apr_remove_poll_socket(readbits, c->aprsock);
630 apr_close_socket(c->aprsock);
633 err("\nTest aborted after 10 failures\n\n");
639 /* have full header */
641 /* this is first time, extract some interesting info */
643 p = strstr(c->cbuff, "Server:");
653 /* XXX: this parsing isn't even remotely HTTP compliant...
654 * but in the interest of speed it doesn't totally have to be,
655 * it just needs to be extended to handle whatever servers
656 * folks want to test against. -djg */
658 /* check response code */
659 part = strstr(c->cbuff, "HTTP"); /* really HTTP/1.x_ */
660 strncpy(respcode, (part + strlen("HTTP/1.x_")), 3);
662 if (respcode[0] != '2') {
665 printf("WARNING: Response code not 2xx (%s)\n", respcode);
667 else if (verbosity >= 3) {
668 printf("LOG: Response code = %s\n", respcode);
672 *s = 0; /* terminate at end of header */
674 (strstr(c->cbuff, "Keep-Alive")
675 || strstr(c->cbuff, "keep-alive"))) { /* for benefit of MSIIS */
677 cl = strstr(c->cbuff, "Content-Length:");
678 /* handle NCSA, which sends Content-length: */
680 cl = strstr(c->cbuff, "Content-length:");
683 c->length = atoi(cl + 16);
686 c->bread += c->cbx - (s + l - c->cbuff) + r - tocopy;
687 totalbread += c->bread;
691 /* outside header, everything we have read is entity body */
696 if (c->keepalive && (c->bread >= c->length)) {
697 /* finished a keep-alive connection */
702 /* first time here */
705 else if (c->bread != doclen) {
709 if (done < requests) {
713 s.ctime = (c->connect - c->start) / 1000;
714 s.time = (c->done - c->start) / 1000;
721 c->read = c->bread = 0;
723 c->start = c->connect; /* zero connect time with keep-alive */
727 /* --------------------------------------------------------- */
731 static void test(void)
734 apr_interval_time_t timeout;
739 apr_size_t inbytes_left, outbytes_left;
743 printf("Benchmarking %s (be patient)...", hostname);
749 con = malloc(concurrency * sizeof(struct connection));
750 memset(con, 0, concurrency * sizeof(struct connection));
752 stats = malloc(requests * sizeof(struct data));
753 apr_setup_poll(&readbits, concurrency, cntxt);
757 sprintf(request, "%s %s HTTP/1.0\r\n"
758 "User-Agent: ApacheBench/%s\r\n"
763 (posting == 0) ? "GET" : "HEAD",
766 keepalive ? "Connection: Keep-Alive\r\n" : "",
767 cookie, auth, hostname, hdrs);
770 sprintf(request, "POST %s HTTP/1.0\r\n"
771 "User-Agent: ApacheBench/%s\r\n"
775 "Content-length: %d\r\n"
776 "Content-type: %s\r\n"
781 keepalive ? "Connection: Keep-Alive\r\n" : "",
784 (content_type[0]) ? content_type : "text/plain", hdrs);
788 printf("INFO: POST header == \n---\n%s\n---\n", request);
790 reqlen = strlen(request);
793 inbytes_left = outbytes_left = reqlen;
794 status = apr_xlate_conv_buffer(to_ascii, request, &inbytes_left,
795 request, &outbytes_left);
796 if (status || inbytes_left || outbytes_left) {
797 fprintf(stderr, "only simple translation is supported (%d/%u/%u)\n",
798 status, inbytes_left, outbytes_left);
803 /* ok - lets start */
806 /* initialise lots of requests */
807 for (i = 0; i < concurrency; i++) {
809 start_connect(&con[i]);
812 while (done < requests) {
816 /* check for time limit expiry */
818 timed = (now - start) / APR_USEC_PER_SEC;
819 if (tlimit && timed > (tlimit * 1000)) {
820 requests = done; /* so stats are correct */
822 /* Timeout of 30 seconds. */
823 timeout = 30 * APR_USEC_PER_SEC;
826 status = apr_poll(readbits, &n, timeout);
827 if (status != APR_SUCCESS)
828 apr_err("apr_poll", status);
831 err("\nServer timed out\n\n");
834 for (i = 0; i < concurrency; i++) {
835 apr_get_revents(&rv, con[i].aprsock, readbits);
837 /* Note: APR_POLLHUP is set after FIN is received on some
838 * systems, so treat that like APR_POLLIN so that we try
841 if ((rv & APR_POLLERR) || (rv & APR_POLLNVAL)) {
844 start_connect(&con[i]);
847 if ((rv & APR_POLLIN) || (rv & APR_POLLPRI) || (rv & APR_POLLHUP))
848 read_connection(&con[i]);
849 if (rv & APR_POLLOUT)
850 write_request(&con[i]);
854 output_html_results();
859 /* ------------------------------------------------------- */
861 /* display copyright information */
862 static void copyright(void)
865 printf("This is ApacheBench, Version %s\n", AB_VERSION " <$Revision: 1.25 $> apache-2.0");
866 printf("Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/\n");
867 printf("Copyright (c) 1998-2000 The Apache Software Foundation, http://www.apache.org/\n");
872 printf(" This is ApacheBench, Version %s <i><%s></i> apache-2.0<br>\n", AB_VERSION, "$Revision: 1.25 $");
873 printf(" Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/<br>\n");
874 printf(" Copyright (c) 1998-2000 The Apache Software Foundation, http://www.apache.org/<br>\n");
875 printf("</p>\n<p>\n");
879 /* display usage information */
880 static void usage(char *progname)
882 fprintf(stderr, "Usage: %s [options] [http://]hostname[:port]/path\n", progname);
883 fprintf(stderr, "Options are:\n");
884 fprintf(stderr, " -n requests Number of requests to perform\n");
885 fprintf(stderr, " -c concurrency Number of multiple requests to make\n");
886 fprintf(stderr, " -t timelimit Seconds to max. wait for responses\n");
887 fprintf(stderr, " -p postfile File containg data to POST\n");
888 fprintf(stderr, " -T content-type Content-type header for POSTing\n");
889 fprintf(stderr, " -v verbosity How much troubleshooting info to print\n");
890 fprintf(stderr, " -w Print out results in HTML tables\n");
891 fprintf(stderr, " -i Use HEAD instead of GET\n");
892 fprintf(stderr, " -x attributes String to insert as table attributes\n");
893 fprintf(stderr, " -y attributes String to insert as tr attributes\n");
894 fprintf(stderr, " -z attributes String to insert as td or th attributes\n");
895 fprintf(stderr, " -C attribute Add cookie, eg. 'Apache=1234. (repeatable)\n");
896 fprintf(stderr, " -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: zop'\n");
897 fprintf(stderr, " Inserted after all normal header lines. (repeatable)\n");
898 fprintf(stderr, " -A attribute Add Basic WWW Authentication, the attributes\n");
899 fprintf(stderr, " are a colon separated username and password.\n");
900 fprintf(stderr, " -p attribute Add Basic Proxy Authentication, the attributes\n");
901 fprintf(stderr, " are a colon separated username and password.\n");
902 fprintf(stderr, " -V Print version number and exit\n");
903 fprintf(stderr, " -k Use HTTP KeepAlive feature\n");
904 fprintf(stderr, " -h Display usage information (this message)\n");
908 /* ------------------------------------------------------- */
910 /* split URL into parts */
912 static int parse_url(char *url)
916 char *p = NULL; /* points to port if url has it */
918 if (strlen(url) > 7 && strncmp(url, "http://", 7) == 0)
921 if ((cp = strchr(url, ':')) != NULL) {
926 if ((cp = strchr(url, '/')) == NULL)
936 /* ------------------------------------------------------- */
938 /* read data to POST from file, save contents and length */
940 static int open_postfile(char *pfile)
942 apr_file_t *postfd = NULL;
944 apr_fileperms_t mode = APR_OS_DEFAULT;
947 if (apr_open(&postfd, pfile, APR_READ, mode, cntxt) != APR_SUCCESS) {
948 printf("Invalid postfile name (%s)\n", pfile);
952 apr_getfileinfo(&finfo, postfd);
953 postlen = finfo.size;
954 postdata = (char *)malloc(postlen);
956 printf("Can\'t alloc postfile buffer\n");
960 if (apr_read(postfd, postdata, &length) != APR_SUCCESS &&
962 printf("error reading postfilen");
968 /* ------------------------------------------------------- */
970 /* sort out command-line args and call test */
971 int main(int argc, char **argv)
983 tdstring = "bgcolor=white";
989 atexit(apr_terminate);
990 apr_create_pool(&cntxt, NULL);
993 status = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, cntxt);
995 fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", status);
998 status = apr_xlate_open(&from_ascii, APR_DEFAULT_CHARSET, "ISO8859-1", cntxt);
1000 fprintf(stderr, "apr_xlate_open(from ASCII)->%d\n", status);
1003 status = ap_base64init_ebcdic(to_ascii, from_ascii);
1005 fprintf(stderr, "ap_base64init_ebcdic()->%d\n", status);
1010 apr_initopt(&opt, cntxt, argc, argv);
1011 while ((status = apr_getopt(opt, "n:c:t:T:p:v:kVhwix:y:z:C:H:P:A:", &c, &optarg)) == APR_SUCCESS) {
1014 requests = atoi(optarg);
1016 err("Invalid number of requests\n");
1023 concurrency = atoi(optarg);
1027 err("Cannot mix POST and HEAD\n");
1032 err("Cannot mix POST and HEAD\n");
1034 if (0 == (r = open_postfile(optarg))) {
1037 else if (postdata) {
1042 verbosity = atoi(optarg);
1045 tlimit = atoi(optarg);
1046 requests = MAX_REQUESTS; /* need to size data array on something */
1049 strcpy(content_type, optarg);
1052 strncat(cookie, "Cookie: ", sizeof(cookie));
1053 strncat(cookie, optarg, sizeof(cookie));
1054 strncat(cookie, "\r\n", sizeof(cookie));
1057 /* assume username passwd already to be in colon separated form.
1058 * Ready to be uu-encoded.
1060 while(isspace(*optarg))
1062 l=ap_base64encode(tmp, optarg, strlen(optarg));
1065 strncat(auth, "Authorization: basic ", sizeof(auth));
1066 strncat(auth, tmp, sizeof(auth));
1067 strncat(auth, "\r\n", sizeof(auth));
1071 * assume username passwd already to be in colon separated form.
1073 while(isspace(*optarg))
1075 l=ap_base64encode(tmp, optarg, strlen(optarg));
1078 strncat(auth, "Proxy-Authorization: basic ", sizeof(auth));
1079 strncat(auth, tmp, sizeof(auth));
1080 strncat(auth, "\r\n", sizeof(auth));
1083 strncat(hdrs, optarg, sizeof(hdrs));
1084 strncat(hdrs, "\r\n", sizeof(hdrs));
1090 * if any of the following three are used, turn on html output
1095 tablestring = optarg;
1114 if (opt->ind != argc - 1) {
1115 fprintf(stderr, "%s: wrong number of arguments\n", argv[0]);
1119 if (parse_url((char*)opt->argv[opt->ind++])) {
1120 fprintf(stderr, "%s: invalid URL\n", argv[0]);