1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-02 00:00:11 +08:00
librf/tutorial/test_async_resumable.cpp

79 lines
1.8 KiB
C++
Raw Normal View History

2020-03-26 17:26:39 +08:00
2017-09-24 14:01:30 +08:00
#include <chrono>
#include <iostream>
#include <string>
#include "librf.h"
2018-09-18 13:43:50 +08:00
static std::mutex lock_console;
2017-09-24 14:01:30 +08:00
template <typename T>
2019-10-09 10:08:31 +08:00
void dump(size_t idx, std::string name, T start, T end, intptr_t count)
2017-09-24 14:01:30 +08:00
{
2018-09-18 13:43:50 +08:00
lock_console.lock();
2017-09-24 14:01:30 +08:00
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
2018-09-18 13:43:50 +08:00
std::cout << idx << ":" << name << " ";
2019-10-09 10:08:31 +08:00
std::cout << count << " " << ns << " ns ";
std::cout << (ns / count) << " ns/op" << " ";
std::cout << (count * 100ll * 1000ll / ns) << "w/cps" << std::endl;
2018-09-18 13:43:50 +08:00
lock_console.unlock();
2017-09-24 14:01:30 +08:00
}
2019-10-09 10:08:31 +08:00
static const intptr_t N = 3000000;
//static const int N = 10;
2020-02-18 11:32:20 +08:00
auto yield_switch(intptr_t coro) -> resumef::generator_t<intptr_t>
2017-09-24 14:01:30 +08:00
{
2020-03-09 18:13:10 +08:00
for (intptr_t i = N / coro; i > 0; --i)
2017-09-24 14:01:30 +08:00
co_yield i;
2020-03-09 18:13:10 +08:00
co_return 0;
2017-09-24 14:01:30 +08:00
}
2018-09-18 13:43:50 +08:00
void resumable_switch(intptr_t coro, size_t idx)
2017-09-24 14:01:30 +08:00
{
resumef::local_scheduler_t ls;
2018-09-18 13:43:50 +08:00
2017-09-24 14:01:30 +08:00
auto start = std::chrono::steady_clock::now();
2018-09-18 13:43:50 +08:00
for (intptr_t i = 0; i < coro; ++i)
2017-09-24 14:01:30 +08:00
{
2020-03-09 18:13:10 +08:00
go yield_switch(coro);
2017-09-24 14:01:30 +08:00
}
2019-10-09 10:08:31 +08:00
auto middle = std::chrono::steady_clock::now();
dump(idx, "BenchmarkCreate_" + std::to_string(coro), start, middle, coro);
2017-10-01 10:33:08 +08:00
resumef::this_scheduler()->run_until_notask();
2017-09-24 14:01:30 +08:00
auto end = std::chrono::steady_clock::now();
2019-10-09 10:08:31 +08:00
dump(idx, "BenchmarkSwitch_" + std::to_string(coro), middle, end, N);
2017-09-24 14:01:30 +08:00
}
void resumable_main_resumable()
{
std::cout << __FUNCTION__ << std::endl;
2020-03-10 21:31:05 +08:00
resumable_switch(1, 99);
2019-10-09 10:08:31 +08:00
resumable_switch(1, 0);
2020-03-01 13:17:04 +08:00
resumable_switch(10, 0);
resumable_switch(100, 0);
2019-10-09 10:08:31 +08:00
resumable_switch(1000, 0);
2020-03-01 13:17:04 +08:00
resumable_switch(10000, 0);
2019-10-09 10:08:31 +08:00
resumable_switch(30000, 0);
2018-08-08 21:01:25 +08:00
2019-10-09 10:08:31 +08:00
/*
2018-09-18 13:43:50 +08:00
std::thread works[32];
for (size_t w = 1; w <= std::size(works); ++w)
2018-09-18 13:43:50 +08:00
{
for (size_t idx = 0; idx < w; ++idx)
works[idx] = std::thread(&resumable_switch, 1000, idx);
for (size_t idx = 0; idx < w; ++idx)
works[idx].join();
std::cout << std::endl << std::endl;
}
2019-10-09 10:08:31 +08:00
*/
2017-09-24 14:01:30 +08:00
}