Last active
May 2, 2025 12:24
-
-
Save philippta/f1621c03bcdd9c8b5c13855e7b2283e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <event2/buffer.h> | |
| #include <event2/event.h> | |
| #include <event2/http.h> | |
| #include <event2/keyvalq_struct.h> | |
| const char *get_query_arg(struct evhttp_request *req, const char *name) { | |
| // Parse uri | |
| const struct evhttp_uri *uri = evhttp_request_get_evhttp_uri(req); | |
| const char *query = evhttp_uri_get_query(uri); | |
| // Parse query | |
| struct evkeyvalq kv; | |
| evhttp_parse_query_str(query, &kv); | |
| // Find query field | |
| return evhttp_find_header(&kv, name); | |
| } | |
| void greet_handler(struct evhttp_request *req, void *arg) { | |
| // Get name from query string | |
| const char *name = get_query_arg(req, "name"); | |
| // Write response | |
| struct evbuffer *buf = evbuffer_new(); | |
| evbuffer_add_printf(buf, "Hello %s!\n", name); | |
| evhttp_send_reply(req, 200, "OK", buf); | |
| // Cleanup | |
| evbuffer_free(buf); | |
| } | |
| int main() { | |
| struct event_base *base; | |
| struct evhttp *http; | |
| // Init event loop and http server | |
| base = event_base_new(); | |
| http = evhttp_new(base); | |
| // Attach request handlers | |
| evhttp_set_cb(http, "/hello", greet_handler, NULL); | |
| // Listen for connections | |
| evhttp_bind_socket_with_handle(http, "0.0.0.0", 8080); | |
| // Run event loop | |
| event_base_dispatch(base); | |
| // Clean up | |
| evhttp_free(http); | |
| event_base_free(base); | |
| return 0; | |
| } | |
| /* | |
| Compile and run | |
| $ clang -levent httpserver.c -o httpserver | |
| $ ./httpserver | |
| Benchmarks on my MacBook Pro M1 | |
| $ hey -c 100 -n 100000 "http://localhost:8080/hello?name=world" | |
| Summary: | |
| Total: 1.2842 secs | |
| Slowest: 0.0138 secs | |
| Fastest: 0.0000 secs | |
| Average: 0.0013 secs | |
| Requests/sec: 77871.0496 | |
| Total data: 1300000 bytes | |
| Size/request: 13 bytes | |
| Response time histogram: | |
| 0.000 [1] | | |
| 0.001 [79859] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | |
| 0.003 [19260] |■■■■■■■■■■ | |
| 0.004 [649] | | |
| 0.006 [31] | | |
| 0.007 [0] | | |
| 0.008 [84] | | |
| 0.010 [20] | | |
| 0.011 [18] | | |
| 0.012 [41] | | |
| 0.014 [37] | | |
| Latency distribution: | |
| 10% in 0.0010 secs | |
| 25% in 0.0011 secs | |
| 50% in 0.0012 secs | |
| 75% in 0.0014 secs | |
| 90% in 0.0016 secs | |
| 95% in 0.0018 secs | |
| 99% in 0.0028 secs | |
| Details (average, fastest, slowest): | |
| DNS+dialup: 0.0000 secs, 0.0000 secs, 0.0138 secs | |
| DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0032 secs | |
| req write: 0.0000 secs, 0.0000 secs, 0.0020 secs | |
| resp wait: 0.0012 secs, 0.0000 secs, 0.0084 secs | |
| resp read: 0.0000 secs, 0.0000 secs, 0.0020 secs | |
| Status code distribution: | |
| [200] 100000 responses | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment