libcopp  1.1.0
sample_benchmark_task_malloc.cpp
Go to the documentation of this file.
1 /*
2  * sample_stress_test_task_malloc.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 #include <vector>
18 
19 // include manager header file
20 #include <libcotask/task.h>
21 
22 #ifdef LIBCOTASK_MACRO_ENABLED
23 
24 #if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
25 #include <chrono>
26 #define CALC_CLOCK_T std::chrono::system_clock::time_point
27 #define CALC_CLOCK_NOW() std::chrono::system_clock::now()
28 #define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
29 #define CALC_NS_AVG_CLOCK(x, y) static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
30 #else
31 #define CALC_CLOCK_T clock_t
32 #define CALC_CLOCK_NOW() clock()
33 #define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
34 #define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
35 #endif
36 
37 int switch_count = 100;
38 int max_task_number = 100000; // 协程Task数量
39 size_t stack_size = 16 * 1024;
40 
41 struct my_macro_coroutine {
42  typedef copp::allocator::stack_allocator_malloc stack_allocator_t;
43 
45 };
46 
48 
49 std::vector<my_task_t::ptr_t> task_arr;
50 
51 // define a coroutine runner
52 int my_task_action(void *) {
53  // ... your code here ...
54  int count = switch_count; // 每个task地切换次数
55 
56  while (count-- > 0) {
58  }
59 
60  return 0;
61 }
62 
63 int main(int argc, char *argv[]) {
64  puts("###################### task (stack using malloc/free) ###################");
65  printf("########## Cmd:");
66  for (int i = 0; i < argc; ++i) {
67  printf(" %s", argv[i]);
68  }
69  puts("");
70 
71  if (argc > 1) {
72  max_task_number = atoi(argv[1]);
73  }
74 
75  if (argc > 2) {
76  switch_count = atoi(argv[2]);
77  }
78 
79  if (argc > 3) {
80  stack_size = atoi(argv[3]) * 1024;
81  }
82 
83  time_t begin_time = time(NULL);
84  CALC_CLOCK_T begin_clock = CALC_CLOCK_NOW();
85 
86  // create coroutines
87  task_arr.reserve(static_cast<size_t>(max_task_number));
88  while (task_arr.size() < static_cast<size_t>(max_task_number)) {
89  task_arr.push_back(my_task_t::create(my_task_action, stack_size));
90  }
91 
92  time_t end_time = time(NULL);
93  CALC_CLOCK_T end_clock = CALC_CLOCK_NOW();
94  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),
95  CALC_MS_CLOCK(end_clock - begin_clock), CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_task_number));
96 
97  begin_time = end_time;
98  begin_clock = end_clock;
99 
100  // start a task
101  for (int i = 0; i < max_task_number; ++i) {
102  task_arr[i]->start();
103  }
104 
105  // yield & resume from runner
106  bool continue_flag = true;
107  long long real_switch_times = static_cast<long long>(0);
108 
109  while (continue_flag) {
110  continue_flag = false;
111  for (int i = 0; i < max_task_number; ++i) {
112  if (false == task_arr[i]->is_completed()) {
113  continue_flag = true;
114  ++real_switch_times;
115  task_arr[i]->resume();
116  }
117  }
118  }
119 
120  end_time = time(NULL);
121  end_clock = CALC_CLOCK_NOW();
122  printf("switch %d tasks %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number, real_switch_times,
123  static_cast<int>(end_time - begin_time), CALC_MS_CLOCK(end_clock - begin_clock),
124  CALC_NS_AVG_CLOCK(end_clock - begin_clock, real_switch_times));
125 
126  begin_time = end_time;
127  begin_clock = end_clock;
128 
129  task_arr.clear();
130 
131  end_time = time(NULL);
132  end_clock = CALC_CLOCK_NOW();
133  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),
134  CALC_MS_CLOCK(end_clock - begin_clock), CALC_NS_AVG_CLOCK(end_clock - begin_clock, max_task_number));
135 
136  return 0;
137 }
138 #else
139 int main() {
140  puts("cotask disabled.");
141  return 0;
142 }
143 
144 #endif
#define CALC_MS_CLOCK(x)
cotask::task my_task_t
memory allocator this allocator will maintain buffer using malloc/free function
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
virtual int yield(void **priv_data)=0
#define CALC_NS_AVG_CLOCK(x, y)