Debian Codegraph @

tests/net_load_tests/net_load_tests.h

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
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 //: m_return_code(LEVIN_OK)
65 //, m_last_command(-1)
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 //m_invoke_counter.inc();
72 //std::unique_lock<std::mutex> lock(m_mutex);
73 //m_last_command = command;
74 //m_last_in_buf = in_buff;
75 //buff_out = m_invoke_out_buf;
76 //return m_return_code;
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 //m_notify_counter.inc();
83 //std::unique_lock<std::mutex> lock(m_mutex);
84 //m_last_command = command;
85 //m_last_in_buf = in_buff;
86 //return m_return_code;
87 return LEVIN_OK;
88 }
89
90 virtual void callback(test_connection_context& context)
91 {
92 //m_callback_counter.inc();
93 //std::cout << "test_levin_commands_handler::callback()" << std::endl;
94 }
95
96 virtual void on_connection_new(test_connection_context& context)
97 {
98 m_new_connection_counter.inc();
99 //std::cout << "test_levin_commands_handler::on_connection_new()" << std::endl;
100 }
101
102 virtual void on_connection_close(test_connection_context& context)
103 {
104 m_close_connection_counter.inc();
105 //std::cout << "test_levin_commands_handler::on_connection_close()" << std::endl;
106 }
107
108 //size_t invoke_counter() const { return m_invoke_counter.get(); }
109 //size_t notify_counter() const { return m_notify_counter.get(); }
110 //size_t callback_counter() const { return m_callback_counter.get(); }
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 //int return_code() const { return m_return_code; }
115 //void return_code(int v) { m_return_code = v; }
116
117 //const std::string& invoke_out_buf() const { return m_invoke_out_buf; }
118 //void invoke_out_buf(const std::string& v) { m_invoke_out_buf = v; }
119
120 //int last_command() const { return m_last_command; }
121 //const std::string& last_in_buf() const { return m_last_in_buf; }
122
123 protected:
124 //unit_test::call_counter m_invoke_counter;
125 //unit_test::call_counter m_notify_counter;
126 //unit_test::call_counter m_callback_counter;
127 unit_test::call_counter m_new_connection_counter;
128 unit_test::call_counter m_close_connection_counter;
129
130 //std::mutex m_mutex;
131
132 //int m_return_code;
133 //std::string m_invoke_out_buf;
134
135 //int m_last_command;
136 //std::string m_last_in_buf;
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 }