| 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 |
#pragma once |
| 32 |
|
| 33 |
#include <atomic> |
| 34 |
|
| 35 |
#include <boost/uuid/uuid_io.hpp> |
| 36 |
|
| 37 |
#include "include_base_utils.h" |
| 38 |
#include "string_tools.h" |
| 39 |
#include "net/levin_protocol_handler_async.h" |
| 40 |
#include "net/abstract_tcp_server2.h" |
| 41 |
#include "serialization/keyvalue_serialization.h" |
| 42 |
|
| 43 |
#include "../unit_tests/unit_tests_utils.h" |
| 44 |
|
| 45 |
namespace net_load_tests |
| 46 |
{ |
| 47 |
struct test_connection_context : epee::net_utils::connection_context_base |
| 48 |
{ |
| 49 |
test_connection_context(): epee::net_utils::connection_context_base(boost::uuids::nil_uuid(), {}, false, false), m_closed(false) {} |
| 50 |
static constexpr int handshake_command() noexcept { return 1001; } |
| 51 |
static constexpr bool handshake_complete() noexcept { return true; } |
| 52 |
size_t get_max_bytes(int command) const { return LEVIN_DEFAULT_MAX_PACKET_SIZE; } |
| 53 |
volatile bool m_closed; |
| 54 |
}; |
| 55 |
|
| 56 |
typedef epee::levin::async_protocol_handler<test_connection_context> test_levin_protocol_handler; |
| 57 |
typedef epee::levin::async_protocol_handler_config<test_connection_context> test_levin_protocol_handler_config; |
| 58 |
typedef epee::net_utils::connection<test_levin_protocol_handler> test_connection; |
| 59 |
typedef epee::net_utils::boosted_tcp_server<test_levin_protocol_handler> test_tcp_server; |
| 60 |
|
| 61 |
struct test_levin_commands_handler : public epee::levin::levin_commands_handler<test_connection_context> |
| 62 |
{ |
| 63 |
test_levin_commands_handler() |
| 64 |
|
| 65 |
|
| 66 |
{ |
| 67 |
} |
| 68 |
|
| 69 |
virtual int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_stream& buff_out, test_connection_context& context) |
| 70 |
{ |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
return LEVIN_OK; |
| 78 |
} |
| 79 |
|
| 80 |
virtual int notify(int command, const epee::span<const uint8_t> in_buff, test_connection_context& context) |
| 81 |
{ |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
return LEVIN_OK; |
| 88 |
} |
| 89 |
|
| 90 |
virtual void callback(test_connection_context& context) |
| 91 |
{ |
| 92 |
|
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
virtual void on_connection_new(test_connection_context& context) |
| 97 |
{ |
| 98 |
m_new_connection_counter.inc(); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
virtual void on_connection_close(test_connection_context& context) |
| 103 |
{ |
| 104 |
m_close_connection_counter.inc(); |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
size_t new_connection_counter() const { return m_new_connection_counter.get(); } |
| 112 |
size_t close_connection_counter() const { return m_close_connection_counter.get(); } |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected: |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
unit_test::call_counter m_new_connection_counter; |
| 128 |
unit_test::call_counter m_close_connection_counter; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
}; |
| 138 |
|
| 139 |
class open_close_test_helper |
| 140 |
{ |
| 141 |
public: |
| 142 |
open_close_test_helper(test_tcp_server& tcp_server, size_t open_request_target, size_t max_opened_connection_count) |
| 143 |
: m_tcp_server(tcp_server) |
| 144 |
, m_max_opened_connection_count(max_opened_connection_count) |
| 145 |
, m_opened_connection_count(0) |
| 146 |
, m_next_opened_conn_idx(0) |
| 147 |
, m_next_closed_conn_idx(0) |
| 148 |
, m_connections(open_request_target) |
| 149 |
{ |
| 150 |
for (auto& conn_id : m_connections) |
| 151 |
conn_id = boost::uuids::nil_uuid(); |
| 152 |
} |
| 153 |
|
| 154 |
bool handle_new_connection(const boost::uuids::uuid& connection_id, bool ignore_close_fails = false) |
| 155 |
{ |
| 156 |
size_t idx = m_next_opened_conn_idx.fetch_add(1, std::memory_order_relaxed); |
| 157 |
if (idx >= m_connections.size()) |
| 158 |
{ |
| 159 |
LOG_PRINT_L0("ERROR: connections overflow"); |
| 160 |
exit(1); |
| 161 |
} |
| 162 |
m_connections[idx] = connection_id; |
| 163 |
|
| 164 |
size_t prev_connection_count = m_opened_connection_count.fetch_add(1, std::memory_order_relaxed); |
| 165 |
if (m_max_opened_connection_count <= prev_connection_count) |
| 166 |
{ |
| 167 |
return close_next_connection(ignore_close_fails); |
| 168 |
} |
| 169 |
|
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
void close_remaining_connections() |
| 174 |
{ |
| 175 |
while (close_next_connection(false)); |
| 176 |
} |
| 177 |
|
| 178 |
bool close_next_connection(bool ignore_close_fails) |
| 179 |
{ |
| 180 |
size_t idx = m_next_closed_conn_idx.fetch_add(1, std::memory_order_relaxed); |
| 181 |
if (m_next_opened_conn_idx.load(std::memory_order_relaxed) <= idx) |
| 182 |
{ |
| 183 |
LOG_PRINT_L0("Not enough opened connections"); |
| 184 |
return false; |
| 185 |
} |
| 186 |
if (m_connections[idx].is_nil()) |
| 187 |
{ |
| 188 |
LOG_PRINT_L0("Connection isn't opened"); |
| 189 |
return false; |
| 190 |
} |
| 191 |
if (!m_tcp_server.get_config_object().close(m_connections[idx], true)) |
| 192 |
{ |
| 193 |
LOG_PRINT_L0("Close connection error: " << m_connections[idx]); |
| 194 |
if (!ignore_close_fails) |
| 195 |
{ |
| 196 |
return false; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
m_connections[idx] = boost::uuids::nil_uuid(); |
| 201 |
m_opened_connection_count.fetch_sub(1, std::memory_order_relaxed); |
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
size_t opened_connection_count() const { return m_opened_connection_count.load(std::memory_order_relaxed); } |
| 206 |
|
| 207 |
private: |
| 208 |
test_tcp_server& m_tcp_server; |
| 209 |
size_t m_max_opened_connection_count; |
| 210 |
std::atomic<size_t> m_opened_connection_count; |
| 211 |
std::atomic<size_t> m_next_opened_conn_idx; |
| 212 |
std::atomic<size_t> m_next_closed_conn_idx; |
| 213 |
std::vector<boost::uuids::uuid> m_connections; |
| 214 |
}; |
| 215 |
|
| 216 |
const unsigned int min_thread_count = 2; |
| 217 |
const std::string clt_port("36230"); |
| 218 |
const std::string srv_port("36231"); |
| 219 |
|
| 220 |
enum command_ids |
| 221 |
{ |
| 222 |
cmd_close_all_connections_id = 73564, |
| 223 |
cmd_start_open_close_test_id, |
| 224 |
cmd_get_statistics_id, |
| 225 |
cmd_reset_statistics_id, |
| 226 |
cmd_shutdown_id, |
| 227 |
cmd_send_data_requests_id, |
| 228 |
cmd_data_request_id |
| 229 |
}; |
| 230 |
|
| 231 |
struct CMD_CLOSE_ALL_CONNECTIONS |
| 232 |
{ |
| 233 |
const static int ID = cmd_close_all_connections_id; |
| 234 |
|
| 235 |
struct request |
| 236 |
{ |
| 237 |
BEGIN_KV_SERIALIZE_MAP() |
| 238 |
END_KV_SERIALIZE_MAP() |
| 239 |
}; |
| 240 |
}; |
| 241 |
|
| 242 |
struct CMD_START_OPEN_CLOSE_TEST |
| 243 |
{ |
| 244 |
const static int ID = cmd_start_open_close_test_id; |
| 245 |
|
| 246 |
struct request |
| 247 |
{ |
| 248 |
uint64_t open_request_target; |
| 249 |
uint64_t max_opened_conn_count; |
| 250 |
|
| 251 |
BEGIN_KV_SERIALIZE_MAP() |
| 252 |
KV_SERIALIZE(open_request_target) |
| 253 |
KV_SERIALIZE(max_opened_conn_count) |
| 254 |
END_KV_SERIALIZE_MAP() |
| 255 |
}; |
| 256 |
|
| 257 |
struct response |
| 258 |
{ |
| 259 |
BEGIN_KV_SERIALIZE_MAP() |
| 260 |
END_KV_SERIALIZE_MAP() |
| 261 |
}; |
| 262 |
}; |
| 263 |
|
| 264 |
struct CMD_GET_STATISTICS |
| 265 |
{ |
| 266 |
const static int ID = cmd_get_statistics_id; |
| 267 |
|
| 268 |
struct request |
| 269 |
{ |
| 270 |
BEGIN_KV_SERIALIZE_MAP() |
| 271 |
END_KV_SERIALIZE_MAP() |
| 272 |
}; |
| 273 |
|
| 274 |
struct response |
| 275 |
{ |
| 276 |
uint64_t opened_connections_count; |
| 277 |
uint64_t new_connection_counter; |
| 278 |
uint64_t close_connection_counter; |
| 279 |
|
| 280 |
BEGIN_KV_SERIALIZE_MAP() |
| 281 |
KV_SERIALIZE(opened_connections_count) |
| 282 |
KV_SERIALIZE(new_connection_counter) |
| 283 |
KV_SERIALIZE(close_connection_counter) |
| 284 |
END_KV_SERIALIZE_MAP() |
| 285 |
|
| 286 |
std::string to_string() const |
| 287 |
{ |
| 288 |
std::stringstream ss; |
| 289 |
ss << "opened_connections_count = " << opened_connections_count << |
| 290 |
", new_connection_counter = " << new_connection_counter << |
| 291 |
", close_connection_counter = " << close_connection_counter; |
| 292 |
return ss.str(); |
| 293 |
} |
| 294 |
}; |
| 295 |
}; |
| 296 |
|
| 297 |
struct CMD_RESET_STATISTICS |
| 298 |
{ |
| 299 |
const static int ID = cmd_reset_statistics_id; |
| 300 |
|
| 301 |
struct request |
| 302 |
{ |
| 303 |
BEGIN_KV_SERIALIZE_MAP() |
| 304 |
END_KV_SERIALIZE_MAP() |
| 305 |
}; |
| 306 |
|
| 307 |
struct response |
| 308 |
{ |
| 309 |
BEGIN_KV_SERIALIZE_MAP() |
| 310 |
END_KV_SERIALIZE_MAP() |
| 311 |
}; |
| 312 |
}; |
| 313 |
|
| 314 |
struct CMD_SHUTDOWN |
| 315 |
{ |
| 316 |
const static int ID = cmd_shutdown_id; |
| 317 |
|
| 318 |
struct request |
| 319 |
{ |
| 320 |
BEGIN_KV_SERIALIZE_MAP() |
| 321 |
END_KV_SERIALIZE_MAP() |
| 322 |
}; |
| 323 |
}; |
| 324 |
|
| 325 |
struct CMD_SEND_DATA_REQUESTS |
| 326 |
{ |
| 327 |
const static int ID = cmd_send_data_requests_id; |
| 328 |
|
| 329 |
struct request |
| 330 |
{ |
| 331 |
uint64_t request_size; |
| 332 |
|
| 333 |
BEGIN_KV_SERIALIZE_MAP() |
| 334 |
KV_SERIALIZE(request_size) |
| 335 |
END_KV_SERIALIZE_MAP() |
| 336 |
}; |
| 337 |
}; |
| 338 |
|
| 339 |
struct CMD_DATA_REQUEST |
| 340 |
{ |
| 341 |
const static int ID = cmd_data_request_id; |
| 342 |
|
| 343 |
struct request |
| 344 |
{ |
| 345 |
std::string data; |
| 346 |
uint64_t response_size; |
| 347 |
|
| 348 |
BEGIN_KV_SERIALIZE_MAP() |
| 349 |
KV_SERIALIZE(data) |
| 350 |
END_KV_SERIALIZE_MAP() |
| 351 |
}; |
| 352 |
|
| 353 |
struct response |
| 354 |
{ |
| 355 |
std::string data; |
| 356 |
|
| 357 |
BEGIN_KV_SERIALIZE_MAP() |
| 358 |
KV_SERIALIZE(data) |
| 359 |
END_KV_SERIALIZE_MAP() |
| 360 |
}; |
| 361 |
}; |
| 362 |
} |