2020-03-26 17:35:12 +08:00
|
|
|
|
#include <chrono>
|
2017-09-24 14:01:30 +08:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <deque>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
#include "librf/librf.h"
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
using namespace librf;
|
2020-03-02 14:50:01 +08:00
|
|
|
|
using namespace std::chrono;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2020-03-09 17:36:36 +08:00
|
|
|
|
const size_t MAX_CHANNEL_QUEUE = 1; //0, 1, 5, 10, -1
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2020-03-09 15:31:30 +08:00
|
|
|
|
//如果使用move_only_type来操作channel失败,说明中间过程发生了拷贝操作----这不是设计目标。
|
|
|
|
|
template<class _Ty>
|
|
|
|
|
struct move_only_type
|
|
|
|
|
{
|
|
|
|
|
_Ty value;
|
|
|
|
|
|
|
|
|
|
move_only_type() = default;
|
2020-03-29 08:35:42 +08:00
|
|
|
|
explicit move_only_type(const _Ty& val) : value(val) {}
|
|
|
|
|
explicit move_only_type(_Ty&& val) : value(std::forward<_Ty>(val)) {}
|
2020-03-09 15:31:30 +08:00
|
|
|
|
|
|
|
|
|
move_only_type(const move_only_type&) = delete;
|
|
|
|
|
move_only_type& operator =(const move_only_type&) = delete;
|
|
|
|
|
|
|
|
|
|
move_only_type(move_only_type&&) = default;
|
|
|
|
|
move_only_type& operator =(move_only_type&&) = default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//如果channel缓存的元素不能凭空产生,或者产生代价较大,则推荐第二个模板参数使用true。从而减小不必要的开销。
|
2020-03-31 21:52:44 +08:00
|
|
|
|
using string_channel_t = channel_t<move_only_type<std::string>>;
|
2020-03-09 15:31:30 +08:00
|
|
|
|
|
|
|
|
|
//channel其实内部引用了一个channel实现体,故可以支持复制拷贝操作
|
|
|
|
|
future_t<> test_channel_read(string_channel_t c)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 10; ++i)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-09 08:17:52 +08:00
|
|
|
|
//auto val = co_await c.read();
|
|
|
|
|
auto val = co_await c; //第二种从channel读出数据的方法。利用重载operator co_await(),而不是c是一个awaitable_t。
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2020-03-09 15:31:30 +08:00
|
|
|
|
std::cout << val.value << ":";
|
2017-09-24 14:01:30 +08:00
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
}
|
2021-11-01 17:59:08 +08:00
|
|
|
|
catch (librf::channel_exception& e)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
//MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
|
|
|
|
|
std::cout << e.what() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
co_await sleep_for(50ms);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 15:31:30 +08:00
|
|
|
|
future_t<> test_channel_write(string_channel_t c)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 10; ++i)
|
|
|
|
|
{
|
2020-03-09 08:17:52 +08:00
|
|
|
|
//co_await c.write(std::to_string(i));
|
2020-03-09 15:31:30 +08:00
|
|
|
|
co_await(c << std::to_string(i)); //第二种写入数据到channel的方法。因为优先级关系,需要将'c << i'括起来
|
2017-09-24 14:01:30 +08:00
|
|
|
|
std::cout << "<" << i << ">:";
|
|
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_channel_read_first()
|
|
|
|
|
{
|
2020-03-09 15:31:30 +08:00
|
|
|
|
string_channel_t c(MAX_CHANNEL_QUEUE);
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
go test_channel_read(c);
|
|
|
|
|
go test_channel_write(c);
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_channel_write_first()
|
|
|
|
|
{
|
2020-03-09 15:31:30 +08:00
|
|
|
|
string_channel_t c(MAX_CHANNEL_QUEUE);
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
go test_channel_write(c);
|
|
|
|
|
go test_channel_read(c);
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 14:50:01 +08:00
|
|
|
|
static const int N = 1000000;
|
|
|
|
|
|
2020-03-09 17:36:36 +08:00
|
|
|
|
void test_channel_performance_single_thread(size_t buff_size)
|
2020-03-02 14:50:01 +08:00
|
|
|
|
{
|
2020-03-09 08:17:52 +08:00
|
|
|
|
//1的话,效率跟golang比,有点惨不忍睹。
|
|
|
|
|
//1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
|
2020-03-09 16:45:04 +08:00
|
|
|
|
channel_t<int, false, true> c{ buff_size };
|
2020-03-02 14:50:01 +08:00
|
|
|
|
|
|
|
|
|
go[&]() -> future_t<>
|
|
|
|
|
{
|
|
|
|
|
for (int i = N - 1; i >= 0; --i)
|
|
|
|
|
{
|
|
|
|
|
co_await(c << i);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
go[&]() -> future_t<>
|
|
|
|
|
{
|
|
|
|
|
auto tstart = high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
i = co_await c;
|
|
|
|
|
} while (i > 0);
|
|
|
|
|
|
|
|
|
|
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
|
2020-03-09 08:17:52 +08:00
|
|
|
|
std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
|
2020-03-02 14:50:01 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this_scheduler()->run_until_notask();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 17:36:36 +08:00
|
|
|
|
void test_channel_performance_double_thread(size_t buff_size)
|
|
|
|
|
{
|
|
|
|
|
//1的话,效率跟golang比,有点惨不忍睹。
|
|
|
|
|
//1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
|
|
|
|
|
channel_t<int, false, true> c{ buff_size };
|
|
|
|
|
|
|
|
|
|
std::thread wr_th([c]
|
|
|
|
|
{
|
2020-03-31 15:30:45 +08:00
|
|
|
|
local_scheduler_t ls;
|
2020-03-09 17:36:36 +08:00
|
|
|
|
|
|
|
|
|
GO
|
|
|
|
|
{
|
|
|
|
|
for (int i = N - 1; i >= 0; --i)
|
|
|
|
|
{
|
|
|
|
|
co_await(c << i);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this_scheduler()->run_until_notask();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
go[&]() -> future_t<>
|
|
|
|
|
{
|
|
|
|
|
auto tstart = high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
i = co_await c;
|
|
|
|
|
} while (i > 0);
|
|
|
|
|
|
|
|
|
|
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
|
|
|
|
|
std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this_scheduler()->run_until_notask();
|
|
|
|
|
|
|
|
|
|
wr_th.join();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-02 11:02:58 +08:00
|
|
|
|
void test_channel_performance_four_thread(size_t buff_size)
|
|
|
|
|
{
|
2021-12-06 08:57:44 +08:00
|
|
|
|
auto tstart = high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
channel_t<bool> q{ 8 };
|
2021-12-02 11:02:58 +08:00
|
|
|
|
channel_t<int, false, true> c{ buff_size };
|
|
|
|
|
|
|
|
|
|
std::thread wr_th[4];
|
|
|
|
|
std::thread rd_th[4];
|
2021-12-06 08:57:44 +08:00
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
|
wr_th[i] = std::thread([c, q] {
|
|
|
|
|
for (int i = N - 1; i >= 0; --i)
|
|
|
|
|
(void)(c << i);
|
|
|
|
|
(void)(q << true);
|
2021-12-02 11:02:58 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 08:57:44 +08:00
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
|
rd_th[i] = std::thread([c, q] {
|
|
|
|
|
for (int i = N - 1; i >= 0; --i)
|
|
|
|
|
(void)c.read();
|
|
|
|
|
(void)(q << true);
|
2021-12-02 11:02:58 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 08:57:44 +08:00
|
|
|
|
GO {
|
|
|
|
|
for (size_t i = 0; i < 8; ++i)
|
|
|
|
|
co_await q;
|
|
|
|
|
};
|
|
|
|
|
this_scheduler()->run_until_notask();
|
|
|
|
|
|
|
|
|
|
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
|
|
|
|
|
std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
|
|
|
|
|
|
2021-12-02 11:02:58 +08:00
|
|
|
|
for (int i = 0; i < 4; ++i)
|
2021-12-06 08:57:44 +08:00
|
|
|
|
{
|
2021-12-02 11:02:58 +08:00
|
|
|
|
wr_th[i].join();
|
2021-12-06 08:57:44 +08:00
|
|
|
|
rd_th[i].join();
|
|
|
|
|
}
|
2021-12-02 11:02:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_channel_performance_four_coroutine(size_t capacity, size_t nThreads, int n)
|
|
|
|
|
{
|
|
|
|
|
auto tstart = high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
channel_t<bool> q{ nThreads };
|
|
|
|
|
channel_t<bool> c{ capacity };
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < nThreads; ++i)
|
|
|
|
|
{
|
|
|
|
|
GO
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
co_await(c << true);
|
|
|
|
|
co_await(q << true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GO
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
co_await c;
|
|
|
|
|
co_await(q << true);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GO
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < nThreads * 2; ++i)
|
|
|
|
|
{
|
|
|
|
|
co_await q;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this_scheduler()->run_until_notask();
|
|
|
|
|
|
|
|
|
|
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
|
|
|
|
|
std::cout << "channel buff=" << c.capacity() << ", w/r " << n << " times, cost time " << dt << "s" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-24 14:01:30 +08:00
|
|
|
|
void resumable_main_channel()
|
|
|
|
|
{
|
2021-12-09 11:25:24 +08:00
|
|
|
|
test_channel_read_first();
|
|
|
|
|
std::cout << std::endl;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2021-12-09 11:25:24 +08:00
|
|
|
|
test_channel_write_first();
|
|
|
|
|
std::cout << std::endl;
|
2020-03-02 14:50:01 +08:00
|
|
|
|
|
2021-12-02 11:02:58 +08:00
|
|
|
|
std::cout << "single thread" << std::endl;
|
2020-03-09 17:36:36 +08:00
|
|
|
|
test_channel_performance_single_thread(1);
|
|
|
|
|
test_channel_performance_single_thread(10);
|
|
|
|
|
test_channel_performance_single_thread(100);
|
|
|
|
|
test_channel_performance_single_thread(1000);
|
|
|
|
|
|
2021-12-02 11:02:58 +08:00
|
|
|
|
std::cout << "double thread" << std::endl;
|
2020-03-09 17:36:36 +08:00
|
|
|
|
test_channel_performance_double_thread(1);
|
|
|
|
|
test_channel_performance_double_thread(10);
|
|
|
|
|
test_channel_performance_double_thread(100);
|
|
|
|
|
test_channel_performance_double_thread(1000);
|
2021-12-02 11:02:58 +08:00
|
|
|
|
|
|
|
|
|
std::cout << "four thread" << std::endl;
|
2021-12-09 11:25:24 +08:00
|
|
|
|
test_channel_performance_four_thread(1);
|
2021-12-06 08:57:44 +08:00
|
|
|
|
test_channel_performance_four_thread(1000);
|
2021-12-02 11:02:58 +08:00
|
|
|
|
|
|
|
|
|
std::cout << "four coroutine" << std::endl;
|
2021-12-09 11:25:24 +08:00
|
|
|
|
test_channel_performance_four_coroutine(1, 4, N);
|
2021-12-02 11:02:58 +08:00
|
|
|
|
test_channel_performance_four_coroutine(1000, 4, N);
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
2020-09-23 22:56:51 +08:00
|
|
|
|
|
2021-11-02 17:15:17 +08:00
|
|
|
|
#if LIBRF_TUTORIAL_STAND_ALONE
|
2020-09-23 22:56:51 +08:00
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
resumable_main_channel();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-11-02 17:15:17 +08:00
|
|
|
|
#endif
|