request parser speed improvements and test updates
Some checks failed
Docker Build & Publish / build (push) Failing after 2s

This commit is contained in:
Nicholas Orlowsky 2025-02-05 09:07:21 -05:00
parent 95430a5dc3
commit f930792a11
Signed by: nickorlow
GPG key ID: 838827D8C4611687
5 changed files with 127 additions and 78 deletions

View file

@ -2,63 +2,65 @@
#include <fstream>
#include <chrono>
#include "../lib/http/request.hpp"
#include <boost/beast.hpp>
#ifdef SPEEDTEST_COMPARE_BOOST
#include <boost/beast.hpp>
#endif
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
constexpr uint32_t num_requests = 10000000;
TEST(speed_tests, request_parse) {
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
std::ifstream t("./test_files/test_request.http");
std::stringstream buffer;
buffer << t.rdbuf();
std::string raw_req = buffer.str();
auto t1 = high_resolution_clock::now();
for(int i = 0; i < 10000000; ++i) {
auto start = high_resolution_clock::now();
for(int i = 0; i < num_requests; ++i) {
volatile anthracite::http::request req (raw_req, "0.0.0.0");
}
auto t2 = high_resolution_clock::now();
auto ms_int = duration_cast<milliseconds>(t2 - t1);
auto end = high_resolution_clock::now();
std::cout << "Parsed 1 Million requests in " << ms_int << "ms" << std::endl;
auto ms_int = duration_cast<milliseconds>(end-start);
double m_rps = ((1000.0 / ms_int.count()) * num_requests) / 1000000;
std::cout << "Parsed " << (num_requests/1000000) << " Million requests in " << ms_int << " ms";
std::cout << " at " << m_rps << " Million RPS " << std::endl;
ASSERT_LT(ms_int.count(), 2000);
}
#ifdef SPEEDTEST_COMPARE_BOOST
TEST(speed_tests, boost) {
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
std::ifstream t("./test_files/test_request.http");
std::stringstream buffer;
buffer << t.rdbuf();
std::string raw_req = buffer.str();
auto t1 = high_resolution_clock::now();
for(int i = 0; i < 10000000; ++i) {
auto start = high_resolution_clock::now();
for(int i = 0; i < num_requests; ++i) {
boost::system::error_code ec;
boost::beast::http::request_parser<boost::beast::http::string_body> p;
p.put(boost::asio::buffer(raw_req), ec);
boost::beast::http::request<boost::beast::http::string_body> r = p.get();
}
auto t2 = high_resolution_clock::now();
auto ms_int = duration_cast<milliseconds>(t2 - t1);
auto end = high_resolution_clock::now();
auto ms_int = duration_cast<milliseconds>(end-start);
std::cout << "Parsed 1 Million requests in " << ms_int << "ms" << std::endl;
}
TEST(speed_tests, single_request_parse) {
std::ifstream t("./test_files/test_request.http");
std::stringstream buffer;
buffer << t.rdbuf();
std::string raw_req = buffer.str();
anthracite::http::request req (raw_req, "0.0.0.0");
std::cout << req.to_string() << std::endl;
double m_rps = ((1000.0 / ms_int.count()) * num_requests) / 1000000;
std::cout << "Parsed " << (num_requests/1000000) << " Million requests in " << ms_int << " ms";
std::cout << " at " << m_rps << " Million RPS " << std::endl;
}
#endif

View file

@ -1,13 +1,15 @@
GET /foo/bar?test=a&test2=b HTTP/1.1
Host: example.org
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: */*
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Accept-Encoding: gzip,deflate
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
X-Requested-With: XMLHttpRequest
Referer: http://example.org/test
Cookie: foo=bar; lorem=ipsum;
Host: example.org
Keep-Alive: 115
Referer: http://example.org/test
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
X-Requested-With: XMLHttpRequest
{ "test": "content" }

17
tests/unit_tests.cpp Normal file
View file

@ -0,0 +1,17 @@
#include <gtest/gtest.h>
#include <fstream>
#include "../lib/http/request.hpp"
#include <boost/beast.hpp>
TEST(unit_tests, single_request_parse) {
std::ifstream t("./test_files/test_request.http");
std::stringstream buffer;
buffer << t.rdbuf();
std::string raw_req = buffer.str();
std::string expected = buffer.str();
anthracite::http::request req (raw_req, "0.0.0.0");
ASSERT_EQ(expected, req.to_string());
}