2021-11-01 17:59:08 +08:00
|
|
|
|
|
2017-09-24 14:01:30 +08:00
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
#include "librf/librf.h"
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
|
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();
|
2020-09-23 23:58:13 +08:00
|
|
|
|
|
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;
|
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
auto yield_switch(intptr_t coro) -> librf::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
|
|
|
|
{
|
2021-11-01 17:59:08 +08:00
|
|
|
|
librf::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);
|
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
librf::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()
|
|
|
|
|
{
|
2020-05-26 11:52:53 +08:00
|
|
|
|
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];
|
2020-03-31 10:55:43 +08:00
|
|
|
|
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
|
|
|
|
}
|
2020-09-23 22:56:51 +08:00
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
resumable_main_resumable();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|