Skip to content

Instantly share code, notes, and snippets.

@heynemann
Created June 21, 2011 03:35
Show Gist options
  • Save heynemann/1037192 to your computer and use it in GitHub Desktop.
Save heynemann/1037192 to your computer and use it in GitHub Desktop.
libevent hello world server
#include <event.h>
#include <evhttp.h>
#include <cstdio>
#include <iostream>
#define SRVR_SIGNATURE "dirq v 0.0.1"
void router(struct evhttp_request *r, void *arg) {
// just testing retrieval of URI
const char *uri = evhttp_request_uri(r);
printf("%s\n", uri);
struct evbuffer *buffer;
buffer = evbuffer_new();
// commented til I find the proper way to do this
//struct evkeyvalq *headers = evhttp_request_get_output_headers(r);
//evhttp_add_header(headers, "Content-Type", "text/html; charset=UTF-8");
//evhttp_add_header(headers, "Server", SRVR_SIGNATURE);
evbuffer_add_printf(buffer, "HELLO WORLD");
evhttp_send_reply(r, HTTP_OK, "OK", buffer);
evbuffer_free(buffer);
}
int main(int argc, char **argv) {
struct evhttp *libsrvr;
struct event_base *libbase;
libbase = event_base_new();
libsrvr = evhttp_new(libbase);
evhttp_bind_socket(libsrvr, "0.0.0.0", 8000);
evhttp_set_gencb(libsrvr, router, NULL);
event_base_dispatch(libbase);
return 0;
}
@heynemann
Copy link
Author

To compile - g++ -Wall -levent libevent.cpp -o libevent-test
To run: ./libevent-test

@heynemann
Copy link
Author

Benchmark:

ab -n 1000000 -c 50 http://127.0.0.1:8000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests

Server Software:
Server Hostname: 127.0.0.1
Server Port: 8000

Document Path: /
Document Length: 11 bytes

Concurrency Level: 50
Time taken for tests: 56.808 seconds
Complete requests: 1000000
Failed requests: 0
Write errors: 0
Total transferred: 75000000 bytes
HTML transferred: 11000000 bytes
Requests per second: 17603.19 #/sec
Time per request: 2.840 ms
Time per request: 0.057 [ms](mean, across all concurrent requests)
Transfer rate: 1289.30 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.2 1 3
Processing: 0 2 0.3 2 11
Waiting: 0 1 0.3 1 10
Total: 1 3 0.3 3 12

Percentage of the requests served within a certain time (ms)
50% 3
66% 3
75% 3
80% 3
90% 3
95% 3
98% 3
99% 3
100% 12 (longest request)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment