Make tests

This commit is contained in:
Alex Hultman
2018-07-02 21:56:21 +02:00
parent aac0eee30a
commit df2a48f042
6 changed files with 15 additions and 103 deletions
+1 -2
View File
@@ -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
+7 -1
View File
@@ -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
-5
View File
@@ -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();
+4
View File
@@ -88,6 +88,10 @@ protected:
// onHttpRequest should probably be hard-coded to HttpRouter
((HttpSocket<SSL> *) s)->onData(data, length, appData->onHttpRequest);
// compared to routing directly
//typename Data::UserData user = {(HttpSocket<SSL> *) 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) {
+3 -1
View File
@@ -168,7 +168,9 @@ struct HttpSocket {
httpData->httpParser.consumePostPadded(data, length, this, [&onHttpRequest](void *user, HttpRequest *httpRequest) {
onHttpRequest((HttpSocket<SSL> *) 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;
});
-94
View File
@@ -1,94 +0,0 @@
#ifndef TESTS_H
#define TESTS_H
#include "HttpParser.h"
#include <chrono>
// 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<std::chrono::milliseconds>(stop - start).count() << "ms" << std::endl;
}
#endif // TESTS_H