Add Tests.h with benchmarks
This commit is contained in:
@@ -18,7 +18,8 @@ HEADERS += \
|
|||||||
src/Loop.h \
|
src/Loop.h \
|
||||||
src/App.h \
|
src/App.h \
|
||||||
src/HttpSocket.h \
|
src/HttpSocket.h \
|
||||||
src/HttpParser.h
|
src/HttpParser.h \
|
||||||
|
src/Tests.h
|
||||||
|
|
||||||
INCLUDEPATH += uSockets/src src
|
INCLUDEPATH += uSockets/src src
|
||||||
#QMAKE_CXXFLAGS += -fsanitize=address
|
#QMAKE_CXXFLAGS += -fsanitize=address
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "uWS.h"
|
#include "uWS.h"
|
||||||
|
|
||||||
|
#include "Tests.h"
|
||||||
|
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
|
|
||||||
//#define USE_SSL
|
//#define USE_SSL
|
||||||
@@ -16,6 +18,9 @@ int main(int argc, char **argv) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// first of all we run a couple of benchmarks
|
||||||
|
testHttpParserPerformance();
|
||||||
|
|
||||||
//uWS::init();
|
//uWS::init();
|
||||||
//uWS::Loop loop();
|
//uWS::Loop loop();
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -117,7 +117,7 @@ public:
|
|||||||
|
|
||||||
// think about better interface for this one. HttpParser::consumePostPadded<limit or not>(data, length, httpData, onHttpRequest)
|
// think about better interface for this one. HttpParser::consumePostPadded<limit or not>(data, length, httpData, onHttpRequest)
|
||||||
template <int LIMIT_TO_ONE_REQUEST>
|
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 ret = 0;
|
||||||
|
|
||||||
int consumed = 0;
|
int consumed = 0;
|
||||||
@@ -132,7 +132,7 @@ public:
|
|||||||
if (std::string_view contentLengthString = req->getHeader("content-length"); contentLengthString.length()) {
|
if (std::string_view contentLengthString = req->getHeader("content-length"); contentLengthString.length()) {
|
||||||
remainingStreamingBytes = str2int(contentLengthString.data(), contentLengthString.length());
|
remainingStreamingBytes = str2int(contentLengthString.data(), contentLengthString.length());
|
||||||
int emittable = std::min(remainingStreamingBytes, 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;
|
remainingStreamingBytes -= emittable;
|
||||||
length -= 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;
|
HttpRequest req;
|
||||||
|
|
||||||
|
|||||||
+36
@@ -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
|
||||||
Reference in New Issue
Block a user