21 #if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
23 # define CALC_CLOCK_T std::chrono::system_clock::time_point
24 # define CALC_CLOCK_NOW() std::chrono::system_clock::now()
25 # define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
26 # define CALC_NS_AVG_CLOCK(x, y) \
27 static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
29 # define CALC_CLOCK_T clock_t
30 # define CALC_CLOCK_NOW() clock()
31 # define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
32 # define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
36 typedef copp::stack_pool<copp::allocator::default_statck_allocator>
stack_pool_t;
40 typedef copp::coroutine_context_container<copp::allocator::stack_allocator_pool<stack_pool_t> >
my_cotoutine_t;
57 printf(
"### Round: %d ###\n", index);
59 time_t begin_time = time(
nullptr);
65 time_t end_time = time(
nullptr);
67 printf(
"allocate %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
MAX_COROUTINE_NUMBER,
68 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
75 fprintf(stderr,
"coroutine create failed, the real number is %d\n", i);
76 fprintf(stderr,
"maybe sysconf [vm.max_map_count] extended?\n");
82 end_time = time(
nullptr);
84 printf(
"create %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
MAX_COROUTINE_NUMBER,
85 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
88 begin_time = end_time;
89 begin_clock = end_clock;
97 bool continue_flag =
true;
98 long long real_switch_times =
static_cast<long long>(0);
100 while (continue_flag) {
101 continue_flag =
false;
103 if (0 ==
co_arr[i]->resume()) {
104 continue_flag =
true;
110 end_time = time(
nullptr);
112 printf(
"switch %d coroutine contest %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
116 begin_time = end_time;
117 begin_clock = end_clock;
121 end_time = time(
nullptr);
123 printf(
"remove %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
MAX_COROUTINE_NUMBER,
124 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
128 int main(
int argc,
char *argv[]) {
129 puts(
"###################### context coroutine (stack using stack pool) ###################");
130 printf(
"########## Cmd:");
131 for (
int i = 0; i < argc; ++i) {
132 printf(
" %s", argv[i]);
144 size_t stack_size = 16 * 1024;
146 stack_size =
static_cast<size_t>(atoi(argv[3]) * 1024);
153 for (
int i = 1; i <= 5; ++i) {
LIBCOPP_COPP_API coroutine_context * get_coroutine() LIBCOPP_MACRO_NOEXCEPT
get current coroutine
copp::coroutine_context_default::ptr_t * co_arr
int main(int argc, char *argv[])
static void benchmark_round(int index)
stack_pool_t::ptr_t global_stack_pool
static int my_runner(void *)
copp::stack_pool< copp::allocator::default_statck_allocator > stack_pool_t
#define CALC_NS_AVG_CLOCK(x, y)
copp::coroutine_context_container< copp::allocator::stack_allocator_pool< stack_pool_t > > my_cotoutine_t