From df2a48f0421ea7bef4256d08c2407984f7d1d329 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 2 Jul 2018 21:56:21 +0200 Subject: [PATCH] Make tests --- 15.pro | 3 +- Makefile | 8 ++++- main.cpp | 5 --- src/App.h | 4 +++ src/HttpSocket.h | 4 ++- src/Tests.h | 94 ------------------------------------------------ 6 files changed, 15 insertions(+), 103 deletions(-) delete mode 100644 src/Tests.h diff --git a/15.pro b/15.pro index 750945a..16c568c 100644 --- a/15.pro +++ b/15.pro @@ -18,8 +18,7 @@ HEADERS += \ src/Loop.h \ src/App.h \ src/HttpSocket.h \ - src/HttpParser.h \ - src/Tests.h + src/HttpParser.h INCLUDEPATH += uSockets/src src #QMAKE_CXXFLAGS += -fsanitize=address diff --git a/Makefile b/Makefile index 8bcf0c1..6bad04a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,10 @@ default: + rm *.o clang -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c clang++ -flto -O3 -c -std=c++17 -Isrc -IuSockets/src main.cpp - clang++ -flto -O3 -s *.o -o uWS_test -lssl -lcrypto + clang++ -flto -O3 -s *.o -o uWS_main -lssl -lcrypto +tests: + rm *.o + clang -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c + clang++ -flto -O3 -c -std=c++17 -Isrc -IuSockets/src tests.cpp + clang++ -flto -O3 -s *.o -o uWS_tests -lssl -lcrypto diff --git a/main.cpp b/main.cpp index 7ec3a66..33fe71c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,5 @@ #include "uWS.h" -#include "Tests.h" - std::string buffer; //#define USE_SSL @@ -18,9 +16,6 @@ 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/App.h b/src/App.h index 09e9165..b9cf0c6 100644 --- a/src/App.h +++ b/src/App.h @@ -88,6 +88,10 @@ protected: // onHttpRequest should probably be hard-coded to HttpRouter ((HttpSocket *) s)->onData(data, length, appData->onHttpRequest); + + // compared to routing directly + //typename Data::UserData user = {(HttpSocket *) s, nullptr}; + //appData->r.route("GET", 3, "/", 1, &user); }); static_dispatch(us_ssl_socket_context_on_writable, us_socket_context_on_writable)(httpServerContext, [](auto *s) { diff --git a/src/HttpSocket.h b/src/HttpSocket.h index b79ccac..06e6b2b 100644 --- a/src/HttpSocket.h +++ b/src/HttpSocket.h @@ -168,7 +168,9 @@ struct HttpSocket { httpData->httpParser.consumePostPadded(data, length, this, [&onHttpRequest](void *user, HttpRequest *httpRequest) { onHttpRequest((HttpSocket *) user, httpRequest); }, [httpData](void *user, std::string_view data) { - httpData->inStream(data); + if (httpData->inStream) { + httpData->inStream(data); + } }, [](void *user) { std::cout << "INVALID HTTP!" << std::endl; }); diff --git a/src/Tests.h b/src/Tests.h deleted file mode 100644 index c125ee2..0000000 --- a/src/Tests.h +++ /dev/null @@ -1,94 +0,0 @@ -#ifndef TESTS_H -#define TESTS_H - -#include "HttpParser.h" - -#include - -// todo: random test of chunked http parsing of randomly generated requests -void testHttpParserPerformance() { - - char headers[] = "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" - "Content-length: 1048576\r\n\r\n"; - - const int requestLength = sizeof(headers) - 1 + 1048576; - char *request = (char *) malloc(requestLength + 32); - memset(request, 0, requestLength); - memcpy(request, headers, sizeof(headers) - 1); - - char *data = (char *) malloc(requestLength * 10); - int length = requestLength * 10; - - int maxChunkSize = 10000; - char *paddedBuffer = (char *) malloc(maxChunkSize + 32); - - // dela upp dessa 10 i 5 segment - HttpParser httpParser; - int validRequests = 0, numDataEmits = 0, numChunks = 0; - size_t dataBytes = 0; - - for (int i = 0; i < 10; i++) { - memcpy(data + requestLength * i, request, requestLength); - } - - for (int j = 0; j < 1000; j++) { - for (int currentOffset = 0; currentOffset != length; ) { - - int chunkSize = rand() % 10000; - if (currentOffset + chunkSize > length) { - chunkSize = length - currentOffset; - } - - memcpy(paddedBuffer, data + currentOffset, chunkSize); - - httpParser.consumePostPadded(paddedBuffer, chunkSize, nullptr, [&validRequests](void *user, HttpRequest *req) { - validRequests++; - - if (req->getUrl() != "/hello.htm") { - std::cout << "WRONG URL!" << std::endl; - exit(-1); - } - - }, [&dataBytes, &numDataEmits](void *, std::string_view data) { - numDataEmits++; - dataBytes += data.length(); - }, [](void *) { - - std::cout << "Error!" << std::endl; - return; - }); - - numChunks++; - - currentOffset += chunkSize; - } - } - - std::cout << "validRequests: " << validRequests << std::endl; - std::cout << "Data bytes: " << dataBytes << std::endl; - std::cout << "Data emits: " << numDataEmits << std::endl; - std::cout << "Chunks parsed: " << numChunks << std::endl; - - validRequests = 0; - - auto start = std::chrono::high_resolution_clock::now(); - for (int i = 0; i < 10000000; i++) { - httpParser.consumePostPadded(request, requestLength, 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