基于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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include <deque>
  6. #include <mutex>
  7. #include "librf.h"
  8. using namespace resumef;
  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>, false, true>;
  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. #ifndef __clang__
  33. try
  34. #endif
  35. {
  36. //auto val = co_await c.read();
  37. auto val = co_await c; //第二种从channel读出数据的方法。利用重载operator co_await(),而不是c是一个awaitable_t。
  38. std::cout << val.value << ":";
  39. std::cout << std::endl;
  40. }
  41. #ifndef __clang__
  42. catch (resumef::channel_exception& e)
  43. {
  44. //MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
  45. std::cout << e.what() << std::endl;
  46. }
  47. #endif
  48. co_await sleep_for(50ms);
  49. }
  50. }
  51. future_t<> test_channel_write(string_channel_t c)
  52. {
  53. using namespace std::chrono;
  54. for (size_t i = 0; i < 10; ++i)
  55. {
  56. //co_await c.write(std::to_string(i));
  57. co_await(c << std::to_string(i)); //第二种写入数据到channel的方法。因为优先级关系,需要将'c << i'括起来
  58. std::cout << "<" << i << ">:";
  59. std::cout << std::endl;
  60. }
  61. }
  62. void test_channel_read_first()
  63. {
  64. string_channel_t c(MAX_CHANNEL_QUEUE);
  65. go test_channel_read(c);
  66. go test_channel_write(c);
  67. this_scheduler()->run_until_notask();
  68. }
  69. void test_channel_write_first()
  70. {
  71. string_channel_t c(MAX_CHANNEL_QUEUE);
  72. go test_channel_write(c);
  73. go test_channel_read(c);
  74. this_scheduler()->run_until_notask();
  75. }
  76. static const int N = 1000000;
  77. void test_channel_performance_single_thread(size_t buff_size)
  78. {
  79. //1的话,效率跟golang比,有点惨不忍睹。
  80. //1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
  81. channel_t<int, false, true> c{ buff_size };
  82. go[&]() -> future_t<>
  83. {
  84. for (int i = N - 1; i >= 0; --i)
  85. {
  86. co_await(c << i);
  87. }
  88. };
  89. go[&]() -> future_t<>
  90. {
  91. auto tstart = high_resolution_clock::now();
  92. int i;
  93. do
  94. {
  95. i = co_await c;
  96. } while (i > 0);
  97. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  98. std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
  99. };
  100. this_scheduler()->run_until_notask();
  101. }
  102. void test_channel_performance_double_thread(size_t buff_size)
  103. {
  104. //1的话,效率跟golang比,有点惨不忍睹。
  105. //1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
  106. channel_t<int, false, true> c{ buff_size };
  107. std::thread wr_th([c]
  108. {
  109. local_scheduler_t ls;
  110. GO
  111. {
  112. for (int i = N - 1; i >= 0; --i)
  113. {
  114. co_await(c << i);
  115. }
  116. };
  117. this_scheduler()->run_until_notask();
  118. });
  119. go[&]() -> future_t<>
  120. {
  121. auto tstart = high_resolution_clock::now();
  122. int i;
  123. do
  124. {
  125. i = co_await c;
  126. } while (i > 0);
  127. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  128. std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
  129. };
  130. this_scheduler()->run_until_notask();
  131. wr_th.join();
  132. }
  133. void resumable_main_channel()
  134. {
  135. test_channel_read_first();
  136. std::cout << std::endl;
  137. test_channel_write_first();
  138. std::cout << std::endl;
  139. test_channel_performance_single_thread(1);
  140. test_channel_performance_single_thread(10);
  141. test_channel_performance_single_thread(100);
  142. test_channel_performance_single_thread(1000);
  143. test_channel_performance_double_thread(1);
  144. test_channel_performance_double_thread(10);
  145. test_channel_performance_double_thread(100);
  146. test_channel_performance_double_thread(1000);
  147. }