libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sample_benchmark_std_couroutine_callable_recursive.cpp
Go to the documentation of this file.
1// Copyright 2023 owent
2// std coroutine trivial callable benchmark
3
6
7#include <inttypes.h>
8#include <stdint.h>
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12#include <ctime>
13#include <vector>
14
15#if defined(LIBCOPP_MACRO_ENABLE_STD_COROUTINE) && LIBCOPP_MACRO_ENABLE_STD_COROUTINE
16
17# if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
18# include <chrono>
19# define CALC_CLOCK_T std::chrono::system_clock::time_point
20# define CALC_CLOCK_NOW() std::chrono::system_clock::now()
21# define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
22# define CALC_NS_AVG_CLOCK(x, y) \
23 static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
24# else
25# define CALC_CLOCK_T clock_t
26# define CALC_CLOCK_NOW() clock()
27# define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
28# define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
29# endif
30
31using benchmark_callable_future_type = copp::callable_future<int64_t>;
32using benchmark_generator_future_type = copp::generator_future<int64_t>;
33
34std::vector<std::unique_ptr<benchmark_callable_future_type>> g_benchmark_callable_list;
35std::vector<benchmark_generator_future_type> g_benchmark_generator_list;
36
37int recursive_count = 100;
38int max_task_number = 100000;
39
40benchmark_callable_future_type run_benchmark(size_t idx, int left_recursive_count,
41 benchmark_generator_future_type* first_hang) {
42 int64_t result = left_recursive_count;
43 if (nullptr != first_hang) {
44 auto gen_res = co_await *first_hang;
45 result += gen_res;
46 } else {
47 result += static_cast<int64_t>(idx);
48 }
49
50 if (left_recursive_count > 1) {
51 auto gen_res = co_await run_benchmark(idx, left_recursive_count - 1, nullptr);
52 result += gen_res;
53 }
54
55 co_return result;
56}
57
58static void benchmark_round(int index) {
59 g_benchmark_callable_list.reserve(static_cast<size_t>(max_task_number));
60 g_benchmark_generator_list.reserve(static_cast<size_t>(max_task_number));
61
62 printf("### Round: %d ###\n", index);
63
64 // create generators
65 for (int i = 0; i < max_task_number; ++i) {
66 g_benchmark_generator_list.emplace_back(
67 benchmark_generator_future_type([](benchmark_generator_future_type::context_pointer_type) {}));
68 }
69
70 time_t begin_time = time(nullptr);
71 CALC_CLOCK_T begin_clock = CALC_CLOCK_NOW();
72
73 // create coroutines callable
74 while (g_benchmark_callable_list.size() < static_cast<size_t>(max_task_number)) {
75 size_t idx = g_benchmark_callable_list.size();
76 g_benchmark_callable_list.push_back(std::unique_ptr<benchmark_callable_future_type>(
77 new benchmark_callable_future_type(run_benchmark(idx, recursive_count, &g_benchmark_generator_list[idx]))));
78 }
79
80 time_t end_time = time(nullptr);
81 CALC_CLOCK_T end_clock = CALC_CLOCK_NOW();
82 printf("create %d callable(s) and generator(s), cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
83 static_cast<int>(end_time - begin_time), CALC_MS_CLOCK(end_clock - begin_clock),
84 CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_task_number));
85
86 begin_time = end_time;
87 begin_clock = end_clock;
88
89 // yield & resume from runner
90 long long real_switch_times = static_cast<long long>(max_task_number) * recursive_count;
91
92 for (int i = 0; i < max_task_number; ++i) {
93 g_benchmark_generator_list[static_cast<size_t>(i)].get_context()->set_value(i);
94 }
95
96 end_time = time(nullptr);
97 end_clock = CALC_CLOCK_NOW();
98 printf("resume %d callable(s) and generator(s) for %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
99 max_task_number, real_switch_times, static_cast<int>(end_time - begin_time),
100 CALC_MS_CLOCK(end_clock - begin_clock), CALC_NS_AVG_CLOCK(end_clock - begin_clock, real_switch_times));
101
102 begin_time = end_time;
103 begin_clock = end_clock;
104
105 g_benchmark_callable_list.clear();
106
107 end_time = time(nullptr);
108 end_clock = CALC_CLOCK_NOW();
109 printf("remove %d callable(s), cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
110 static_cast<int>(end_time - begin_time), CALC_MS_CLOCK(end_clock - begin_clock),
111 CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_task_number));
112}
113
114int main(int argc, char* argv[]) {
115 puts("###################### std callable - recursive ###################");
116 printf("########## Cmd:");
117 for (int i = 0; i < argc; ++i) {
118 printf(" %s", argv[i]);
119 }
120 puts("");
121
122 if (argc > 1) {
123 max_task_number = atoi(argv[1]);
124 }
125
126 if (argc > 2) {
127 recursive_count = atoi(argv[2]);
128 }
129
130 for (int i = 1; i <= 5; ++i) {
132 }
133 return 0;
134}
135#else
136int main() {
137 puts("std coroutine is not supported by current compiler.");
138 return 0;
139}
140#endif
#define CALC_CLOCK_T
#define CALC_MS_CLOCK(x)
#define CALC_NS_AVG_CLOCK(x, y)
#define CALC_CLOCK_NOW()
static void benchmark_round(int index)