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

test_async_channel.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include <deque>
  6. #include <mutex>
  7. #include "librf/librf.h"
  8. using namespace librf;
  9. using namespace std::chrono;
  10. const size_t MAX_CHANNEL_QUEUE = 1; //0, 1, 5, 10, -1
  11. //如果使用move_only_type来操作channel失败,说明中间过程发生了拷贝操作----这不是设计目标。
  12. template<class _Ty>
  13. struct move_only_type
  14. {
  15. _Ty value;
  16. move_only_type() = default;
  17. explicit move_only_type(const _Ty& val) : value(val) {}
  18. explicit move_only_type(_Ty&& val) : value(std::forward<_Ty>(val)) {}
  19. move_only_type(const move_only_type&) = delete;
  20. move_only_type& operator =(const move_only_type&) = delete;
  21. move_only_type(move_only_type&&) = default;
  22. move_only_type& operator =(move_only_type&&) = default;
  23. };
  24. //如果channel缓存的元素不能凭空产生,或者产生代价较大,则推荐第二个模板参数使用true。从而减小不必要的开销。
  25. using string_channel_t = channel_t<move_only_type<std::string>>;
  26. //channel其实内部引用了一个channel实现体,故可以支持复制拷贝操作
  27. future_t<> test_channel_read(string_channel_t c)
  28. {
  29. using namespace std::chrono;
  30. for (size_t i = 0; i < 10; ++i)
  31. {
  32. try
  33. {
  34. //auto val = co_await c.read();
  35. auto val = co_await c; //第二种从channel读出数据的方法。利用重载operator co_await(),而不是c是一个awaitable_t。
  36. std::cout << val.value << ":";
  37. std::cout << std::endl;
  38. }
  39. catch (librf::channel_exception& e)
  40. {
  41. //MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
  42. std::cout << e.what() << std::endl;
  43. }
  44. co_await sleep_for(50ms);
  45. }
  46. }
  47. future_t<> test_channel_write(string_channel_t c)
  48. {
  49. using namespace std::chrono;
  50. for (size_t i = 0; i < 10; ++i)
  51. {
  52. //co_await c.write(std::to_string(i));
  53. co_await(c << std::to_string(i)); //第二种写入数据到channel的方法。因为优先级关系,需要将'c << i'括起来
  54. std::cout << "<" << i << ">:";
  55. std::cout << std::endl;
  56. }
  57. }
  58. void test_channel_read_first()
  59. {
  60. string_channel_t c(MAX_CHANNEL_QUEUE);
  61. go test_channel_read(c);
  62. go test_channel_write(c);
  63. this_scheduler()->run_until_notask();
  64. }
  65. void test_channel_write_first()
  66. {
  67. string_channel_t c(MAX_CHANNEL_QUEUE);
  68. go test_channel_write(c);
  69. go test_channel_read(c);
  70. this_scheduler()->run_until_notask();
  71. }
  72. static const int N = 1000000;
  73. void test_channel_performance_single_thread(size_t buff_size)
  74. {
  75. //1的话,效率跟golang比,有点惨不忍睹。
  76. //1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
  77. channel_t<int, false, true> c{ buff_size };
  78. go[&]() -> future_t<>
  79. {
  80. for (int i = N - 1; i >= 0; --i)
  81. {
  82. co_await(c << i);
  83. }
  84. };
  85. go[&]() -> future_t<>
  86. {
  87. auto tstart = high_resolution_clock::now();
  88. int i;
  89. do
  90. {
  91. i = co_await c;
  92. } while (i > 0);
  93. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  94. std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
  95. };
  96. this_scheduler()->run_until_notask();
  97. }
  98. void test_channel_performance_double_thread(size_t buff_size)
  99. {
  100. //1的话,效率跟golang比,有点惨不忍睹。
  101. //1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
  102. channel_t<int, false, true> c{ buff_size };
  103. std::thread wr_th([c]
  104. {
  105. local_scheduler_t ls;
  106. GO
  107. {
  108. for (int i = N - 1; i >= 0; --i)
  109. {
  110. co_await(c << i);
  111. }
  112. };
  113. this_scheduler()->run_until_notask();
  114. });
  115. go[&]() -> future_t<>
  116. {
  117. auto tstart = high_resolution_clock::now();
  118. int i;
  119. do
  120. {
  121. i = co_await c;
  122. } while (i > 0);
  123. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  124. std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
  125. };
  126. this_scheduler()->run_until_notask();
  127. wr_th.join();
  128. }
  129. void test_channel_performance_four_thread(size_t buff_size)
  130. {
  131. //1的话,效率跟golang比,有点惨不忍睹。
  132. //1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
  133. channel_t<int, false, true> c{ buff_size };
  134. std::thread wr_th[4];
  135. std::thread rd_th[4];
  136. for (int i = 0; i < 4; ++i)
  137. {
  138. wr_th[i] = std::thread([c]
  139. {
  140. local_scheduler_t ls;
  141. GO
  142. {
  143. for (int i = N - 1; i >= 0; --i)
  144. {
  145. co_await(c << i);
  146. }
  147. };
  148. this_scheduler()->run_until_notask();
  149. });
  150. }
  151. for (int i = 0; i < 4; ++i)
  152. {
  153. rd_th[i] = std::thread([c]
  154. {
  155. local_scheduler_t ls;
  156. auto tstart = high_resolution_clock::now();
  157. GO
  158. {
  159. for (int i = N - 1; i >= 0; --i)
  160. {
  161. co_await c;
  162. }
  163. };
  164. this_scheduler()->run_until_notask();
  165. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  166. std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
  167. });
  168. }
  169. for (int i = 0; i < 4; ++i)
  170. rd_th[i].join();
  171. for (int i = 0; i < 4; ++i)
  172. wr_th[i].join();
  173. }
  174. void test_channel_performance_four_coroutine(size_t capacity, size_t nThreads, int n)
  175. {
  176. auto tstart = high_resolution_clock::now();
  177. channel_t<bool> q{ nThreads };
  178. channel_t<bool> c{ capacity };
  179. for (size_t i = 0; i < nThreads; ++i)
  180. {
  181. GO
  182. {
  183. for (int i = 0; i < n; ++i)
  184. co_await(c << true);
  185. co_await(q << true);
  186. };
  187. GO
  188. {
  189. for (int i = 0; i < n; ++i)
  190. co_await c;
  191. co_await(q << true);
  192. };
  193. }
  194. GO
  195. {
  196. for (size_t i = 0; i < nThreads * 2; ++i)
  197. {
  198. co_await q;
  199. }
  200. };
  201. this_scheduler()->run_until_notask();
  202. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  203. std::cout << "channel buff=" << c.capacity() << ", w/r " << n << " times, cost time " << dt << "s" << std::endl;
  204. }
  205. void resumable_main_channel()
  206. {
  207. //test_channel_read_first();
  208. //std::cout << std::endl;
  209. //test_channel_write_first();
  210. //std::cout << std::endl;
  211. /*
  212. std::cout << "single thread" << std::endl;
  213. test_channel_performance_single_thread(1);
  214. test_channel_performance_single_thread(10);
  215. test_channel_performance_single_thread(100);
  216. test_channel_performance_single_thread(1000);
  217. std::cout << "double thread" << std::endl;
  218. test_channel_performance_double_thread(1);
  219. test_channel_performance_double_thread(10);
  220. test_channel_performance_double_thread(100);
  221. test_channel_performance_double_thread(1000);
  222. */
  223. std::cout << "four thread" << std::endl;
  224. //test_channel_performance_four_thread(1);
  225. //test_channel_performance_four_thread(1000);
  226. std::cout << "four coroutine" << std::endl;
  227. test_channel_performance_four_coroutine(1000, 4, N);
  228. }
  229. #if LIBRF_TUTORIAL_STAND_ALONE
  230. int main()
  231. {
  232. resumable_main_channel();
  233. return 0;
  234. }
  235. #endif