libcopp  2.2.0
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 #include <inttypes.h>
11 #include <stdint.h>
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring>
15 #include <ctime>
16 
17 // include manager header file
19 
20 #if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
21 # include <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))
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 *self = copp::this_coroutine::get_coroutine();
41  while (count-- > 0) {
42  self->yield();
43  }
44 
45  return 1;
46 }
47 
48 int max_coroutine_number = 100000; // 协程数量
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]) ###################");
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(nullptr);
78  CALC_CLOCK_T begin_clock = CALC_CLOCK_NOW();
79 
80  // create coroutines
81  co_arr = new copp::coroutine_context_default::ptr_t[max_coroutine_number];
82 
83  time_t end_time = time(nullptr);
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");
97  break;
98  }
99  }
100 
101  end_time = time(nullptr);
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(nullptr);
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",
133  max_coroutine_number, real_switch_times, static_cast<int>(end_time - begin_time),
134  CALC_MS_CLOCK(end_clock - begin_clock), 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(nullptr);
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 }
LIBCOPP_COPP_API coroutine_context * get_coroutine() LIBCOPP_MACRO_NOEXCEPT
get current coroutine
#define CALC_CLOCK_T
int main(int argc, char *argv[])
int max_coroutine_number
static int my_runner(void *)
copp::coroutine_context_default::ptr_t * co_arr
#define CALC_MS_CLOCK(x)
#define CALC_NS_AVG_CLOCK(x, y)
#define CALC_CLOCK_NOW()