ipc/chromium/src/third_party/libevent/test/bench_http.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/test/bench_http.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,191 @@
     1.4 +/*
     1.5 + * Copyright 2008-2012 Niels Provos and Nick Mathewson
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 2. Redistributions in binary form must reproduce the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer in the
    1.14 + *    documentation and/or other materials provided with the distribution.
    1.15 + * 4. The name of the author may not be used to endorse or promote products
    1.16 + *    derived from this software without specific prior written permission.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +#include <sys/types.h>
    1.32 +#include <sys/stat.h>
    1.33 +#ifdef WIN32
    1.34 +#include <winsock2.h>
    1.35 +#else
    1.36 +#include <sys/socket.h>
    1.37 +#include <sys/resource.h>
    1.38 +#include <sys/time.h>
    1.39 +#include <unistd.h>
    1.40 +#endif
    1.41 +#include <fcntl.h>
    1.42 +#include <signal.h>
    1.43 +#include <stdlib.h>
    1.44 +#include <stdio.h>
    1.45 +#include <string.h>
    1.46 +#include <errno.h>
    1.47 +
    1.48 +#include "event2/event.h"
    1.49 +#include "event2/buffer.h"
    1.50 +#include "event2/util.h"
    1.51 +#include "event2/http.h"
    1.52 +#include "event2/thread.h"
    1.53 +
    1.54 +static void http_basic_cb(struct evhttp_request *req, void *arg);
    1.55 +
    1.56 +static char *content;
    1.57 +static size_t content_len = 0;
    1.58 +
    1.59 +static void
    1.60 +http_basic_cb(struct evhttp_request *req, void *arg)
    1.61 +{
    1.62 +	struct evbuffer *evb = evbuffer_new();
    1.63 +
    1.64 +	evbuffer_add(evb, content, content_len);
    1.65 +
    1.66 +	/* allow sending of an empty reply */
    1.67 +	evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
    1.68 +
    1.69 +	evbuffer_free(evb);
    1.70 +}
    1.71 +
    1.72 +#if LIBEVENT_VERSION_NUMBER >= 0x02000200
    1.73 +static void
    1.74 +http_ref_cb(struct evhttp_request *req, void *arg)
    1.75 +{
    1.76 +	struct evbuffer *evb = evbuffer_new();
    1.77 +
    1.78 +	evbuffer_add_reference(evb, content, content_len, NULL, NULL);
    1.79 +
    1.80 +	/* allow sending of an empty reply */
    1.81 +	evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
    1.82 +
    1.83 +	evbuffer_free(evb);
    1.84 +}
    1.85 +#endif
    1.86 +
    1.87 +int
    1.88 +main(int argc, char **argv)
    1.89 +{
    1.90 +	struct event_config *cfg = event_config_new();
    1.91 +	struct event_base *base;
    1.92 +	struct evhttp *http;
    1.93 +	int i;
    1.94 +	int c;
    1.95 +	int use_iocp = 0;
    1.96 +	unsigned short port = 8080;
    1.97 +	char *endptr = NULL;
    1.98 +
    1.99 +#ifdef WIN32
   1.100 +	WSADATA WSAData;
   1.101 +	WSAStartup(0x101, &WSAData);
   1.102 +#else
   1.103 +	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
   1.104 +		return (1);
   1.105 +#endif
   1.106 +
   1.107 +	for (i = 1; i < argc; ++i) {
   1.108 +		if (*argv[i] != '-')
   1.109 +			continue;
   1.110 +
   1.111 +		c = argv[i][1];
   1.112 +
   1.113 +		if ((c == 'p' || c == 'l') && i + 1 >= argc) {
   1.114 +			fprintf(stderr, "-%c requires argument.\n", c);
   1.115 +			exit(1);
   1.116 +		}
   1.117 +
   1.118 +		switch (c) {
   1.119 +		case 'p':
   1.120 +			if (i+1 >= argc || !argv[i+1]) {
   1.121 +				fprintf(stderr, "Missing port\n");
   1.122 +				exit(1);
   1.123 +			}
   1.124 +			port = (int)strtol(argv[i+1], &endptr, 10);
   1.125 +			if (*endptr != '\0') {
   1.126 +				fprintf(stderr, "Bad port\n");
   1.127 +				exit(1);
   1.128 +			}
   1.129 +			break;
   1.130 +		case 'l':
   1.131 +			if (i+1 >= argc || !argv[i+1]) {
   1.132 +				fprintf(stderr, "Missing content length\n");
   1.133 +				exit(1);
   1.134 +			}
   1.135 +			content_len = (size_t)strtol(argv[i+1], &endptr, 10);
   1.136 +			if (*endptr != '\0' || content_len == 0) {
   1.137 +				fprintf(stderr, "Bad content length\n");
   1.138 +				exit(1);
   1.139 +			}
   1.140 +			break;
   1.141 +#ifdef WIN32
   1.142 +		case 'i':
   1.143 +			use_iocp = 1;
   1.144 +			evthread_use_windows_threads();
   1.145 +			event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP);
   1.146 +			break;
   1.147 +#endif
   1.148 +		default:
   1.149 +			fprintf(stderr, "Illegal argument \"%c\"\n", c);
   1.150 +			exit(1);
   1.151 +		}
   1.152 +	}
   1.153 +
   1.154 +	base = event_base_new_with_config(cfg);
   1.155 +	if (!base) {
   1.156 +		fprintf(stderr, "creating event_base failed. Exiting.\n");
   1.157 +		return 1;
   1.158 +	}
   1.159 +
   1.160 +	http = evhttp_new(base);
   1.161 +
   1.162 +	content = malloc(content_len);
   1.163 +	if (content == NULL) {
   1.164 +		fprintf(stderr, "Cannot allocate content\n");
   1.165 +		exit(1);
   1.166 +	} else {
   1.167 +		int i = 0;
   1.168 +		for (i = 0; i < (int)content_len; ++i)
   1.169 +			content[i] = (i & 255);
   1.170 +	}
   1.171 +
   1.172 +	evhttp_set_cb(http, "/ind", http_basic_cb, NULL);
   1.173 +	fprintf(stderr, "/ind - basic content (memory copy)\n");
   1.174 +
   1.175 +#ifdef _EVENT2_EVENT_H_
   1.176 +	evhttp_set_cb(http, "/ref", http_ref_cb, NULL);
   1.177 +	fprintf(stderr, "/ref - basic content (reference)\n");
   1.178 +#endif
   1.179 +
   1.180 +	fprintf(stderr, "Serving %d bytes on port %d using %s\n",
   1.181 +	    (int)content_len, port,
   1.182 +	    use_iocp? "IOCP" : event_base_get_method(base));
   1.183 +
   1.184 +	evhttp_bind_socket(http, "0.0.0.0", port);
   1.185 +
   1.186 +	if (use_iocp) {
   1.187 +		struct timeval tv={99999999,0};
   1.188 +		event_base_loopexit(base, &tv);
   1.189 +	}
   1.190 +	event_base_dispatch(base);
   1.191 +
   1.192 +	/* NOTREACHED */
   1.193 +	return (0);
   1.194 +}

mercurial