Esempio n. 1
0
int
main(int argc, char **argv)
{
	int i,j;
	double ratio;

#ifdef _WIN32
	WORD wVersionRequested = MAKEWORD(2,2);
	WSADATA wsaData;
	int err;

	err = WSAStartup(wVersionRequested, &wsaData);
#endif

#ifndef _WIN32
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
		return 1;
#endif
	for (i = 1; i < argc; ++i) {
		for (j = 0; options[j].name; ++j) {
			if (!strcmp(argv[i],options[j].name)) {
				if (handle_option(argc,argv,&i,&options[j])<0)
					return 1;
				goto again;
			}
		}
		fprintf(stderr, "Unknown option '%s'\n", argv[i]);
		usage();
		return 1;
	again:
		;
	}
	if (cfg_help) {
		usage();
		return 0;
	}

	cfg_tick.tv_sec = cfg_tick_msec / 1000;
	cfg_tick.tv_usec = (cfg_tick_msec % 1000)*1000;

	seconds_per_tick = ratio = cfg_tick_msec / 1000.0;

	cfg_connlimit *= ratio;
	cfg_grouplimit *= ratio;

	{
		struct timeval tv;
		evutil_gettimeofday(&tv, NULL);
#ifdef _WIN32
		srand(tv.tv_usec);
#else
		srandom(tv.tv_usec);
#endif
	}

#ifndef EVENT__DISABLE_THREAD_SUPPORT
	evthread_enable_lock_debugging();
#endif

	return test_ratelimiting();
}
Esempio n. 2
0
static void
thread_conditions_simple(void *arg)
{
	struct timeval tv_signal, tv_timeout, tv_broadcast;
	struct alerted_record alerted[NUM_THREADS];
	THREAD_T threads[NUM_THREADS];
	struct cond_wait cond;
	int i;
	struct timeval launched_at;
	struct event wake_one;
	struct event wake_all;
	struct basic_test_data *data = arg;
	struct event_base *base = data->base;
	int n_timed_out=0, n_signal=0, n_broadcast=0;

	tv_signal.tv_sec = tv_timeout.tv_sec = tv_broadcast.tv_sec = 0;
	tv_signal.tv_usec = 30*1000;
	tv_timeout.tv_usec = 150*1000;
	tv_broadcast.tv_usec = 500*1000;

	EVTHREAD_ALLOC_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
	EVTHREAD_ALLOC_COND(cond.cond);
	tt_assert(cond.lock);
	tt_assert(cond.cond);
	for (i = 0; i < NUM_THREADS; ++i) {
		memset(&alerted[i], 0, sizeof(struct alerted_record));
		alerted[i].cond = &cond;
	}

	/* Threads 5 and 6 will be allowed to time out */
	memcpy(&alerted[5].delay, &tv_timeout, sizeof(tv_timeout));
	memcpy(&alerted[6].delay, &tv_timeout, sizeof(tv_timeout));

	evtimer_assign(&wake_one, base, wake_one_timeout, &cond);
	evtimer_assign(&wake_all, base, wake_all_timeout, &cond);

	evutil_gettimeofday(&launched_at, NULL);

	/* Launch the threads... */
	for (i = 0; i < NUM_THREADS; ++i) {
		THREAD_START(threads[i], wait_for_condition, &alerted[i]);
	}

	/* Start the timers... */
	tt_int_op(event_add(&wake_one, &tv_signal), ==, 0);
	tt_int_op(event_add(&wake_all, &tv_broadcast), ==, 0);

	/* And run for a bit... */
	event_base_dispatch(base);

	/* And wait till the threads are done. */
	for (i = 0; i < NUM_THREADS; ++i)
		THREAD_JOIN(threads[i]);

	/* Now, let's see what happened. At least one of 5 or 6 should
	 * have timed out. */
	n_timed_out = alerted[5].timed_out + alerted[6].timed_out;
	tt_int_op(n_timed_out, >=, 1);
	tt_int_op(n_timed_out, <=, 2);

	for (i = 0; i < NUM_THREADS; ++i) {
		const struct timeval *target_delay;
		struct timeval target_time, actual_delay;
		if (alerted[i].timed_out) {
			TT_BLATHER(("%d looks like a timeout\n", i));
			target_delay = &tv_timeout;
			tt_assert(i == 5 || i == 6);
		} else if (evutil_timerisset(&alerted[i].alerted_at)) {
			long diff1,diff2;
			evutil_timersub(&alerted[i].alerted_at,
			    &launched_at, &actual_delay);
			diff1 = timeval_msec_diff(&actual_delay,
			    &tv_signal);
			diff2 = timeval_msec_diff(&actual_delay,
			    &tv_broadcast);
			if (abs(diff1) < abs(diff2)) {
				TT_BLATHER(("%d looks like a signal\n", i));
				target_delay = &tv_signal;
				++n_signal;
			} else {
				TT_BLATHER(("%d looks like a broadcast\n", i));
				target_delay = &tv_broadcast;
				++n_broadcast;
			}
		} else {
			TT_FAIL(("Thread %d never got woken", i));
			continue;
		}
		evutil_timeradd(target_delay, &launched_at, &target_time);
		test_timeval_diff_leq(&target_time, &alerted[i].alerted_at,
		    0, 50);
	}
	tt_int_op(n_broadcast + n_signal + n_timed_out, ==, NUM_THREADS);
	tt_int_op(n_signal, ==, 1);

end:
	;
}
Esempio n. 3
0
/**
 * This function must be optimize later.
 */
