Electroneum
net_load_tests.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 
34 #include <atomic>
35 
36 #include <boost/asio/io_service.hpp>
37 #include <boost/uuid/uuid_io.hpp>
38 
39 #include "include_base_utils.h"
40 #include "string_tools.h"
44 
45 #include "../unit_tests/unit_tests_utils.h"
46 
47 namespace net_load_tests
48 {
50  {
51  volatile bool m_closed;
52  };
53 
58 
59  struct test_levin_commands_handler : public epee::levin::levin_commands_handler<test_connection_context>
60  {
62  //: m_return_code(LEVIN_OK)
63  //, m_last_command(-1)
64  {
65  }
66 
67  virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, test_connection_context& context)
68  {
69  //m_invoke_counter.inc();
70  //std::unique_lock<std::mutex> lock(m_mutex);
71  //m_last_command = command;
72  //m_last_in_buf = in_buff;
73  //buff_out = m_invoke_out_buf;
74  //return m_return_code;
75  return LEVIN_OK;
76  }
77 
78  virtual int notify(int command, const epee::span<const uint8_t> in_buff, test_connection_context& context)
79  {
80  //m_notify_counter.inc();
81  //std::unique_lock<std::mutex> lock(m_mutex);
82  //m_last_command = command;
83  //m_last_in_buf = in_buff;
84  //return m_return_code;
85  return LEVIN_OK;
86  }
87 
89  {
90  //m_callback_counter.inc();
91  //std::cout << "test_levin_commands_handler::callback()" << std::endl;
92  }
93 
95  {
97  //std::cout << "test_levin_commands_handler::on_connection_new()" << std::endl;
98  }
99 
101  {
103  //std::cout << "test_levin_commands_handler::on_connection_close()" << std::endl;
104  }
105 
106  //size_t invoke_counter() const { return m_invoke_counter.get(); }
107  //size_t notify_counter() const { return m_notify_counter.get(); }
108  //size_t callback_counter() const { return m_callback_counter.get(); }
111 
112  //int return_code() const { return m_return_code; }
113  //void return_code(int v) { m_return_code = v; }
114 
115  //const std::string& invoke_out_buf() const { return m_invoke_out_buf; }
116  //void invoke_out_buf(const std::string& v) { m_invoke_out_buf = v; }
117 
118  //int last_command() const { return m_last_command; }
119  //const std::string& last_in_buf() const { return m_last_in_buf; }
120 
121  protected:
122  //unit_test::call_counter m_invoke_counter;
123  //unit_test::call_counter m_notify_counter;
124  //unit_test::call_counter m_callback_counter;
127 
128  //std::mutex m_mutex;
129 
130  //int m_return_code;
131  //std::string m_invoke_out_buf;
132 
133  //int m_last_command;
134  //std::string m_last_in_buf;
135  };
136 
138  {
139  public:
140  open_close_test_helper(test_tcp_server& tcp_server, size_t open_request_target, size_t max_opened_connection_count)
141  : m_tcp_server(tcp_server)
142  , m_max_opened_connection_count(max_opened_connection_count)
143  , m_opened_connection_count(0)
144  , m_next_opened_conn_idx(0)
145  , m_next_closed_conn_idx(0)
146  , m_connections(open_request_target)
147  {
148  for (auto& conn_id : m_connections)
149  conn_id = boost::uuids::nil_uuid();
150  }
151 
152  bool handle_new_connection(const boost::uuids::uuid& connection_id, bool ignore_close_fails = false)
153  {
154  size_t idx = m_next_opened_conn_idx.fetch_add(1, std::memory_order_relaxed);
155  if (idx >= m_connections.size())
156  {
157  LOG_PRINT_L0("ERROR: connections overflow");
158  exit(1);
159  }
160  m_connections[idx] = connection_id;
161 
162  size_t prev_connection_count = m_opened_connection_count.fetch_add(1, std::memory_order_relaxed);
163  if (m_max_opened_connection_count <= prev_connection_count)
164  {
165  return close_next_connection(ignore_close_fails);
166  }
167 
168  return true;
169  }
170 
172  {
173  while (close_next_connection(false));
174  }
175 
176  bool close_next_connection(bool ignore_close_fails)
177  {
178  size_t idx = m_next_closed_conn_idx.fetch_add(1, std::memory_order_relaxed);
179  if (m_next_opened_conn_idx.load(std::memory_order_relaxed) <= idx)
180  {
181  LOG_PRINT_L0("Not enough opened connections");
182  return false;
183  }
184  if (m_connections[idx].is_nil())
185  {
186  LOG_PRINT_L0("Connection isn't opened");
187  return false;
188  }
189  if (!m_tcp_server.get_config_object().close(m_connections[idx]))
190  {
191  LOG_PRINT_L0("Close connection error: " << m_connections[idx]);
192  if (!ignore_close_fails)
193  {
194  return false;
195  }
196  }
197 
198  m_connections[idx] = boost::uuids::nil_uuid();
199  m_opened_connection_count.fetch_sub(1, std::memory_order_relaxed);
200  return true;
201  }
202 
203  size_t opened_connection_count() const { return m_opened_connection_count.load(std::memory_order_relaxed); }
204 
205  private:
206  test_tcp_server& m_tcp_server;
207  size_t m_max_opened_connection_count;
208  std::atomic<size_t> m_opened_connection_count;
209  std::atomic<size_t> m_next_opened_conn_idx;
210  std::atomic<size_t> m_next_closed_conn_idx;
211  std::vector<boost::uuids::uuid> m_connections;
212  };
213 
214  const unsigned int min_thread_count = 2;
215  const std::string clt_port("36230");
216  const std::string srv_port("36231");
217 
219  {
227  };
228 
230  {
231  const static int ID = cmd_close_all_connections_id;
232 
233  struct request
234  {
237  };
238  };
239 
241  {
242  const static int ID = cmd_start_open_close_test_id;
243 
244  struct request
245  {
248 
253  };
254 
255  struct response
256  {
259  };
260  };
261 
263  {
264  const static int ID = cmd_get_statistics_id;
265 
266  struct request
267  {
270  };
271 
272  struct response
273  {
277 
279  KV_SERIALIZE(opened_connections_count)
280  KV_SERIALIZE(new_connection_counter)
281  KV_SERIALIZE(close_connection_counter)
283 
284  std::string to_string() const
285  {
286  std::stringstream ss;
287  ss << "opened_connections_count = " << opened_connections_count <<
288  ", new_connection_counter = " << new_connection_counter <<
289  ", close_connection_counter = " << close_connection_counter;
290  return ss.str();
291  }
292  };
293  };
294 
296  {
297  const static int ID = cmd_reset_statistics_id;
298 
299  struct request
300  {
303  };
304 
305  struct response
306  {
309  };
310  };
311 
313  {
314  const static int ID = cmd_shutdown_id;
315 
316  struct request
317  {
320  };
321  };
322 
324  {
325  const static int ID = cmd_send_data_requests_id;
326 
327  struct request
328  {
330 
332  KV_SERIALIZE(request_size)
334  };
335  };
336 
338  {
339  const static int ID = cmd_data_request_id;
340 
341  struct request
342  {
345 
347  KV_SERIALIZE(data)
349  };
350 
351  struct response
352  {
354 
356  KV_SERIALIZE(data)
358  };
359  };
360 }
epee::net_utils::connection< test_levin_protocol_handler > test_connection
bool close_next_connection(bool ignore_close_fails)
virtual int notify(int command, const epee::span< const uint8_t > in_buff, test_connection_context &context)
boost::uuids::uuid uuid
::std::string string
Definition: gtest-port.h:1097
const std::string clt_port("36230")
epee::net_utils::boosted_tcp_server< test_levin_protocol_handler > test_tcp_server
virtual int invoke(int command, const epee::span< const uint8_t > in_buff, std::string &buff_out, test_connection_context &context)
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
STL namespace.
Non-owning sequence of data. Does not deep copy.
Definition: span.h:56
#define KV_SERIALIZE(varialble)
unit_test::call_counter m_close_connection_counter
the connection templated-class for one peer connection
Represents a single connection from a client.
epee::levin::async_protocol_handler_config< test_connection_context > test_levin_protocol_handler_config
unit_test::call_counter m_new_connection_counter
unsigned __int64 uint64_t
Definition: stdint.h:136
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:98
virtual void callback(test_connection_context &context)
virtual void on_connection_new(test_connection_context &context)
epee::levin::async_protocol_handler< test_connection_context > test_levin_protocol_handler
bool handle_new_connection(const boost::uuids::uuid &connection_id, bool ignore_close_fails=false)
virtual void on_connection_close(test_connection_context &context)
const std::string srv_port("36231")
open_close_test_helper(test_tcp_server &tcp_server, size_t open_request_target, size_t max_opened_connection_count)
const unsigned int min_thread_count
std::string to_string(t_connection_type type)
#define END_KV_SERIALIZE_MAP()
#define LEVIN_OK
Definition: levin_base.h:93
#define BEGIN_KV_SERIALIZE_MAP()
size_t get() volatile const
t_protocol_handler::config_type & get_config_object()