diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 938cbc5..5b326f7 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -29,6 +29,9 @@ if(UNIX) set(EXAMPLE_LIBS ${EXAMPLE_LIBS} ${CMAKE_DL_LIBS}) endif() +add_executable(concurrency_test concurrency_test.cc) +target_link_libraries(concurrency_test ${EXAMPLE_LIBS}) + add_executable(client_basics client_basics.cc) target_link_libraries(client_basics ${EXAMPLE_LIBS}) diff --git a/bak/concurrency_test.cc b/examples/concurrency_test.cc similarity index 69% rename from bak/concurrency_test.cc rename to examples/concurrency_test.cc index 4cb6a51..06262b6 100644 --- a/bak/concurrency_test.cc +++ b/examples/concurrency_test.cc @@ -6,18 +6,17 @@ #include "webcc/client_session.h" #include "webcc/logger.h" -// CS Server: -// $ concurrency_test 10 http://localhost:55555/patient/query?global_search=ivory&count=200&offset=0&sort_item=birth_date&sort_order=asc -// GitHub: -// $ concurrency_test 10 https://api.github.com/public/events - int main(int argc, const char* argv[]) { if (argc < 3) { std::cerr << "Usage: concurrency_test " << std::endl; + std::cerr << "E.g.," << std::endl; + std::cerr << " $ concurrency_test 10 https://api.github.com/public/events" + << std::endl; + std::cerr << " $ concurrency_test 10 http://localhost:8080/" << std::endl; return 1; } - WEBCC_LOG_INIT("", webcc::LOG_CONSOLE_FILE_OVERWRITE); + WEBCC_LOG_INIT("", webcc::LOG_CONSOLE); int workers = std::atoi(argv[1]); std::string url = argv[2]; @@ -29,13 +28,17 @@ int main(int argc, const char* argv[]) { for (int i = 0; i < workers; ++i) { threads.emplace_back([&url]() { + // NOTE: Each thread has its own client session. webcc::ClientSession session; session.set_timeout(180); try { LOG_USER("Start"); - session.Get(url); + + session.Send(webcc::RequestBuilder{}.Get(url)()); + LOG_USER("End"); + } catch (const webcc::Error& error) { LOG_ERRO("Error: %s", error.message().c_str()); } diff --git a/examples/hello_world_server.cc b/examples/hello_world_server.cc index 16b29af..927998a 100644 --- a/examples/hello_world_server.cc +++ b/examples/hello_world_server.cc @@ -4,24 +4,47 @@ class HelloView : public webcc::View { public: + HelloView(int sleep_seconds) : sleep_seconds_(sleep_seconds) { + } + webcc::ResponsePtr Handle(webcc::RequestPtr request) override { + if (sleep_seconds_ > 0) { + std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_)); + } + if (request->method() == "GET") { return webcc::ResponseBuilder{}.OK().Body("Hello, World!")(); } return {}; } + +private: + int sleep_seconds_; }; -int main() { +int main(int argc, const char* argv[]) { WEBCC_LOG_INIT("", webcc::LOG_CONSOLE); + int workers = 1; + int sleep_seconds = 0; + + if (argc > 1) { + workers = std::stoi(argv[1]); + if (argc > 2) { + sleep_seconds = std::stoi(argv[2]); + } + } + + LOG_USER("Workers: %d", workers); + LOG_USER("Sleep seconds: %d", sleep_seconds); + try { webcc::Server server(8080); - server.Route("/", std::make_shared()); + server.Route("/", std::make_shared(sleep_seconds)); - server.Run(); + server.Run(workers); } catch (const std::exception&) { return 1; diff --git a/webcc/body.h b/webcc/body.h index 1c0a9c4..b4f443d 100644 --- a/webcc/body.h +++ b/webcc/body.h @@ -84,6 +84,10 @@ public: return data_; } + bool compressed() const { + return compressed_; + } + #if WEBCC_ENABLE_GZIP bool Compress() override; diff --git a/webcc/parser.cc b/webcc/parser.cc index a8a5383..6c68cdb 100644 --- a/webcc/parser.cc +++ b/webcc/parser.cc @@ -55,7 +55,9 @@ bool StringBodyHandler::Finish() { return false; } #else - LOG_WARN("Compressed HTTP content remains untouched."); + if (body->compressed()) { + LOG_WARN("Compressed HTTP content remains untouched."); + } #endif // WEBCC_ENABLE_GZIP message_->SetBody(body, false);