22 #ifdef LIBCOTASK_MACRO_ENABLED
24 # if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
26 # define CALC_CLOCK_T std::chrono::system_clock::time_point
27 # define CALC_CLOCK_NOW() std::chrono::system_clock::now()
28 # define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
29 # define CALC_NS_AVG_CLOCK(x, y) \
30 static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
32 # define CALC_CLOCK_T clock_t
33 # define CALC_CLOCK_NOW() clock()
34 # define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
35 # define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
39 typedef copp::stack_pool<copp::allocator::default_statck_allocator>
stack_pool_t;
44 int max_task_number = 100000;
46 struct my_macro_coroutine {
47 using stack_allocator_type = copp::allocator::stack_allocator_pool<stack_pool_t>;
48 using coroutine_type = copp::coroutine_context_container<stack_allocator_type>;
52 typedef cotask::task<my_macro_coroutine>
my_task_t;
54 std::vector<my_task_t::ptr_t> task_arr;
57 int my_task_action(
void *) {
69 printf(
"### Round: %d ###\n", index);
71 time_t begin_time = time(
nullptr);
75 task_arr.reserve(
static_cast<size_t>(max_task_number));
76 while (task_arr.size() <
static_cast<size_t>(max_task_number)) {
78 my_task_t::ptr_t new_task = my_task_t::create(my_task_action, alloc, 0);
80 fprintf(stderr,
"create coroutine task failed, real size is %d.\n",
static_cast<int>(task_arr.size()));
81 fprintf(stderr,
"maybe sysconf [vm.max_map_count] extended.\n");
82 max_task_number =
static_cast<int>(task_arr.size());
85 task_arr.push_back(new_task);
89 time_t end_time = time(
nullptr);
91 printf(
"create %d task, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
92 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
95 begin_time = end_time;
96 begin_clock = end_clock;
99 for (
int i = 0; i < max_task_number; ++i) {
100 task_arr[i]->start();
104 bool continue_flag =
true;
105 long long real_switch_times =
static_cast<long long>(0);
107 while (continue_flag) {
108 continue_flag =
false;
109 for (
int i = 0; i < max_task_number; ++i) {
110 if (
false == task_arr[i]->is_completed()) {
111 continue_flag =
true;
113 task_arr[i]->resume();
118 end_time = time(
nullptr);
120 printf(
"switch %d tasks %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
121 real_switch_times,
static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
124 begin_time = end_time;
125 begin_clock = end_clock;
129 end_time = time(
nullptr);
131 printf(
"remove %d tasks, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
132 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
136 int main(
int argc,
char *argv[]) {
137 puts(
"###################### task (stack using stack pool) ###################");
138 printf(
"########## Cmd:");
139 for (
int i = 0; i < argc; ++i) {
140 printf(
" %s", argv[i]);
145 max_task_number = atoi(argv[1]);
152 size_t stack_size = 16 * 1024;
154 stack_size =
static_cast<size_t>(atoi(argv[3]) * 1024);
161 for (
int i = 1; i <= 5; ++i) {
168 puts(
"cotask disabled.");
LIBCOPP_COTASK_API impl::task_impl * get_task() LIBCOPP_MACRO_NOEXCEPT
get current running task
std::shared_ptr< cli::cmd_option_value > value_type
#define CALC_NS_AVG_CLOCK(x, y)
static void benchmark_round(int index)
stack_pool_t::ptr_t global_stack_pool
copp::stack_pool< copp::allocator::default_statck_allocator > stack_pool_t