From cbe3d3375d277e4f2b0060e6eb8a9f7aaad1bea9 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sat, 30 Jun 2018 00:42:03 +0200 Subject: [PATCH] Add Tests.h with benchmarks --- 15.pro | 3 ++- main.cpp | 5 +++++ src/HttpParser.h | 6 +++--- src/Tests.h | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/Tests.h diff --git a/15.pro b/15.pro index 16c568c..750945a 100644 --- a/15.pro +++ b/15.pro @@ -18,7 +18,8 @@ HEADERS += \ src/Loop.h \ src/App.h \ src/HttpSocket.h \ - src/HttpParser.h + src/HttpParser.h \ + src/Tests.h INCLUDEPATH += uSockets/src src #QMAKE_CXXFLAGS += -fsanitize=address diff --git a/main.cpp b/main.cpp index 33fe71c..7ec3a66 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,7 @@ #include "uWS.h" +#include "Tests.h" + std::string buffer; //#define USE_SSL @@ -16,6 +18,9 @@ int main(int argc, char **argv) { return -1; } + // first of all we run a couple of benchmarks + testHttpParserPerformance(); + //uWS::init(); //uWS::Loop loop(); diff --git a/src/HttpParser.h b/src/HttpParser.h index b0dd3de..16ca6f2 100644 --- a/src/HttpParser.h +++ b/src/HttpParser.h @@ -117,7 +117,7 @@ public: // think about better interface for this one. HttpParser::consumePostPadded(data, length, httpData, onHttpRequest) template - int fenceAndConsumePostPadded(char *data, int length, void *user, HttpRequest *req, std::function &requestHandler, std::function &dataHandler) { + inline int fenceAndConsumePostPadded(char *data, int length, void *user, HttpRequest *req, std::function &requestHandler, std::function &dataHandler) { int ret = 0; int consumed = 0; @@ -132,7 +132,7 @@ public: if (std::string_view contentLengthString = req->getHeader("content-length"); contentLengthString.length()) { remainingStreamingBytes = str2int(contentLengthString.data(), contentLengthString.length()); int emittable = std::min(remainingStreamingBytes, length); - /*httpData->inStream*/dataHandler(user, std::string_view(data, emittable)); + dataHandler(user, std::string_view(data, emittable)); remainingStreamingBytes -= emittable; length -= emittable; @@ -147,7 +147,7 @@ public: } - void consumePostPadded(char *data, int length, void *user, std::function requestHandler, std::function dataHandler, std::function errorHandler) { + inline void consumePostPadded(char *data, int length, void *user, std::function &&requestHandler, std::function &&dataHandler, std::function &&errorHandler) { HttpRequest req; diff --git a/src/Tests.h b/src/Tests.h new file mode 100644 index 0000000..16ebfe0 --- /dev/null +++ b/src/Tests.h @@ -0,0 +1,36 @@ +#ifndef TESTS_H +#define TESTS_H + +#include "HttpParser.h" + +#include + +void testHttpParserPerformance() { + + char data[] = "GET /hello.htm HTTP/1.1\r\n" + "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\n" + "Host: www.tutorialspoint.com\r\n" + "Accept-Language: en-us\r\n" + "Accept-Encoding: gzip, deflate\r\n" + "Connection: Keep-Alive\r\n\r\n "; + + HttpParser httpParser; + + int validRequests = 0; + + auto start = std::chrono::high_resolution_clock::now(); + for (int i = 0; i < 10000000; i++) { + httpParser.consumePostPadded(data, sizeof(data) - 2, nullptr, [&validRequests](void *user, HttpRequest *req) { + validRequests++; + }, [](void *, std::string_view data) { + + }, [](void *) { + + }); + } + auto stop = std::chrono::high_resolution_clock::now(); + + std::cout << "Parsed " << validRequests << " in " << std::chrono::duration_cast(stop - start).count() << "ms" << std::endl; +} + +#endif // TESTS_H