bunkum

an old and silly c99 web server with some fun features
Log | Files | Refs | README

commit f34b70f63be68595fc94039a698a51c952d8b172
parent 0221ed0275eae5a6fbcfb33d82b67bd6994e450f
Author: quantumish <freifeld.david@gmail.com>
Date:   Tue, 16 May 2023 15:36:38 -0700

Set up benchmarking

Diffstat:
MMakefile | 4++++
Abench.cpp | 38++++++++++++++++++++++++++++++++++++++
Mhttp.c | 6++----
Mhttp/request.h | 18++++++++++++++++++
4 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile @@ -19,5 +19,9 @@ test: $(CLIBS) test.c $(CC) test.c -o test -ldl ./test libbunkum.so +static: + $(CC) $(CLIBS) testutils.c -shared -fPIC $(CFLAGS) -o bunkum.o $(LIBS) -DTEST + ar rvs bunkum.a bunkum.o + clean: rm serv diff --git a/bench.cpp b/bench.cpp @@ -0,0 +1,38 @@ +#include <benchmark/benchmark.h> + +extern "C" { + #include "http/request.h" + #include <string.h> + + const char* method_names[] = {"GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE"}; + + enum http_method method_naive_enum(char* p) { + for (int i = 0; i < 8; i++) { + if (strcmp(method_names[i], p) == 0) { + return (enum http_method) i; + } + } + } +} + +static void BM_PHF(benchmark::State& state) { + // Perform setup here + for (auto _ : state) { + // This code gets timed + method_enum((char*)"OPTIONS"); + } +} + +static void BM_Naive(benchmark::State& state) { + // Perform setup here + for (auto _ : state) { + // This code gets timed + method_naive_enum((char*)"OPTIONS"); + } +} + +// Register the function as a benchmark +BENCHMARK(BM_PHF); +BENCHMARK(BM_Naive); +// Run the benchmark +BENCHMARK_MAIN(); diff --git a/http.c b/http.c @@ -116,11 +116,9 @@ response_t serve_file(request_t* req) { } /* if (!ok) return serve_error(NotAcceptable); */ -#ifndef __APPLE__ char datebuf[64]; time_to_str(st.st_mtim.tv_sec, datebuf); resp_add_hdr(&r, "Last-Modified", datebuf); -#endif resp_add_content(&r, buf, bufsize); free(buf); @@ -136,8 +134,7 @@ response_t make_response (request_t* req, int pfd) { return serve_error(BadRequest); } - if (hashmap_get(&req->headers, "Profile") != 0x0) { - log_debug("Got header"); + if (hashmap_get(&req->headers, "Profile") != 0x0) { int profile = true; if (ptrace(PTRACE_TRACEME, NULL) < 0) { log_error("PTRACE_TRACME failed with err %d", errno); @@ -166,6 +163,7 @@ response_t make_response (request_t* req, int pfd) { } } + double diff_timespec(const struct timespec *time1, const struct timespec *time0) { return (time1->tv_sec - time0->tv_sec) + (time1->tv_nsec - time0->tv_nsec) / 1000000000.0; diff --git a/http/request.h b/http/request.h @@ -15,11 +15,29 @@ enum http_method { TRACE, }; const char* method_name(enum http_method m); +enum http_method method_enum(char* p); #define MAX_PATH_LEN 128 #define MAX_HEADER_NAME 32 #define MAX_HEADER_VALUE 512 #define MAX_MIMETYPE_LEN 32 +#define MAX_MIME_SUBTYPE_LEN 32 + +enum mime_disctype { + MIME_APPLICATION, + MIME_AUDIO, + MIME_EXAMPLE, + MIME_FONT, + MIME_IMAGE, + MIME_MODEL, + MIME_TEXT, + MIME_VIDEO, +}; + +struct mime_type { + enum mime_disctype type; + char subtype[MAX_MIME_SUBTYPE_LEN]; +}; struct req_mimetype { float q;