int		NetLoop::getSystemTime() {
	timeval tv;
	evutil_gettimeofday(&tv, NULL);

	return tv.tv_sec;
}
Esempio n. 4
0
static void
timer_callback(evutil_socket_t fd, short what, void *arg)
{
	evutil_gettimeofday(&timer_end, NULL);
}
Esempio n. 5
0
int
main(int argc, char **argv)
{
	int i;
	struct timeval start, end, total;
	long long usec;
	double throughput;

#ifdef _WIN32
	WSADATA WSAData;
	WSAStartup(0x101, &WSAData);
#endif

	resource = "/ref";

	setvbuf(stdout, NULL, _IONBF, 0);

	base = event_base_new();

	for (i=0; i < PARALLELISM; ++i) {
		if (launch_request() < 0)
			perror("launch");
	}

	evutil_gettimeofday(&start, NULL);

	event_base_dispatch(base);

	evutil_gettimeofday(&end, NULL);
	evutil_timersub(&end, &start, &total);
	usec = total_time.tv_sec * (long long)1000000 + total_time.tv_usec;

	if (!total_n_handled) {
		puts("Nothing worked.  You probably did something dumb.");
		return 0;
	}


	throughput = total_n_handled /
	    (total.tv_sec+ ((double)total.tv_usec)/1000000.0);

#ifdef _WIN32
#define I64_FMT "%I64d"
#define I64_TYP __int64
#else
#define I64_FMT "%lld"
#define I64_TYP long long int
#endif

	printf("\n%d requests in %d.%06d sec. (%.2f throughput)\n"
	    "Each took about %.02f msec latency\n"
	    I64_FMT "bytes read. %d errors.\n",
	    total_n_handled,
	    (int)total.tv_sec, (int)total.tv_usec,
	    throughput,
	    (double)(usec/1000) / total_n_handled,
	    (I64_TYP)total_n_bytes, n_errors);

#ifdef _WIN32
	WSACleanup();
#endif

	return 0;
}
Esempio n. 6
0
static void
http_chunked_test(void)
{
	struct bufferevent *bev;
	int fd;
	const char *http_request;
	short port = -1;
	struct timeval tv_start, tv_end;
	struct evhttp_connection *evcon = NULL;
	struct evhttp_request *req = NULL;
	int i;

	test_ok = 0;
	fprintf(stdout, "Testing Chunked HTTP Reply: ");

	http = http_setup(&port, NULL);

	fd = http_connect("127.0.0.1", port);

	/* Stupid thing to send a request */
	bev = bufferevent_new(fd, 
	    http_chunked_readcb, http_chunked_writecb,
	    http_chunked_errorcb, NULL);

	http_request =
	    "GET /chunked HTTP/1.1\r\n"
	    "Host: somehost\r\n"
	    "Connection: close\r\n"
	    "\r\n";

	bufferevent_write(bev, http_request, strlen(http_request));

	evutil_gettimeofday(&tv_start, NULL);
	
	event_dispatch();

	evutil_gettimeofday(&tv_end, NULL);
	evutil_timersub(&tv_end, &tv_start, &tv_end);

	if (tv_end.tv_sec >= 1) {
		fprintf(stdout, "FAILED (time)\n");
		exit (1);
	}


	if (test_ok != 2) {
		fprintf(stdout, "FAILED\n");
		exit(1);
	}

	/* now try again with the regular connection object */
	evcon = evhttp_connection_new("127.0.0.1", port);
	if (evcon == NULL) {
		fprintf(stdout, "FAILED\n");
		exit(1);
	}

	/* make two requests to check the keepalive behavior */
	for (i = 0; i < 2; i++) {
		test_ok = 0;
		req = evhttp_request_new(http_chunked_request_done, NULL);

		/* Add the information that we care about */
		evhttp_add_header(req->output_headers, "Host", "somehost");

		/* We give ownership of the request to the connection */
		if (evhttp_make_request(evcon, req,
			EVHTTP_REQ_GET, "/chunked") == -1) {
			fprintf(stdout, "FAILED\n");
			exit(1);
		}

		event_dispatch();

		if (test_ok != 1) {
			fprintf(stdout, "FAILED\n");
			exit(1);
		}
	}

	evhttp_connection_free(evcon);
	evhttp_free(http);
	
	fprintf(stdout, "OK\n");
}
Esempio n. 7
0
/* Testing code for event_base_dump_events().

   Notes that just because we have code to exercise this function,
   doesn't mean that *ANYTHING* about the output format is guaranteed to
   remain in the future.
 */
int
main(int argc, char **argv)
{
#define N_EVENTS 13
	int i;
	struct event *ev[N_EVENTS];
	evutil_socket_t pair1[2];
	evutil_socket_t pair2[2];
	struct timeval tv_onesec = {1,0};
	struct timeval tv_two5sec = {2,500*1000};
	const struct timeval *tv_onesec_common;
	const struct timeval *tv_two5sec_common;
	struct event_base *base;
	struct timeval now;

#ifdef _WIN32
	WORD wVersionRequested;
	WSADATA wsaData;

	wVersionRequested = MAKEWORD(2, 2);

	WSAStartup(wVersionRequested, &wsaData);
#endif

#ifdef _WIN32
#define LOCAL_SOCKETPAIR_AF AF_INET
#else
#define LOCAL_SOCKETPAIR_AF AF_UNIX
#endif

	if (evutil_make_internal_pipe_(pair1) < 0 ||
	    evutil_make_internal_pipe_(pair2) < 0) {
		sock_perror("evutil_make_internal_pipe_");
		return 1;
	}

	if (!(base = event_base_new())) {
		fprintf(stderr,"Couldn't make event_base\n");
		return 2;
	}

	tv_onesec_common = event_base_init_common_timeout(base, &tv_onesec);
	tv_two5sec_common = event_base_init_common_timeout(base, &tv_two5sec);

	ev[0] = event_new(base, pair1[0], EV_WRITE, callback1, NULL);
	ev[1] = event_new(base, pair1[1], EV_READ|EV_PERSIST, callback1, NULL);
	ev[2] = event_new(base, pair2[0], EV_WRITE|EV_PERSIST, callback2, NULL);
	ev[3] = event_new(base, pair2[1], EV_READ, callback2, NULL);

	/* For timers */
	ev[4] = evtimer_new(base, callback1, NULL);
	ev[5] = evtimer_new(base, callback1, NULL);
	ev[6] = evtimer_new(base, callback1, NULL);
	ev[7] = event_new(base, -1, EV_PERSIST, callback2, NULL);
	ev[8] = event_new(base, -1, EV_PERSIST, callback2, NULL);
	ev[9] = event_new(base, -1, EV_PERSIST, callback2, NULL);

	/* To activate */
	ev[10] = event_new(base, -1, 0, callback1, NULL);
	ev[11] = event_new(base, -1, 0, callback2, NULL);

	/* Signals */
	ev[12] = evsignal_new(base, SIGINT, callback2, NULL);

	event_add(ev[0], NULL);
	event_add(ev[1], &tv_onesec);
	event_add(ev[2], tv_onesec_common);
	event_add(ev[3], tv_two5sec_common);

	event_add(ev[4], tv_onesec_common);
	event_add(ev[5], tv_onesec_common);
	event_add(ev[6], &tv_onesec);
	event_add(ev[7], tv_two5sec_common);
	event_add(ev[8], tv_onesec_common);
	event_add(ev[9], &tv_two5sec);

	event_active(ev[10], EV_READ, 1);
	event_active(ev[11], EV_READ|EV_WRITE|EV_TIMEOUT, 1);
	event_active(ev[1], EV_READ, 1);

	event_add(ev[12], NULL);

	evutil_gettimeofday(&now,NULL);
	puts("=====expected");
	printf("Now= %ld.%06d\n",(long)now.tv_sec,(int)now.tv_usec);
	puts("Inserted:");
	printf("  %p [fd  %ld] Write\n",ev[0],(long)pair1[0]);
	printf("  %p [fd  %ld] Read Persist Timeout=T+1\n",ev[1],(long)pair1[1]);
	printf("  %p [fd  %ld] Write Persist Timeout=T+1\n",ev[2],(long)pair2[0]);
	printf("  %p [fd  %ld] Read Timeout=T+2.5\n",ev[3],(long)pair2[1]);
	printf("  %p [fd  -1] Timeout=T+1\n",ev[4]);
	printf("  %p [fd  -1] Timeout=T+1\n",ev[5]);
	printf("  %p [fd  -1] Timeout=T+1\n",ev[6]);
	printf("  %p [fd  -1] Persist Timeout=T+2.5\n",ev[7]);
	printf("  %p [fd  -1] Persist Timeout=T+1\n",ev[8]);
	printf("  %p [fd  -1] Persist Timeout=T+2.5\n",ev[9]);
	printf("  %p [sig %d] Signal Persist\n", ev[12], (int)SIGINT);

	puts("Active:");
	printf("  %p [fd  -1, priority=0] Read active\n", ev[10]);
	printf("  %p [fd  -1, priority=0] Read Write Timeout active\n", ev[11]);
	printf("  %p [fd  %ld, priority=0] Read active\n", ev[1], (long)pair1[1]);

	puts("======received");
	event_base_dump_events(base, stdout);

	for (i = 0; i < N_EVENTS; ++i) {
		event_free(ev[i]);
	}
	event_base_free(base);

	return 0;
}