Add an example to test server concurrency.

master
Chunting Gu 6 years ago
parent 2c75644622
commit 7cb5bca0d0

@ -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})

@ -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 <workers> <url>" << 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());
}

@ -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<HelloView>());
server.Route("/", std::make_shared<HelloView>(sleep_seconds));
server.Run();
server.Run(workers);
} catch (const std::exception&) {
return 1;

@ -84,6 +84,10 @@ public:
return data_;
}
bool compressed() const {
return compressed_;
}
#if WEBCC_ENABLE_GZIP
bool Compress() override;

@ -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);

Loading…
Cancel
Save