mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 08:34:47 +08:00
bench
This commit is contained in:
@@ -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 <auto func, typename... Args>
|
||||
asio::awaitable<
|
||||
@@ -183,5 +189,6 @@ private:
|
||||
|
||||
tcp_socket socket_;
|
||||
std::string body_;
|
||||
bool tcp_no_delay_ = true;
|
||||
};
|
||||
} // namespace rest_rpc
|
||||
@@ -68,6 +68,8 @@ public:
|
||||
|
||||
template <auto func> void remove_handler() { router_.remove_handler<func>(); }
|
||||
|
||||
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<rpc_connection>(std::move(socket), conn_id, router_);
|
||||
@@ -169,5 +175,6 @@ private:
|
||||
std::atomic<bool> has_stop_ = false;
|
||||
std::unordered_map<uint64_t, std::shared_ptr<rpc_connection>> conns_;
|
||||
rpc_router router_;
|
||||
bool tcp_no_delay_ = true;
|
||||
};
|
||||
} // namespace rest_rpc
|
||||
@@ -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)
|
||||
#add_test(NAME test_rpc COMMAND test_rpc)
|
||||
|
||||
add_executable(bench bench.cpp)
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
#include <rest_rpc/client.hpp>
|
||||
#include <rest_rpc/rest_rpc_server.hpp>
|
||||
|
||||
using namespace rest_rpc;
|
||||
|
||||
std::string address = "0.0.0.0:9004";
|
||||
|
||||
std::atomic<size_t> 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<void> bench(std::shared_ptr<client> cl) {
|
||||
co_await cl->connect(address);
|
||||
std::string_view str = "it is a test";
|
||||
while (true) {
|
||||
auto result = co_await cl->call<echo_sv>(str);
|
||||
// std::cout <<result.value << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void watch() {
|
||||
while (true) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
auto qps = g_qps.exchange(0, std::memory_order::acquire);
|
||||
std::cout << "qps: " << qps << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
rest_rpc_server server(address);
|
||||
server.register_handler<echo_sv>();
|
||||
if (argc == 1) {
|
||||
std::thread thd([] { watch(); });
|
||||
server.start();
|
||||
thd.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto arg = argv[1];
|
||||
int par = atoi(arg);
|
||||
|
||||
std::vector<std::shared_ptr<client>> clients;
|
||||
for (int i = 0; i < par; i++) {
|
||||
clients.push_back(std::make_shared<client>());
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < par; i++) {
|
||||
asio::co_spawn(clients[i]->get_executor(), bench(clients[i]),
|
||||
asio::detached);
|
||||
}
|
||||
std::getchar();
|
||||
}
|
||||
Reference in New Issue
Block a user