libcopp  1.1.0
sample_benchmark_task_stack_pool.cpp
Go to the documentation of this file.
1 /*
2  * sample_benchmark_task_stack_pool.cpp
3  *
4  * Created on: 2017年5月19日
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 #include <vector>
18 
19 // include manager header file
21 #include <libcotask/task.h>
22 
23 #ifdef LIBCOTASK_MACRO_ENABLED
24 
25 #if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
26 #include <chrono>
27 #define CALC_CLOCK_T std::chrono::system_clock::time_point
28 #define CALC_CLOCK_NOW() std::chrono::system_clock::now()
29 #define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
30 #define CALC_NS_AVG_CLOCK(x, y) static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
31 #else
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))
36 #endif
37 
38 // =============== 栈池对象 ===============
41 // --------------- 栈池对象 ---------------
42 
43 int switch_count = 100;
44 int max_task_number = 100000; // 协程Task数量
45 
46 struct my_macro_coroutine {
48 
50 };
51 
53 
54 std::vector<my_task_t::ptr_t> task_arr;
55 
56 // define a coroutine runner
57 int my_task_action(void *) {
58  // ... your code here ...
59  int count = switch_count; // 每个task地切换次数
60 
61  while (count-- > 0) {
63  }
64 
65  return 0;
66 }
67 
68 static void benchmark_round(int index) {
69  printf("### Round: %d ###\n", index);
70 
71  time_t begin_time = time(NULL);
72  CALC_CLOCK_T begin_clock = CALC_CLOCK_NOW();
73 
74  // create coroutines
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);
79  if (!new_task) {
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());
83  break;
84  } else {
85  task_arr.push_back(new_task);
86  }
87  }
88 
89  time_t end_time = time(NULL);
90  CALC_CLOCK_T end_clock = CALC_CLOCK_NOW();
91  printf("create %d task, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number, static_cast<int>(end_time - begin_time),
92  CALC_MS_CLOCK(end_clock - begin_clock), CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_task_number));
93 
94  begin_time = end_time;
95  begin_clock = end_clock;
96 
97  // start a task
98  for (int i = 0; i < max_task_number; ++i) {
99  task_arr[i]->start();
100  }
101 
102  // yield & resume from runner
103  bool continue_flag = true;
104  long long real_switch_times = static_cast<long long>(0);
105 
106  while (continue_flag) {
107  continue_flag = false;
108  for (int i = 0; i < max_task_number; ++i) {
109  if (false == task_arr[i]->is_completed()) {
110  continue_flag = true;
111  ++real_switch_times;
112  task_arr[i]->resume();
113  }
114  }
115  }
116 
117  end_time = time(NULL);
118  end_clock = CALC_CLOCK_NOW();
119  printf("switch %d tasks %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number, real_switch_times,
120  static_cast<int>(end_time - begin_time), CALC_MS_CLOCK(end_clock - begin_clock),
121  CALC_NS_AVG_CLOCK(end_clock - begin_clock, real_switch_times));
122 
123  begin_time = end_time;
124  begin_clock = end_clock;
125 
126  task_arr.clear();
127 
128  end_time = time(NULL);
129  end_clock = CALC_CLOCK_NOW();
130  printf("remove %d tasks, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number, static_cast<int>(end_time - begin_time),
131  CALC_MS_CLOCK(end_clock - begin_clock), CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_task_number));
132 }
133 
134 int main(int argc, char *argv[]) {
135  puts("###################### task (stack using stack pool) ###################");
136  printf("########## Cmd:");
137  for (int i = 0; i < argc; ++i) {
138  printf(" %s", argv[i]);
139  }
140  puts("");
141 
142  if (argc > 1) {
143  max_task_number = atoi(argv[1]);
144  }
145 
146  if (argc > 2) {
147  switch_count = atoi(argv[2]);
148  }
149 
150  size_t stack_size = 16 * 1024;
151  if (argc > 3) {
152  stack_size = static_cast<size_t>(atoi(argv[3]) * 1024);
153  }
154 
156  global_stack_pool->set_min_stack_number(static_cast<size_t>(max_task_number));
157  global_stack_pool->set_stack_size(stack_size);
158 
159  for (int i = 1; i <= 5; ++i) {
160  benchmark_round(i);
161  }
162  return 0;
163 }
164 #else
165 int main() {
166  puts("cotask disabled.");
167  return 0;
168 }
169 
170 #endif
#define CALC_MS_CLOCK(x)
static void benchmark_round(int index)
cotask::task my_task_t
copp::stack_pool< copp::allocator::default_statck_allocator > stack_pool_t
stack_pool_t::ptr_t global_stack_pool
static ptr_t create()
Definition: stack_pool.h:52
static ptr_t create(const Ty &functor, size_t stack_size=0, size_t private_buffer_size=0)
create task with functor
Definition: task.h:155
#define CALC_CLOCK_NOW()
coroutine container contain stack context, stack allocator and runtime fcontext
impl::task_impl * get_task() UTIL_CONFIG_NOEXCEPT
get current running task
Definition: this_task.cpp:15
#define CALC_CLOCK_T
memory allocator this allocator will maintain buffer using malloc/free function
virtual int yield(void **priv_data)=0
#define CALC_NS_AVG_CLOCK(x, y)
std::shared_ptr< stack_pool< TAlloc > > ptr_t
Definition: stack_pool.h:25