Add Tests.h with benchmarks

This commit is contained in:
Alex Hultman
2018-06-30 00:42:03 +02:00
parent 999ae69bed
commit cbe3d3375d
4 changed files with 46 additions and 4 deletions
+2 -1
View File
@@ -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
+5
View File
@@ -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();
+3 -3
View File
@@ -117,7 +117,7 @@ public:
// think about better interface for this one. HttpParser::consumePostPadded<limit or not>(data, length, httpData, onHttpRequest)
template <int LIMIT_TO_ONE_REQUEST>
int fenceAndConsumePostPadded(char *data, int length, void *user, HttpRequest *req, std::function<void(void *, HttpRequest *)> &requestHandler, std::function<void(void *, std::string_view)> &dataHandler) {
inline int fenceAndConsumePostPadded(char *data, int length, void *user, HttpRequest *req, std::function<void(void *, HttpRequest *)> &requestHandler, std::function<void(void *, std::string_view)> &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<void(void *, HttpRequest *)> requestHandler, std::function<void(void *, std::string_view)> dataHandler, std::function<void(void *)> errorHandler) {
inline void consumePostPadded(char *data, int length, void *user, std::function<void(void *, HttpRequest *)> &&requestHandler, std::function<void(void *, std::string_view)> &&dataHandler, std::function<void(void *)> &&errorHandler) {
HttpRequest req;
+36
View File
@@ -0,0 +1,36 @@
#ifndef TESTS_H
#define TESTS_H
#include "HttpParser.h"
#include <chrono>
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<std::chrono::milliseconds>(stop - start).count() << "ms" << std::endl;
}
#endif // TESTS_H