From b36d99930057f5af98bafa0322cf161ed0bc64a7 Mon Sep 17 00:00:00 2001 From: jie Date: Sun, 8 Aug 2021 08:56:35 +0800 Subject: [PATCH] add test & update readme --- README.md | 4 ++-- bserv/config.hpp | 8 +++++--- bserv/server.hpp | 2 +- main.cpp | 10 ++++++---- scripts/request_test.py | 18 +++++++++++++----- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index fa57029..1ac0b1a 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,9 @@ You can import the sample database: ``` -### Routing +### Server & Routing -Configure routing in [routing.hpp](routing.hpp). +Configure server & routing in [main.cpp](main.cpp). ### Handlers diff --git a/bserv/config.hpp b/bserv/config.hpp index 0074b41..476417a 100644 --- a/bserv/config.hpp +++ b/bserv/config.hpp @@ -5,18 +5,20 @@ #include #include #include +#include namespace bserv { const std::string NAME = "bserv"; const unsigned short PORT = 8080; -const int NUM_THREADS = 4; +const int NUM_THREADS = + std::thread::hardware_concurrency() > 0 ? std::thread::hardware_concurrency() : 1; -const std::size_t PAYLOAD_LIMIT = 1 * 1024 * 1024; +const std::size_t PAYLOAD_LIMIT = 8 * 1024 * 1024; const int EXPIRY_TIME = 30; // seconds -const std::size_t LOG_ROTATION_SIZE = 4 * 1024 * 1024; +const std::size_t LOG_ROTATION_SIZE = 8 * 1024 * 1024; const std::string LOG_PATH = "./log/" + NAME; const int NUM_DB_CONN = 10; diff --git a/bserv/server.hpp b/bserv/server.hpp index 145eb9e..682438f 100644 --- a/bserv/server.hpp +++ b/bserv/server.hpp @@ -45,7 +45,7 @@ private: std::shared_ptr session_mgr_; std::shared_ptr db_conn_mgr_; public: - server(const server_config& config, router&& routes, router&& ws_routes); + server(const server_config& config, router&& routes, router&& ws_routes = {}); }; } // bserv diff --git a/main.cpp b/main.cpp index 4ed9d9a..f21e6a8 100644 --- a/main.cpp +++ b/main.cpp @@ -12,8 +12,8 @@ void show_usage(const bserv::server_config& config) { "Option:\n" " -h, --help show help and exit\n" " -p, --port port (default: 8080)\n" - " --threads number of threads (default: 4)\n" - " --rotation log rotation size in mega bytes (default: 4)\n" + " --threads number of threads (default: # of cpu cores)\n" + " --rotation log rotation size in mega bytes (default: 8)\n" " --log-path log path (default: ./log/bserv)\n" " --num-conn number of database connections (default: 10)\n" " -c, --conn-str connection string (default: dbname=bserv)" @@ -125,11 +125,13 @@ int main(int argc, char* argv[]) { bserv::placeholders::json_params), bserv::make_path("/echo", &echo, bserv::placeholders::json_params) - }, { + } + , { bserv::make_path("/echo", &ws_echo, bserv::placeholders::session, bserv::placeholders::websocket_server_ptr) - }}; + } + }; return EXIT_SUCCESS; } diff --git a/scripts/request_test.py b/scripts/request_test.py index a0e6174..9bf9f8d 100644 --- a/scripts/request_test.py +++ b/scripts/request_test.py @@ -8,13 +8,21 @@ from pprint import pprint from time import time -# session = requests.session() -# pprint(session.post("http://localhost:8080/send", json={"id": "abc"}).json()) -# pprint(session.post("http://localhost:8080/send", json={"id": "def"}).json()) -# pprint(session.post("http://localhost:8080/send", json={"id": "ghi"}).json()) +def size_test(): + session = requests.session() + length = 4 * 1024 * 1024 # ~4MB + data = {"id": "a" * length} + res = session.post("http://localhost:8080/echo", json=data).json() + if {"echo": data} != res: + print("size test: failed") + else: + print("size test: ok") + print() + +size_test() # exit() -P = 100 # number of concurrent processes +P = 500 # number of concurrent processes N = 10 # for each process, the number of sessions R = 10 # for each session, the number of posts