From 8d0a65163cf4a948aa16d1799ac32bc7f5b8a85d Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 22 Sep 2025 16:12:30 +0800 Subject: [PATCH] bench --- include/rest_rpc/client.hpp | 7 ++++ include/rest_rpc/rest_rpc_server.hpp | 7 ++++ tests/CMakeLists.txt | 4 +- tests/bench.cpp | 56 ++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/bench.cpp diff --git a/include/rest_rpc/client.hpp b/include/rest_rpc/client.hpp index 2fadaee..08fb11a 100644 --- a/include/rest_rpc/client.hpp +++ b/include/rest_rpc/client.hpp @@ -70,6 +70,10 @@ public: co_return conn_ec; } + if (tcp_no_delay_) { + socket_.set_option(asio::ip::tcp::no_delay(true)); + } + co_return std::error_code{}; } @@ -111,6 +115,8 @@ public: co_return std::get<1>(r); } + void enable_tcp_no_delay(bool r) { tcp_no_delay_ = r; } + private: template asio::awaitable< @@ -183,5 +189,6 @@ private: tcp_socket socket_; std::string body_; + bool tcp_no_delay_ = true; }; } // namespace rest_rpc \ No newline at end of file diff --git a/include/rest_rpc/rest_rpc_server.hpp b/include/rest_rpc/rest_rpc_server.hpp index 265baea..8ad13c2 100644 --- a/include/rest_rpc/rest_rpc_server.hpp +++ b/include/rest_rpc/rest_rpc_server.hpp @@ -68,6 +68,8 @@ public: template void remove_handler() { router_.remove_handler(); } + void enable_tcp_no_delay(bool r) { tcp_no_delay_ = r; } + private: std::error_code listen() { using asio::ip::tcp; @@ -125,6 +127,10 @@ private: co_return; } + if (tcp_no_delay_) { + socket.set_option(asio::ip::tcp::no_delay(true)); + } + REST_LOG_INFO << "new connction comming..."; auto conn = std::make_shared(std::move(socket), conn_id, router_); @@ -169,5 +175,6 @@ private: std::atomic has_stop_ = false; std::unordered_map> conns_; rpc_router router_; + bool tcp_no_delay_ = true; }; } // namespace rest_rpc \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 58871e1..ca598b5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,4 +15,6 @@ add_executable(${project_name} add_executable(test_rpc test_rest_rpc1.cpp) add_test(NAME ${project_name} COMMAND test_rest_rpc) -#add_test(NAME test_rpc COMMAND test_rpc) \ No newline at end of file +#add_test(NAME test_rpc COMMAND test_rpc) + +add_executable(bench bench.cpp) \ No newline at end of file diff --git a/tests/bench.cpp b/tests/bench.cpp new file mode 100644 index 0000000..e14d8e7 --- /dev/null +++ b/tests/bench.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace rest_rpc; + +std::string address = "0.0.0.0:9004"; + +std::atomic g_qps = 0; + +std::string_view echo_sv(std::string_view str) { + g_qps.fetch_add(1, std::memory_order::release); + return str; +} + +asio::awaitable bench(std::shared_ptr cl) { + co_await cl->connect(address); + std::string_view str = "it is a test"; + while (true) { + auto result = co_await cl->call(str); + // std::cout <(); + if (argc == 1) { + std::thread thd([] { watch(); }); + server.start(); + thd.join(); + return 0; + } + + auto arg = argv[1]; + int par = atoi(arg); + + std::vector> clients; + for (int i = 0; i < par; i++) { + clients.push_back(std::make_shared()); + } + + for (size_t i = 0; i < par; i++) { + asio::co_spawn(clients[i]->get_executor(), bench(clients[i]), + asio::detached); + } + std::getchar(); +} \ No newline at end of file