Debian Codegraph @

tests/net_load_tests/srv.cpp

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 #include <boost/thread/mutex.hpp>
32 #include <boost/thread/thread.hpp>
33
34 #include "include_base_utils.h"
35 #include "misc_log_ex.h"
36 #include "storages/levin_abstract_invoke2.h"
37 #include "common/util.h"
38
39 #include "net_load_tests.h"
40
41 using namespace net_load_tests;
42
43 #define EXIT_ON_ERROR(cond) { if (!(cond)) { LOG_PRINT_L0("ERROR: " << #cond); exit(1); } else {} }
44
45 namespace
46 {
47 struct srv_levin_commands_handler : public test_levin_commands_handler
48 {
49 srv_levin_commands_handler(test_tcp_server& tcp_server)
50 : m_tcp_server(tcp_server)
51 , m_open_close_test_conn_id(boost::uuids::nil_uuid())
52 {
53 }
54
55 virtual void on_connection_new(test_connection_context& context)
56 {
57 test_levin_commands_handler::on_connection_new(context);
58 context.m_closed = false;
59
60 //std::this_thread::sleep_for(std::chrono::milliseconds(10));
61
62 boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
63 if (!m_open_close_test_conn_id.is_nil())
64 {
65 EXIT_ON_ERROR(m_open_close_test_helper->handle_new_connection(context.m_connection_id, true));
66 }
67 }
68
69 virtual void on_connection_close(test_connection_context& context)
70 {
71 test_levin_commands_handler::on_connection_close(context);
72
73 boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
74 if (context.m_connection_id == m_open_close_test_conn_id)
75 {
76 LOG_PRINT_L0("Stop open/close test");
77 m_open_close_test_conn_id = boost::uuids::nil_uuid();
78 m_open_close_test_helper.reset(0);
79 }
80 }
81
82 CHAIN_LEVIN_INVOKE_MAP2(test_connection_context);
83 CHAIN_LEVIN_NOTIFY_MAP2(test_connection_context);
84
85 BEGIN_INVOKE_MAP2(srv_levin_commands_handler)
86 HANDLE_NOTIFY_T2(CMD_CLOSE_ALL_CONNECTIONS, &srv_levin_commands_handler::handle_close_all_connections)
87 HANDLE_NOTIFY_T2(CMD_SHUTDOWN, &srv_levin_commands_handler::handle_shutdown)
88 HANDLE_NOTIFY_T2(CMD_SEND_DATA_REQUESTS, &srv_levin_commands_handler::handle_send_data_requests)
89 HANDLE_INVOKE_T2(CMD_GET_STATISTICS, &srv_levin_commands_handler::handle_get_statistics)
90 HANDLE_INVOKE_T2(CMD_RESET_STATISTICS, &srv_levin_commands_handler::handle_reset_statistics)
91 HANDLE_INVOKE_T2(CMD_START_OPEN_CLOSE_TEST, &srv_levin_commands_handler::handle_start_open_close_test)
92 END_INVOKE_MAP2()
93
94 int handle_close_all_connections(int command, const CMD_CLOSE_ALL_CONNECTIONS::request& req, test_connection_context& context)
95 {
96 close_connections(context.m_connection_id);
97 return 1;
98 }
99
100 int handle_get_statistics(int command, const CMD_GET_STATISTICS::request&, CMD_GET_STATISTICS::response& rsp, test_connection_context& /*context*/)
101 {
102 rsp.opened_connections_count = m_tcp_server.get_config_object().get_connections_count();
103 rsp.new_connection_counter = new_connection_counter();
104 rsp.close_connection_counter = close_connection_counter();
105 LOG_PRINT_L0("Statistics: " << rsp.to_string());
106 return 1;
107 }
108
109 int handle_reset_statistics(int command, const CMD_RESET_STATISTICS::request&, CMD_RESET_STATISTICS::response& /*rsp*/, test_connection_context& /*context*/)
110 {
111 m_new_connection_counter.reset();
112 m_new_connection_counter.inc();
113 m_close_connection_counter.reset();
114 return 1;
115 }
116
117 int handle_start_open_close_test(int command, const CMD_START_OPEN_CLOSE_TEST::request& req, CMD_START_OPEN_CLOSE_TEST::response&, test_connection_context& context)
118 {
119 boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
120 if (0 == m_open_close_test_helper.get())
121 {
122 LOG_PRINT_L0("Start open/close test (" << req.open_request_target << ", " << req.max_opened_conn_count << ")");
123
124 m_open_close_test_conn_id = context.m_connection_id;
125 m_open_close_test_helper.reset(new open_close_test_helper(m_tcp_server, req.open_request_target, req.max_opened_conn_count));
126 return 1;
127 }
128 else
129 {
130 return -1;
131 }
132 }
133
134 int handle_shutdown(int command, const CMD_SHUTDOWN::request& req, test_connection_context& /*context*/)
135 {
136 LOG_PRINT_L0("Got shutdown request. Shutting down...");
137 m_tcp_server.send_stop_signal();
138 return 1;
139 }
140
141 int handle_send_data_requests(int /*command*/, const CMD_SEND_DATA_REQUESTS::request& req, test_connection_context& context)
142 {
143 boost::uuids::uuid cmd_conn_id = context.m_connection_id;
144 m_tcp_server.get_config_object().foreach_connection([&](test_connection_context& ctx) {
145 if (ctx.m_connection_id != cmd_conn_id)
146 {
147 CMD_DATA_REQUEST::request req2;
148 req2.data.resize(req.request_size);
149
150 bool r = epee::net_utils::async_invoke_remote_command2<CMD_DATA_REQUEST::response>(ctx, CMD_DATA_REQUEST::ID, req2,
151 m_tcp_server.get_config_object(), [=](int code, const CMD_DATA_REQUEST::response& rsp, const test_connection_context&) {
152 if (code <= 0)
153 {
154 LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST. code = " << code);
155 }
156 });
157 if (!r)
158 LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST");
159 }
160 return true;
161 });
162
163 return 1;
164 }
165
166 private:
167 void close_connections(boost::uuids::uuid cmd_conn_id)
168 {
169 LOG_PRINT_L0("Closing connections. Number of opened connections: " << m_tcp_server.get_config_object().get_connections_count());
170
171 size_t count = 0;
172 m_tcp_server.get_config_object().foreach_connection([&](test_connection_context& ctx) {
173 if (ctx.m_connection_id != cmd_conn_id)
174 {
175 ++count;
176 if (!ctx.m_closed)
177 {
178 ctx.m_closed = true;
179 m_tcp_server.get_config_object().close(ctx.m_connection_id, true);
180 }
181 else
182 {
183 LOG_PRINT_L0(count << " connection already closed");
184 }
185 }
186 return true;
187 });
188
189 if (0 < count)
190 {
191 // Perhaps not all connections were closed, try to close it after 7 seconds
192 boost::shared_ptr<boost::asio::deadline_timer> sh_deadline(new boost::asio::deadline_timer(m_tcp_server.get_io_context(), boost::posix_time::seconds(7)));
193 sh_deadline->async_wait([=](const boost::system::error_code& ec)
194 {
195 boost::shared_ptr<boost::asio::deadline_timer> t = sh_deadline; // Capture sh_deadline
196 if (!ec)
197 {
198 close_connections(cmd_conn_id);
199 }
200 else
201 {
202 LOG_PRINT_L0("ERROR: " << ec.message() << ':' << ec.value());
203 }
204 });
205 }
206 }
207
208 private:
209 test_tcp_server& m_tcp_server;
210
211 boost::uuids::uuid m_open_close_test_conn_id;
212 boost::mutex m_open_close_test_mutex;
213 std::unique_ptr<open_close_test_helper> m_open_close_test_helper;
214 };
215 }
216
217 int main(int argc, char** argv)
218 {
219 TRY_ENTRY();
220 tools::on_startup();
221 //set up logging options
222 mlog_configure(mlog_get_default_log_path("net_load_tests_srv.log"), true);
223
224 size_t thread_count = (std::max)(min_thread_count, boost::thread::hardware_concurrency() / 2);
225
226 test_tcp_server tcp_server(epee::net_utils::e_connection_type_RPC);
227 if (!tcp_server.init_server(srv_port, "127.0.0.1"))
228 return 1;
229
230 srv_levin_commands_handler *commands_handler = new srv_levin_commands_handler(tcp_server);
231 tcp_server.get_config_object().set_handler(commands_handler, [](epee::levin::levin_commands_handler<test_connection_context> *handler) { delete handler; });
232 tcp_server.get_config_object().m_invoke_timeout = 10000;
233 //tcp_server.get_config_object().m_max_packet_size = max_packet_size;
234
235 if (!tcp_server.run_server(thread_count, true))
236 return 2;
237 return 0;
238 CATCH_ENTRY_L0("main", 1);
239 }