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))
49 copp::coroutine_context_default::ptr_t *
co_arr =
nullptr;
50 int main(
int argc,
char *argv[]) {
51 #ifdef LIBCOPP_MACRO_SYS_POSIX
52 puts(
"###################### context coroutine (stack using default allocator[mmap]) ###################");
53 #elif defined(LIBCOPP_MACRO_SYS_WIN)
54 puts(
"###################### context coroutine (stack using default allocator[VirtualAlloc]) ###################");
56 puts(
"###################### context coroutine (stack using default allocator ###################");
58 printf(
"########## Cmd:");
59 for (
int i = 0; i < argc; ++i) {
60 printf(
" %s", argv[i]);
72 size_t stack_size = 16 * 1024;
74 stack_size = atoi(argv[3]) * 1024;
77 time_t begin_time = time(
nullptr);
83 time_t end_time = time(
nullptr);
85 printf(
"allocate %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
max_coroutine_number,
86 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
92 co_arr[i] = copp::coroutine_context_default::create(
my_runner, stack_size);
94 fprintf(stderr,
"coroutine create failed, the real number is %d\n", i);
95 fprintf(stderr,
"maybe sysconf [vm.max_map_count] extended?\n");
101 end_time = time(
nullptr);
103 printf(
"create %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
max_coroutine_number,
104 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
107 begin_time = end_time;
108 begin_clock = end_clock;
116 bool continue_flag =
true;
117 long long real_switch_times =
static_cast<long long>(0);
119 while (continue_flag) {
120 continue_flag =
false;
122 if (
false ==
co_arr[i]->is_finished()) {
123 continue_flag =
true;
130 end_time = time(
nullptr);
132 printf(
"switch %d coroutine contest %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
136 begin_time = end_time;
137 begin_clock = end_clock;
141 end_time = time(
nullptr);
143 printf(
"remove %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
max_coroutine_number,
144 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
LIBCOPP_COPP_API coroutine_context * get_coroutine() LIBCOPP_MACRO_NOEXCEPT
get current coroutine
int main(int argc, char *argv[])
static int my_runner(void *)
copp::coroutine_context_default::ptr_t * co_arr
#define CALC_NS_AVG_CLOCK(x, y)