1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-01 15:57:07 +08:00
librf/tutorial/test_async_channel.cpp

146 lines
3.6 KiB
C++
Raw Normal View History

2017-09-24 14:01:30 +08:00

#include <chrono>
#include <iostream>
#include <string>
#include <conio.h>
#include <thread>
#include <deque>
#include <mutex>
#include "librf.h"
using namespace resumef;
using namespace std::chrono;
2017-09-24 14:01:30 +08:00
const size_t MAX_CHANNEL_QUEUE = 5; //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;
move_only_type(const _Ty& val) : value(val) {}
move_only_type(_Ty&& val) : value(std::forward<_Ty>(val)) {}
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-09 16:45:04 +08:00
using string_channel_t = channel_t<move_only_type<std::string>, false, true>;
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)
{
#ifndef __clang__
2017-09-24 14:01:30 +08:00
try
#endif
2017-09-24 14:01:30 +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;
}
#ifndef __clang__
catch (resumef::channel_exception& e)
2017-09-24 14:01:30 +08:00
{
//MAX_CHANNEL_QUEUE=0,并且先读后写会触发read_before_write异常
std::cout << e.what() << std::endl;
}
#endif
2017-09-24 14:01:30 +08:00
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)
{
//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
}
static const int N = 1000000;
void test_channel_performance(size_t buff_size)
{
//1的话效率跟golang比有点惨不忍睹。
//1000的话由于几乎不需要调度器接入效率就很高了随便过千万数量级。
2020-03-09 16:45:04 +08:00
channel_t<int, false, true> c{ buff_size };
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();
std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
};
this_scheduler()->run_until_notask();
}
2017-09-24 14:01:30 +08:00
void resumable_main_channel()
{
test_channel_read_first();
std::cout << std::endl;
test_channel_write_first();
std::cout << std::endl;
test_channel_performance(1);
test_channel_performance(10);
test_channel_performance(100);
test_channel_performance(1000);
test_channel_performance(10000);
2017-09-24 14:01:30 +08:00
}