]> granicus.if.org Git - libevent/commitdiff
http benchmark: add a way to change the size of the buffer; also use add_reference
authorNiels Provos <provos@gmail.com>
Sun, 12 Apr 2009 00:38:31 +0000 (00:38 +0000)
committerNiels Provos <provos@gmail.com>
Sun, 12 Apr 2009 00:38:31 +0000 (00:38 +0000)
svn:r1158

test/bench_http.c

index 90d28b6aa591742b29211ffe565836c24faf5d3a..44c5716d9bd4adb10f522260977666a979fad40b 100644 (file)
 
 static void http_basic_cb(struct evhttp_request *req, void *arg);
 
-static char content[4096];
+static char *content;
+static size_t content_len;
 
 static void
 http_basic_cb(struct evhttp_request *req, void *arg)
 {
        struct evbuffer *evb = evbuffer_new();
 
-       evbuffer_add(evb, content, sizeof(content));
+       evbuffer_add(evb, content, content_len);
 
        /* allow sending of an empty reply */
        evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
@@ -68,20 +69,42 @@ http_basic_cb(struct evhttp_request *req, void *arg)
        evbuffer_free(evb);
 }
 
+/* cheasy way of detecting evbuffer_add_reference */
+#ifdef _EVENT2_EVENT_H_
+static void
+http_ref_cb(struct evhttp_request *req, void *arg)
+{
+       struct evbuffer *evb = evbuffer_new();
+
+       evbuffer_add_reference(evb, content, content_len, NULL, NULL);
+
+       /* allow sending of an empty reply */
+       evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
+
+       evbuffer_free(evb);
+}
+#endif
+
 int
 main (int argc, char **argv)
 {
        struct event_base *base = event_base_new();
        struct evhttp *http = evhttp_new(base);
-       struct evhttp *virtual = evhttp_new(base);
        int c;
 
        unsigned short port = 8080;
-       while ((c = getopt(argc, argv, "p:")) != -1) {
+       while ((c = getopt(argc, argv, "p:l:")) != -1) {
                switch (c) {
                case 'p':
                        port = atoi(optarg);
                        break;
+               case 'l':
+                       content_len = atol(optarg);
+                       if (content_len == 0) {
+                               fprintf(stderr, "Bad content length\n");
+                               exit(1);
+                       }
+                       break;
                default:
                        fprintf(stderr, "Illegal argument \"%c\"\n", c);
                        exit(1);
@@ -93,9 +116,26 @@ main (int argc, char **argv)
                return (1);
 #endif
 
-       evhttp_set_cb(http, "/index.html", http_basic_cb, NULL);
-       evhttp_set_cb(virtual, "/index.html", http_basic_cb, NULL);
-       evhttp_add_virtual_host(http, "foo.com", virtual);
+       content = malloc(content_len);
+       if (content == NULL) {
+               fprintf(stderr, "Cannot allocate content\n");
+               exit(1);
+       } else {
+               int i = 0;
+               for (i = 0; i < content_len; ++i)
+                       content[i] = (i & 255);
+       }
+
+       evhttp_set_cb(http, "/ind", http_basic_cb, NULL);
+       fprintf(stderr, "/ind - basic content (memory copy)\n");
+
+#ifdef _EVENT2_EVENT_H_
+       evhttp_set_cb(http, "/ref", http_ref_cb, NULL);
+       fprintf(stderr, "/ref - basic content (reference)\n");
+#endif
+
+       fprintf(stderr, "Serving %d bytes on port %d\n",
+           (int)content_len, port);
 
        evhttp_bind_socket(http, "0.0.0.0", port);