基于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.2KB

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