-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_mt.cpp
160 lines (136 loc) · 4.71 KB
/
benchmark_mt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "asio.hpp"
#include "debug.hpp"
#include "io_context_pool.hpp"
#include "resource_limit.hpp"
#include "runtime.hpp"
using namespace asio::ip;
using namespace print_hpp::log;
template <typename T>
requires(!std::is_reference_v<T>)
struct AsioCallbackAwaiter {
public:
using CallbackFunction
= std::function<void(std::coroutine_handle<>, std::function<void(T)>)>;
AsioCallbackAwaiter(CallbackFunction callback_function)
: callback_function_(std::move(callback_function)) {}
auto await_ready() noexcept -> bool {
return false;
}
void await_suspend(std::coroutine_handle<> handle) {
callback_function_(handle, [this](T t) {
result_ = std::move(t);
});
}
auto await_resume() noexcept -> T {
return std::move(result_);
}
private:
CallbackFunction callback_function_;
T result_;
};
inline auto async_accept(tcp::acceptor &acceptor, tcp::socket &socket) noexcept
-> Task<std::error_code> {
co_return co_await AsioCallbackAwaiter<std::error_code>{
[&](std::coroutine_handle<> handle, auto set_resume_value) {
acceptor.async_accept(
socket,
[handle, set_resume_value = std::move(set_resume_value)](
auto ec) mutable {
set_resume_value(std::move(ec));
handle.resume();
});
}};
}
template <typename Socket, typename AsioBuffer>
inline auto async_read_some(Socket &socket, AsioBuffer &&buffer) noexcept
-> Task<std::pair<std::error_code, size_t>> {
co_return co_await AsioCallbackAwaiter<std::pair<std::error_code, size_t>>{
[&](std::coroutine_handle<> handle, auto set_resume_value) mutable {
socket.async_read_some(
std::forward<AsioBuffer>(buffer),
[handle,
set_resume_value
= std::move(set_resume_value)](auto ec, auto size) mutable {
set_resume_value(std::make_pair(std::move(ec), size));
handle.resume();
});
}};
}
template <typename Socket, typename AsioBuffer>
inline auto async_write(Socket &socket, AsioBuffer &&buffer) noexcept
-> Task<std::pair<std::error_code, size_t>> {
co_return co_await AsioCallbackAwaiter<std::pair<std::error_code, size_t>>{
[&](std::coroutine_handle<> handle, auto set_resume_value) mutable {
asio::async_write(
socket,
std::forward<AsioBuffer>(buffer),
[handle,
set_resume_value
= std::move(set_resume_value)](auto ec, auto size) mutable {
set_resume_value(std::make_pair(std::move(ec), size));
handle.resume();
});
}};
}
constexpr std::string_view response = R"(HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 14
Hello, World!
)";
auto process(tcp::socket socket) -> Task<void> {
std::array<char, 128> data;
while (true) {
auto [ec, nread] = co_await async_read_some(socket, asio::buffer(data));
if (ec) {
LOG_ERROR("{}", ec.message());
co_return;
}
LOG_INFO("read {} bytes", nread);
co_await async_write(socket, asio::buffer(response));
}
}
class tcp_server {
public:
tcp_server(IOContextPool &pool, uint16_t port)
: pool_{pool}
, endpoint_{tcp::v4(), port}
, acceptor_{pool_.get_io_context(), endpoint_} {
LOG_INFO("Listening on {}:{} ...",
endpoint_.address().to_string(),
port);
spawn(listen());
}
auto listen() -> Task<void> {
while (true) {
auto &ctx = pool_.get_io_context();
tcp::socket socket(ctx);
auto ec = co_await async_accept(acceptor_, socket);
if (ec) [[unlikely]] {
LOG_ERROR("ec={}", ec.message());
co_return;
}
[[maybe_unused]] auto remote = socket.remote_endpoint();
LOG_INFO("new connection: {}:{}",
remote.address().to_string(),
remote.port());
spawn(process(std::move(socket)));
}
}
private:
IOContextPool &pool_;
tcp::endpoint endpoint_;
tcp::acceptor acceptor_;
};
auto main(int argc, char *argv[]) -> int {
if (argc != 2) {
printf("Usage: %s ${num_threads}\n", argv[0]);
return -1;
}
raise_resource_limits();
SET_LOG_LEVEL(LogLevel::WARN);
std::size_t num_threads = std::stoul(argv[1]);
IOContextPool pool{num_threads};
tcp_server server{pool, 8000};
pool.run();
restore_resource_limits();
}