Skip to content

Commit

Permalink
Add basic http router benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Dec 19, 2023
1 parent 53e835c commit 03c2997
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/HttpRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ struct HttpRouter {
inline std::pair<std::string_view, bool> getUrlSegment(int urlSegment) {
if (urlSegment > urlSegmentTop) {
/* Signal as STOP when we have no more URL or stack space */
if (!currentUrl.length() || urlSegment > 99) {
if (!currentUrl.length() || urlSegment > MAX_URL_SEGMENTS - 1) {
return {{}, true};
}

Expand Down Expand Up @@ -292,12 +292,6 @@ struct HttpRouter {
/* Alloate this handler */
handlers.emplace_back(std::move(handler));

/* Assume can find this handler again */
if (((handlers.size() - 1) | priority) != findHandler(methods[0], pattern, priority)) {
std::cerr << "Error: Internal routing error" << std::endl;
std::abort();
}

/* ANY method must be last, GET must be first */
std::sort(root.children.begin(), root.children.end(), [](const auto &a, const auto &b) {
/* Assuming the list of methods is unique, non-repeating */
Expand Down
26 changes: 26 additions & 0 deletions tests/HttpRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,36 @@ void testParameters() {
assert(result == "GLWGPW");
}

#include <chrono>

void testPerformance() {
std::cout << "TestPerformance" << std::endl;
uWS::HttpRouter<int> r;

r.add({"GET"}, "/*", [](auto *h) {
return true;
});

r.add({"*"}, "/*", [](auto *h) {
return true;
});

auto start = std::chrono::steady_clock::now();
for (int i = 0; i < 1000000; i++) {
r.route("GET", "/something");
r.route("other", "/whatever");
}
auto end = std::chrono::steady_clock::now();

auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Duration: " << duration << "ms" << std::endl;
}

int main() {
testPatternPriority();
testMethodPriority();
testUpgrade();
testBugReports();
testParameters();
testPerformance();
}
4 changes: 4 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ default:
$(CXX) -std=c++17 -fsanitize=address HttpParser.cpp -o HttpParser
./HttpParser

performance:
$(CXX) -std=c++17 HttpRouter.cpp -O3 -o HttpRouter
./HttpRouter

smoke:
../Crc32 &
sleep 1
Expand Down

0 comments on commit 03c2997

Please sign in to comment.