libcopp  1.2.1
sample_benchmark_coroutine.cpp
Go to the documentation of this file.
1 /*
2  * sample_stress_test_coroutine.cpp
3  *
4  * Created on: 2014年5月11日
5  * Author: owent
6  *
7  * Released under the MIT license
8  */
9 
10 
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 #include <ctime>
15 #include <inttypes.h>
16 #include <stdint.h>
17 
18 // include manager header file
20 
21 #if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
22 #include <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) static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
27 #else
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))
32 #endif
33 
34 int switch_count = 100;
35 
36 // define a coroutine runner
37 static int my_runner(void *) {
38  // ... your code here ...
39  int count = switch_count; // 每个协程N次切换
40  copp::coroutine_context_default *addr = copp::this_coroutine::get<copp::coroutine_context_default>();
41 
42  while (count-- > 0)
43  addr->yield();
44 
45  return 1;
46 }
47 
48 int max_coroutine_number = 100000; // 协程数量
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]) ###################");
55 #else
56  puts("###################### context coroutine (stack using default allocator ###################");
57 #endif
58  printf("########## Cmd:");
59  for (int i = 0; i < argc; ++i) {
60  printf(" %s", argv[i]);
61  }
62  puts("");
63 
64  if (argc > 1) {
65  max_coroutine_number = atoi(argv[1]);
66  }
67 
68  if (argc > 2) {
69  switch_count = atoi(argv[2]);
70  }
71 
72  size_t stack_size = 16 * 1024;
73  if (argc > 3) {
74  stack_size = atoi(argv[3]) * 1024;
75  }
76 
77  time_t begin_time = time(NULL);
78  CALC_CLOCK_T begin_clock = CALC_CLOCK_NOW();
79 
80  // create coroutines
82 
83  time_t end_time = time(NULL);
84  CALC_CLOCK_T end_clock = CALC_CLOCK_NOW();
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),
87  CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_coroutine_number));
88 
89  // create a runner
90  // bind runner to coroutine object
91  for (int i = 0; i < max_coroutine_number; ++i) {
92  co_arr[i] = copp::coroutine_context_default::create(my_runner, stack_size);
93  if (!co_arr[i]) {
94  fprintf(stderr, "coroutine create failed, the real number is %d\n", i);
95  fprintf(stderr, "maybe sysconf [vm.max_map_count] extended?\n");
96  max_coroutine_number = i;
97  break;
98  }
99  }
100 
101  end_time = time(NULL);
102  end_clock = CALC_CLOCK_NOW();
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),
105  CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_coroutine_number));
106 
107  begin_time = end_time;
108  begin_clock = end_clock;
109 
110  // start a coroutine
111  for (int i = 0; i < max_coroutine_number; ++i) {
112  co_arr[i]->start();
113  }
114 
115  // yield & resume from runner
116  bool continue_flag = true;
117  long long real_switch_times = static_cast<long long>(0);
118 
119  while (continue_flag) {
120  continue_flag = false;
121  for (int i = 0; i < max_coroutine_number; ++i) {
122  if (false == co_arr[i]->is_finished()) {
123  continue_flag = true;
124  ++real_switch_times;
125  co_arr[i]->resume();
126  }
127  }
128  }
129 
130  end_time = time(NULL);
131  end_clock = CALC_CLOCK_NOW();
132  printf("switch %d coroutine contest %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_coroutine_number,
133  real_switch_times, static_cast<int>(end_time - begin_time), CALC_MS_CLOCK(end_clock - begin_clock),
134  CALC_NS_AVG_CLOCK(end_clock - begin_clock, real_switch_times));
135 
136  begin_time = end_time;
137  begin_clock = end_clock;
138 
139  delete[] co_arr;
140 
141  end_time = time(NULL);
142  end_clock = CALC_CLOCK_NOW();
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),
145  CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_coroutine_number));
146 
147  return 0;
148 }
#define CALC_MS_CLOCK(x)
int max_coroutine_number
static int my_runner(void *)
static ptr_t create(const callback_t &runner, allocator_type &alloc, size_t stack_sz=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
create and init coroutine with specify runner and specify stack size
int main(int argc, char *argv[])
copp::coroutine_context_default::ptr_t * co_arr
#define CALC_CLOCK_NOW()
int yield(void **priv_data=UTIL_CONFIG_NULLPTR)
yield coroutine
coroutine container contain stack context, stack allocator and runtime fcontext
#define CALC_CLOCK_T
#define CALC_NS_AVG_CLOCK(x, y)