基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

benchmark_asio_echo.cpp 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include <SDKDDKVer.h>
  2. /*
  3. #include <chrono>
  4. #include <iostream>
  5. #include <string>
  6. #include <conio.h>
  7. #include <thread>
  8. */
  9. #include "librf.h"
  10. #include <asio.hpp>
  11. #include "src/asio_task.h"
  12. #pragma warning(disable : 4834)
  13. using namespace asio;
  14. using namespace asio::ip;
  15. using namespace resumef;
  16. template<class _Ty, size_t _Size>
  17. union uarray
  18. {
  19. std::array<_Ty, _Size> c;
  20. template<class... Args>
  21. uarray(Args&... args)
  22. {
  23. for (auto & v : c)
  24. new(&v) _Ty(args...);
  25. }
  26. ~uarray()
  27. {
  28. for (auto & v : c)
  29. v.~_Ty();
  30. }
  31. };
  32. #define BUF_SIZE 1024
  33. std::atomic<intptr_t> g_echo_count = 0;
  34. future_t<> RunEchoSession(tcp::socket socket)
  35. {
  36. std::size_t bytes_transferred = 0;
  37. std::array<char, BUF_SIZE> buffer;
  38. for(;;)
  39. {
  40. #ifndef __clang__
  41. try
  42. #endif
  43. {
  44. bytes_transferred += co_await socket.async_read_some(asio::buffer(buffer.data() + bytes_transferred, buffer.size() - bytes_transferred), rf_task);
  45. if (bytes_transferred >= buffer.size())
  46. {
  47. co_await asio::async_write(socket, asio::buffer(buffer, buffer.size()), rf_task);
  48. bytes_transferred = 0;
  49. g_echo_count.fetch_add(1, std::memory_order_release);
  50. }
  51. }
  52. #ifndef __clang__
  53. catch (std::exception & e)
  54. {
  55. std::cerr << e.what() << std::endl;
  56. break;
  57. }
  58. #endif
  59. }
  60. }
  61. template<size_t _N>
  62. void AcceptConnections(tcp::acceptor & acceptor, uarray<tcp::socket, _N> & socketes)
  63. {
  64. try
  65. {
  66. for (size_t idx = 0; idx < socketes.c.size(); ++idx)
  67. {
  68. go[&, idx]() -> future_t<>
  69. {
  70. for (;;)
  71. {
  72. #ifndef __clang__
  73. try
  74. #endif
  75. {
  76. co_await acceptor.async_accept(socketes.c[idx], rf_task);
  77. go RunEchoSession(std::move(socketes.c[idx]));
  78. }
  79. #ifndef __clang__
  80. catch (std::exception & e)
  81. {
  82. std::cerr << e.what() << std::endl;
  83. }
  84. #endif
  85. }
  86. };
  87. }
  88. }
  89. catch (std::exception & e)
  90. {
  91. std::cerr << e.what() << std::endl;
  92. }
  93. }
  94. void StartPrintEchoCount()
  95. {
  96. using namespace std::literals;
  97. GO
  98. {
  99. for (;;)
  100. {
  101. g_echo_count.exchange(0, std::memory_order_release);
  102. co_await 1s;
  103. std::cout << g_echo_count.load(std::memory_order_acquire) << std::endl;
  104. }
  105. };
  106. }
  107. void RunOneBenchmark(bool bMain)
  108. {
  109. resumef::local_scheduler ls;
  110. asio::io_service io_service;
  111. tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 3456));
  112. uarray<tcp::socket, 16> socketes(io_service);
  113. AcceptConnections(acceptor, socketes);
  114. if (bMain) StartPrintEchoCount();
  115. for (;;)
  116. {
  117. io_service.poll();
  118. this_scheduler()->run_one_batch();
  119. }
  120. }
  121. void resumable_main_benchmark_asio_server()
  122. {
  123. #if RESUMEF_ENABLE_MULT_SCHEDULER
  124. std::array<std::thread, 2> thds;
  125. for (size_t i = 0; i < thds.size(); ++i)
  126. {
  127. thds[i] = std::thread(&RunOneBenchmark, i == 0);
  128. }
  129. for (auto & t : thds)
  130. t.join();
  131. #else
  132. RunOneBenchmark(true);
  133. #endif
  134. }
  135. //----------------------------------------------------------------------------------------------------------------------
  136. future_t<> RunPipelineEchoClient(asio::io_service & ios, tcp::resolver::iterator ep)
  137. {
  138. std::shared_ptr<tcp::socket> sptr = std::make_shared<tcp::socket>(ios);
  139. #ifndef __clang__
  140. try
  141. #endif
  142. {
  143. co_await asio::async_connect(*sptr, ep, rf_task);
  144. GO
  145. {
  146. std::array<char, BUF_SIZE> write_buff_;
  147. for (auto & c : write_buff_)
  148. c = 'A' + rand() % 52;
  149. #ifndef __clang__
  150. try
  151. #endif
  152. {
  153. for (;;)
  154. {
  155. co_await asio::async_write(*sptr, asio::buffer(write_buff_), rf_task);
  156. }
  157. }
  158. #ifndef __clang__
  159. catch (std::exception & e)
  160. {
  161. std::cerr << e.what() << std::endl;
  162. }
  163. #endif
  164. };
  165. GO
  166. {
  167. #ifndef __clang__
  168. try
  169. #endif
  170. {
  171. std::array<char, BUF_SIZE> read_buff_;
  172. for (;;)
  173. {
  174. co_await sptr->async_read_some(asio::buffer(read_buff_), rf_task);
  175. }
  176. }
  177. #ifndef __clang__
  178. catch (std::exception & e)
  179. {
  180. std::cerr << e.what() << std::endl;
  181. }
  182. #endif
  183. };
  184. }
  185. #ifndef __clang__
  186. catch (std::exception & e)
  187. {
  188. std::cerr << e.what() << std::endl;
  189. }
  190. #endif
  191. }
  192. #if _HAS_CXX17
  193. future_t<> RunPingPongEchoClient(asio::io_service & ios, tcp::resolver::iterator ep)
  194. {
  195. tcp::socket socket_{ ios };
  196. std::array<char, BUF_SIZE> read_buff_;
  197. std::array<char, BUF_SIZE> write_buff_;
  198. #ifndef __clang__
  199. try
  200. #endif
  201. {
  202. co_await asio::async_connect(socket_, ep, rf_task);
  203. for (auto & c : write_buff_)
  204. c = 'A' + rand() % 52;
  205. for (;;)
  206. {
  207. co_await when_all(
  208. asio::async_write(socket_, asio::buffer(write_buff_), rf_task),
  209. socket_.async_read_some(asio::buffer(read_buff_), rf_task)
  210. );
  211. }
  212. }
  213. #ifndef __clang__
  214. catch (std::exception & e)
  215. {
  216. std::cerr << e.what() << std::endl;
  217. }
  218. #endif
  219. }
  220. void resumable_main_benchmark_asio_client_with_rf(intptr_t nNum)
  221. {
  222. nNum = std::max((intptr_t)1, nNum);
  223. try
  224. {
  225. asio::io_service ios;
  226. asio::ip::tcp::resolver resolver_(ios);
  227. asio::ip::tcp::resolver::query query_("localhost", "3456");
  228. tcp::resolver::iterator iter = resolver_.resolve(query_);
  229. for (intptr_t i = 0; i < nNum; ++i)
  230. {
  231. go RunPingPongEchoClient(ios, iter);
  232. }
  233. for (;;)
  234. {
  235. ios.poll();
  236. this_scheduler()->run_one_batch();
  237. }
  238. }
  239. catch (std::exception & e)
  240. {
  241. std::cout << e.what() << std::endl;
  242. }
  243. }
  244. #endif
  245. class chat_session : public std::enable_shared_from_this<chat_session>
  246. {
  247. public:
  248. chat_session(asio::io_service & ios, tcp::resolver::iterator ep)
  249. : socket_(ios)
  250. , endpoint_(ep)
  251. {
  252. }
  253. void start()
  254. {
  255. do_connect();
  256. }
  257. private:
  258. void do_connect()
  259. {
  260. auto self = this->shared_from_this();
  261. asio::async_connect(socket_, endpoint_,
  262. [this, self](std::error_code ec, tcp::resolver::iterator )
  263. {
  264. if (!ec)
  265. {
  266. for (auto & c : write_buff_)
  267. c = 'A' + rand() % 52;
  268. do_write();
  269. }
  270. else
  271. {
  272. std::cerr << ec.message() << std::endl;
  273. }
  274. });
  275. }
  276. void do_read()
  277. {
  278. auto self(shared_from_this());
  279. socket_.async_read_some(asio::buffer(read_buff_),
  280. [this, self](const asio::error_code& ec, std::size_t )
  281. {
  282. if (!ec)
  283. {
  284. do_write();
  285. }
  286. else
  287. {
  288. std::cerr << ec.message() << std::endl;
  289. }
  290. });
  291. }
  292. void do_write()
  293. {
  294. auto self(shared_from_this());
  295. asio::async_write(socket_,
  296. asio::buffer(write_buff_),
  297. [this, self](std::error_code ec, std::size_t)
  298. {
  299. if (!ec)
  300. {
  301. do_read();
  302. }
  303. else
  304. {
  305. std::cerr << ec.message() << std::endl;
  306. }
  307. });
  308. }
  309. tcp::socket socket_;
  310. tcp::resolver::iterator endpoint_;
  311. std::array<char, BUF_SIZE> read_buff_;
  312. std::array<char, BUF_SIZE> write_buff_;
  313. };
  314. void resumable_main_benchmark_asio_client_with_callback(intptr_t nNum)
  315. {
  316. nNum = std::max((intptr_t)1, nNum);
  317. try
  318. {
  319. asio::io_service ios;
  320. asio::ip::tcp::resolver resolver_(ios);
  321. asio::ip::tcp::resolver::query query_("127.0.0.1", "3456");
  322. tcp::resolver::iterator iter = resolver_.resolve(query_);
  323. for (intptr_t i = 0; i < nNum; ++i)
  324. {
  325. auto chat = std::make_shared<chat_session>(ios, iter);
  326. chat->start();
  327. }
  328. ios.run();
  329. }
  330. catch (std::exception & e)
  331. {
  332. std::cout << "Exception: " << e.what() << "\n";
  333. }
  334. }
  335. void resumable_main_benchmark_asio_client(intptr_t nNum)
  336. {
  337. resumable_main_benchmark_asio_client_with_callback(nNum);
  338. }