From c69e4fb11a308744c9c0465975dcbc7561f7151b Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Wed, 8 Mar 2017 00:28:26 +0300 Subject: HTTP parser benchmark. --- test/nxt_http_parse_unit_test.c | 179 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 176 insertions(+), 3 deletions(-) (limited to 'test/nxt_http_parse_unit_test.c') diff --git a/test/nxt_http_parse_unit_test.c b/test/nxt_http_parse_unit_test.c index abe7093d..d9e03e47 100644 --- a/test/nxt_http_parse_unit_test.c +++ b/test/nxt_http_parse_unit_test.c @@ -44,13 +44,17 @@ typedef struct { static nxt_int_t nxt_http_parse_unit_test_run(nxt_http_request_parse_t *rp, nxt_str_t *request); - +static nxt_int_t nxt_http_parse_unit_test_bench(nxt_thread_t *thr, + nxt_str_t *request, nxt_http_fields_hash_t *hash, const char *name, + nxt_uint_t n); static nxt_int_t nxt_http_parse_unit_test_request_line( nxt_http_request_parse_t *rp, nxt_http_parse_unit_test_data_t *data, nxt_str_t *request, nxt_log_t *log); static nxt_int_t nxt_http_unit_test_header_return(void *ctx, nxt_str_t *name, nxt_str_t *value, uintptr_t data); +static nxt_int_t nxt_http_unit_test_header_dump(void *ctx, nxt_str_t *name, + nxt_str_t *value, uintptr_t data); static nxt_http_parse_unit_test_case_t nxt_http_unit_test_cases[] = { @@ -269,6 +273,103 @@ static nxt_http_fields_t nxt_http_unit_test_headers[] = { }; +static nxt_http_fields_t nxt_http_unit_test_bench_headers[] = { + { nxt_string("Host"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("User-Agent"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("Accept-Encoding"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("Accept-Language"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("Connection"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("Content-Length"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("Content-Type"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("If-Modified-Since"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("If-Match"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("Date"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("Upgrade"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("X-Forwarded-For"), &nxt_http_unit_test_header_dump, 0 }, + { nxt_string("X-Request-ID"), &nxt_http_unit_test_header_dump, 0 }, + + { nxt_null_string, NULL, 0 } +}; + + +static nxt_str_t nxt_http_unit_test_simple_request = nxt_string( + "GET /page HTTP/1.1\r\n" + "Host: example.com\r\n\r\n" +); + + +static nxt_str_t nxt_http_unit_test_big_request = nxt_string( + "POST /path/to/very/interesting/article/on.this.site?arg1=value&arg2=value" + "2&very_big_arg=even_bigger_value HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "User-Agent: Mozilla/5.0 (X11; Gentoo Linux x86_64; rv:42.0) Firefox/42.0" + "\r\n" + "Accept: text/html,application/json,application/xml;q=0.9,*/*;q=0.8\r\n" + "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n" + "Accept-Encoding: gzip, deflate, br\r\n" + "If-Modified-Since: Wed, 31 Dec 1986 16:00:00 GMT\r\n" + "Referer: https://example.org/path/to/not-interesting/article.html\r\n" + "Cookie: name=value; name2=value2; some_big_cookie=iVBORw0KGgoAAAANSUhEUgA" + "AAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAABmelRY" + "dFJhdyBwcm9maWxlIHR5cGUgZXhpZgAAeNptitsJgEAMBP9ThSWsZy6PcvKhcB1YvjEni" + "ODAwjAs7ec4aCmkEXc1cREk7OwtUgyTFRA3BU+vFPjS7gUI/p46Q0u2fP/1B7oA1Scbwk" + "nkf9gAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACfUExURQwMDDw8PFBQUAICAhQUFAcHBxs" + "bGxEREQkJCTk5OTU1NSAgIFRUVB8fH0xMTCUlJVtb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AcHH1p8sEFAHAAAAAElFTkSuQmCC\r\n" + "Connection: keep-alive\r\n" + "Content-Length: 0\r\n" + "Upgrade-Insecure-Requests: 1\r\n" + "Pragma: no-cache\r\n" + "Cache-Control: no-cache\r\n" + "X-Forwarded-For: 192.0.2.0, 198.51.100.0, 203.0.113.0\r\n" + "\r\n" +); + + nxt_int_t nxt_http_parse_unit_test(nxt_thread_t *thr) { @@ -317,10 +418,30 @@ nxt_http_parse_unit_test(nxt_thread_t *thr) } } - nxt_mem_pool_destroy(pool); - nxt_log_error(NXT_LOG_NOTICE, thr->log, "http parse unit test passed"); + hash = nxt_http_fields_hash(nxt_http_unit_test_bench_headers, pool); + + if (hash == NULL) { + return NXT_ERROR; + } + + if (nxt_http_parse_unit_test_bench(thr, &nxt_http_unit_test_simple_request, + hash, "simple", 10000000) + != NXT_OK) + { + return NXT_ERROR; + } + + if (nxt_http_parse_unit_test_bench(thr, &nxt_http_unit_test_big_request, + hash, "big", 100000) + != NXT_OK) + { + return NXT_ERROR; + } + + nxt_mem_pool_destroy(pool); + return NXT_OK; } @@ -346,6 +467,50 @@ nxt_http_parse_unit_test_run(nxt_http_request_parse_t *rp, nxt_str_t *request) } +static nxt_int_t +nxt_http_parse_unit_test_bench(nxt_thread_t *thr, nxt_str_t *request, + nxt_http_fields_hash_t *hash, const char *name, nxt_uint_t n) +{ + nxt_nsec_t start, end; + nxt_uint_t i; + nxt_buf_mem_t buf; + nxt_http_request_parse_t rp; + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "http parse unit %s request bench started: " + "%uz bytes, %ui runs", + name, request->length, n); + + buf.start = request->start; + buf.end = request->start + request->length; + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + for (i = 0; nxt_fast_path(i < n); i++) { + nxt_memzero(&rp, sizeof(nxt_http_request_parse_t)); + + buf.pos = buf.start; + buf.free = buf.end; + + if (nxt_slow_path(nxt_http_parse_request(&rp, &buf) != NXT_DONE)) { + nxt_log_alert(thr->log, "http parse unit %s request bench failed", + name); + return NXT_ERROR; + } + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "http parse unit %s request bench: %0.3fs", + name, (end - start) / 1000000000.0); + + return NXT_OK; +} + + static nxt_int_t nxt_http_parse_unit_test_request_line(nxt_http_request_parse_t *rp, nxt_http_parse_unit_test_data_t *data, nxt_str_t *request, nxt_log_t *log) @@ -455,3 +620,11 @@ nxt_http_unit_test_header_return(void *ctx, nxt_str_t *name, nxt_str_t *value, { return (nxt_int_t) data; } + + +static nxt_int_t +nxt_http_unit_test_header_dump(void *ctx, nxt_str_t *name, nxt_str_t *value, + uintptr_t data) +{ + return NXT_OK; +} -- cgit