more benchmarks

This commit is contained in:
Nicholas Orlowsky 2023-10-14 19:34:02 -04:00
parent ef78691f97
commit 89a6d9a528
No known key found for this signature in database
GPG key ID: BE7DF0188A405E2B
6 changed files with 82 additions and 26 deletions

View file

@ -2,7 +2,7 @@
A simple web server written in C++ A simple web server written in C++
## Module-Based Backends ## Module-Based Backends
Anthracite includes (read: will include) a system for allowing different "backend modules" to handle requests. Anthracite includes (read: will include) system for allowing different "backend modules" to handle requests.
This allows for anthracite to be extended for additional use-cases. For example, the following This allows for anthracite to be extended for additional use-cases. For example, the following
backends could be implemented: backends could be implemented:

View file

@ -2,5 +2,5 @@
class backend { class backend {
public: public:
virtual http_response handle_request(http_request req) {}; virtual http_response handle_request(http_request req) = 0;
}; };

View file

@ -1,8 +1,12 @@
#include "backend.cpp" #include "backend.cpp"
#include <filesystem>
class file_backend : public backend { class file_backend : public backend {
public: private:
http_response handle_request(http_request req) { unordered_map<string, string> file_cache;
bool cache_enabled;
http_response handle_request_nocache(http_request req) {
string filename = req.path() == "/" ? "index.html" : req.path(); string filename = req.path() == "/" ? "index.html" : req.path();
filename = "./www/" + filename; filename = "./www/" + filename;
ifstream stream(filename); ifstream stream(filename);
@ -16,6 +20,53 @@ public:
stringstream buffer; stringstream buffer;
buffer << stream.rdbuf(); buffer << stream.rdbuf();
return http_response(buffer.str(), status); return { buffer.str(), status };
}
http_response handle_request_cache(http_request req) {
string filename = req.path() == "/" ? "/index.html" : req.path();
filename = "./www" + filename;
auto file_info = file_cache.find(filename);
int status = 200;
if (file_info == file_cache.end()) {
status = 404;
filename = "./error_pages/404.html";
file_info = file_cache.find(filename);
}
return { file_info->second, status };
}
void populate_cache_dir(string dir) {
filesystem::recursive_directory_iterator cur = begin(filesystem::recursive_directory_iterator(dir));
filesystem::recursive_directory_iterator fin = end(filesystem::recursive_directory_iterator(dir));
while (cur != fin) {
auto p = cur->path();
string filename = p.string();
stringstream buffer;
ifstream stream(filename);
buffer << stream.rdbuf();
file_cache[filename] = buffer.str();
cout << "File at " << filename << " cached (" << file_cache[filename].size() << " bytes)" << endl;
++cur;
}
}
void populate_cache() {
populate_cache_dir("./www/");
populate_cache_dir("./error_pages/");
}
public:
file_backend(bool enable_cache) : cache_enabled(enable_cache) {
if(cache_enabled) {
populate_cache();
}
}
http_response handle_request(http_request req) override {
return cache_enabled ? handle_request_cache(req) : handle_request_nocache(req);
} }
}; };

View file

@ -142,11 +142,11 @@ static unordered_map<int, string> const http_status_map = {
class name_value { class name_value {
private: private:
string _name; string _name;
string _value; string _value;
protected: protected:
name_value() {} name_value() {}
public: public:
name_value(string name, string value) name_value(string name, string value)
@ -155,11 +155,10 @@ public:
{ {
} }
virtual ~name_value() = default; virtual ~name_value() = default;
name_value(const name_value &) = default; name_value(const name_value&) = default;
name_value& operator =(name_value const&) = default; name_value& operator=(name_value const&) = default;
name_value(name_value&&) = default; name_value(name_value&&) = default;
name_value& operator = (name_value&&) = default; name_value& operator=(name_value&&) = default;
string name() { return _name; } string name() { return _name; }
string value() { return _value; } string value() { return _value; }

View file

@ -5,6 +5,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <sstream> #include <sstream>
#include <sys/socket.h> #include <sys/socket.h>
#include <thread>
#include <unistd.h> #include <unistd.h>
#include <unordered_map> #include <unordered_map>
@ -14,6 +15,15 @@ void log_request_an_response(http_request req, http_response resp);
constexpr int default_port = 80; constexpr int default_port = 80;
void handle_client(anthracite_socket s, file_backend fb)
{
http_request req(s);
http_response resp = fb.handle_request(req);
log_request_an_response(req, resp);
s.send_message(resp.to_string());
s.close_conn();
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int port_number = default_port; int port_number = default_port;
@ -22,25 +32,21 @@ int main(int argc, char** argv)
port_number = atoi(argv[1]); port_number = atoi(argv[1]);
} }
cout << "Initializing Anthracite\n"; cout << "Initializing Anthracite" << endl;
anthracite_socket s(port_number); anthracite_socket s(port_number);
cout << "Initialization Complete\n"; file_backend fb(false);
cout << "Listening for HTTP connections on port " << port_number << "\n"; cout << "Initialization Complete" << endl;
file_backend fb; cout << "Listening for HTTP connections on port " << port_number << endl;
while (true) { while(true) {
s.wait_for_conn(); s.wait_for_conn();
http_request req(s); thread(handle_client, s, ref(fb)).detach();
http_response resp = fb.handle_request(req);
log_request_an_response(req, resp);
s.send_message(resp.to_string());
s.close_conn();
} }
return 0; exit(0);
} }
void log_request_an_response(http_request req, http_response resp) void log_request_an_response(http_request req, http_response resp)
{ {
cout << "[" << resp.status_code() << " " + http_status_map.find(resp.status_code())->second + "] " + req.client_ip() + " " + http_reverse_method_map.find(req.method())->second + " " + req.path() + "\n"; cout << "[" << resp.status_code() << " " + http_status_map.find(resp.status_code())->second + "] " + req.client_ip() + " " + http_reverse_method_map.find(req.method())->second + " " + req.path() << endl;
} }

View file

@ -20,7 +20,7 @@ private:
socklen_t client_addr_len {}; socklen_t client_addr_len {};
public: public:
anthracite_socket(int port, int max_queue = 10) anthracite_socket(int port, int max_queue = 100)
: server_socket(socket(AF_INET, SOCK_STREAM, 0)) : server_socket(socket(AF_INET, SOCK_STREAM, 0))
, client_ip("") , client_ip("")
{ {