16 #if defined(LIBCOPP_MACRO_ENABLE_STD_COROUTINE) && LIBCOPP_MACRO_ENABLE_STD_COROUTINE
18 # ifdef LIBCOTASK_MACRO_ENABLED
20 # if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
22 # define CALC_CLOCK_T std::chrono::system_clock::time_point
23 # define CALC_CLOCK_NOW() std::chrono::system_clock::now()
24 # define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
25 # define CALC_NS_AVG_CLOCK(x, y) \
26 static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
28 # define CALC_CLOCK_T clock_t
29 # define CALC_CLOCK_NOW() clock()
30 # define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
31 # define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
34 using benchmark_task_future_type = cotask::task_future<int64_t, void>;
35 using benchmark_generator_future_type = copp::generator_future<int64_t>;
37 std::vector<benchmark_task_future_type> g_benchmark_task_list;
38 std::vector<benchmark_generator_future_type::context_pointer_type> g_benchmark_generator_list;
41 int max_task_number = 100000;
43 benchmark_task_future_type run_benchmark(
size_t idx,
int left_switch_count) {
46 while (left_switch_count-- >= 0) {
48 co_await benchmark_generator_future_type([idx](benchmark_generator_future_type::context_pointer_type ctx) {
49 g_benchmark_generator_list[idx] = std::move(ctx);
58 g_benchmark_task_list.reserve(
static_cast<size_t>(max_task_number));
59 g_benchmark_generator_list.resize(
static_cast<size_t>(max_task_number),
nullptr);
61 printf(
"### Round: %d ###\n", index);
63 time_t begin_time = time(
nullptr);
67 while (g_benchmark_task_list.size() <
static_cast<size_t>(max_task_number)) {
68 g_benchmark_task_list.push_back(run_benchmark(g_benchmark_task_list.size(),
switch_count));
70 for (
auto& task_inst : g_benchmark_task_list) {
74 time_t end_time = time(
nullptr);
76 printf(
"create %d task(s) and generator(s), cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
77 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
80 begin_time = end_time;
81 begin_clock = end_clock;
84 bool continue_flag =
true;
85 long long real_switch_times =
static_cast<long long>(0);
88 while (continue_flag) {
90 continue_flag =
false;
91 for (
auto& generator_context : g_benchmark_generator_list) {
92 benchmark_generator_future_type::context_pointer_type move_context;
93 move_context.swap(generator_context);
95 move_context->set_value(round);
102 end_time = time(
nullptr);
104 printf(
"resume %d task(s) and generator(s) for %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
105 max_task_number, real_switch_times,
static_cast<int>(end_time - begin_time),
108 begin_time = end_time;
109 begin_clock = end_clock;
111 g_benchmark_task_list.clear();
112 g_benchmark_generator_list.clear();
114 end_time = time(
nullptr);
116 printf(
"remove %d task(s), cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
117 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
121 int main(
int argc,
char* argv[]) {
122 puts(
"###################### std task - create generator - trivial ###################");
123 printf(
"########## Cmd:");
124 for (
int i = 0; i < argc; ++i) {
125 printf(
" %s", argv[i]);
130 max_task_number = atoi(argv[1]);
137 for (
int i = 1; i <= 5; ++i) {
144 puts(
"task_future disabled.");
151 puts(
"std coroutine is not supported by current compiler.");
#define CALC_NS_AVG_CLOCK(x, y)
static void benchmark_round(int index)