1 /* $NetBSD: bench_http.c,v 1.1.1.1 2013/04/11 16:43:33 christos Exp $ */
3 * Copyright 2008-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/resource.h>
46 #include "event2/event.h"
47 #include "event2/buffer.h"
48 #include "event2/util.h"
49 #include "event2/http.h"
50 #include "event2/thread.h"
52 static void http_basic_cb(struct evhttp_request
*req
, void *arg
);
55 static size_t content_len
= 0;
58 http_basic_cb(struct evhttp_request
*req
, void *arg
)
60 struct evbuffer
*evb
= evbuffer_new();
62 evbuffer_add(evb
, content
, content_len
);
64 /* allow sending of an empty reply */
65 evhttp_send_reply(req
, HTTP_OK
, "Everything is fine", evb
);
70 #if LIBEVENT_VERSION_NUMBER >= 0x02000200
72 http_ref_cb(struct evhttp_request
*req
, void *arg
)
74 struct evbuffer
*evb
= evbuffer_new();
76 evbuffer_add_reference(evb
, content
, content_len
, NULL
, NULL
);
78 /* allow sending of an empty reply */
79 evhttp_send_reply(req
, HTTP_OK
, "Everything is fine", evb
);
86 main(int argc
, char **argv
)
88 struct event_config
*cfg
= event_config_new();
89 struct event_base
*base
;
94 unsigned short port
= 8080;
99 WSAStartup(0x101, &WSAData
);
101 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
105 for (i
= 1; i
< argc
; ++i
) {
111 if ((c
== 'p' || c
== 'l') && i
+ 1 >= argc
) {
112 fprintf(stderr
, "-%c requires argument.\n", c
);
118 if (i
+1 >= argc
|| !argv
[i
+1]) {
119 fprintf(stderr
, "Missing port\n");
122 port
= (int)strtol(argv
[i
+1], &endptr
, 10);
123 if (*endptr
!= '\0') {
124 fprintf(stderr
, "Bad port\n");
129 if (i
+1 >= argc
|| !argv
[i
+1]) {
130 fprintf(stderr
, "Missing content length\n");
133 content_len
= (size_t)strtol(argv
[i
+1], &endptr
, 10);
134 if (*endptr
!= '\0' || content_len
== 0) {
135 fprintf(stderr
, "Bad content length\n");
142 evthread_use_windows_threads();
143 event_config_set_flag(cfg
,EVENT_BASE_FLAG_STARTUP_IOCP
);
147 fprintf(stderr
, "Illegal argument \"%c\"\n", c
);
152 base
= event_base_new_with_config(cfg
);
154 fprintf(stderr
, "creating event_base failed. Exiting.\n");
158 http
= evhttp_new(base
);
160 content
= malloc(content_len
);
161 if (content
== NULL
) {
162 fprintf(stderr
, "Cannot allocate content\n");
166 for (i
= 0; i
< (int)content_len
; ++i
)
167 content
[i
] = (i
& 255);
170 evhttp_set_cb(http
, "/ind", http_basic_cb
, NULL
);
171 fprintf(stderr
, "/ind - basic content (memory copy)\n");
173 #ifdef _EVENT2_EVENT_H_
174 evhttp_set_cb(http
, "/ref", http_ref_cb
, NULL
);
175 fprintf(stderr
, "/ref - basic content (reference)\n");
178 fprintf(stderr
, "Serving %d bytes on port %d using %s\n",
179 (int)content_len
, port
,
180 use_iocp
? "IOCP" : event_base_get_method(base
));
182 evhttp_bind_socket(http
, "0.0.0.0", port
);
185 struct timeval tv
={99999999,0};
186 event_base_loopexit(base
, &tv
);
188 event_base_dispatch(base
);