libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sample_benchmark_coroutine_malloc.cpp
Go to the documentation of this file.
1/*
2 * sample_stress_test_coroutine_malloc.cpp
3 *
4 * Created on: 2014年5月15日
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
34int switch_count = 100;
35
36typedef copp::coroutine_context_container<copp::allocator::stack_allocator_malloc> my_cotoutine_t;
37
38// define a coroutine runner
39static int my_runner(void *) {
40 // ... your code here ...
41 int count = switch_count; // 每个协程N次切换
42 copp::coroutine_context *self = copp::this_coroutine::get_coroutine();
43 while (count-- > 0) {
44 self->yield();
45 }
46
47 return 1;
48}
49
50int MAX_COROUTINE_NUMBER = 100000; // 协程数量
51my_cotoutine_t::ptr_t *co_arr = nullptr;
52
53int main(int argc, char *argv[]) {
54 puts("###################### context coroutine (stack using malloc/free) ###################");
55 printf("########## Cmd:");
56 for (int i = 0; i < argc; ++i) {
57 printf(" %s", argv[i]);
58 }
59 puts("");
60
61 if (argc > 1) {
62 MAX_COROUTINE_NUMBER = atoi(argv[1]);
63 }
64
65 if (argc > 2) {
66 switch_count = atoi(argv[2]);
67 }
68
69 size_t stack_size = 16 * 1024;
70 if (argc > 3) {
71 stack_size = static_cast<size_t>(atoi(argv[3]) * 1024);
72 }
73 if (stack_size < copp::stack_traits::minimum_size()) {
74 stack_size = copp::stack_traits::minimum_size();
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 my_cotoutine_t::ptr_t[static_cast<size_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] = my_cotoutine_t::create(my_runner, stack_size);
93 }
94
95 end_time = time(nullptr);
96 end_clock = CALC_CLOCK_NOW();
97 printf("create %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n", MAX_COROUTINE_NUMBER,
98 static_cast<int>(end_time - begin_time), CALC_MS_CLOCK(end_clock - begin_clock),
99 CALC_NS_AVG_CLOCK(end_clock - begin_clock, MAX_COROUTINE_NUMBER));
100
101 begin_time = end_time;
102 begin_clock = end_clock;
103
104 // start a coroutine
105 for (int i = 0; i < MAX_COROUTINE_NUMBER; ++i) {
106 co_arr[i]->start();
107 }
108
109 // yield & resume from runner
110 bool continue_flag = true;
111 long long real_switch_times = static_cast<long long>(0);
112
113 while (continue_flag) {
114 continue_flag = false;
115 for (int i = 0; i < MAX_COROUTINE_NUMBER; ++i) {
116 if (false == co_arr[i]->is_finished()) {
117 continue_flag = true;
118 ++real_switch_times;
119 co_arr[i]->resume();
120 }
121 }
122 }
123
124 end_time = time(nullptr);
125 end_clock = CALC_CLOCK_NOW();
126 printf("switch %d coroutine contest %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
127 MAX_COROUTINE_NUMBER, real_switch_times, static_cast<int>(end_time - begin_time),
128 CALC_MS_CLOCK(end_clock - begin_clock), CALC_NS_AVG_CLOCK(end_clock - begin_clock, real_switch_times));
129
130 begin_time = end_time;
131 begin_clock = end_clock;
132
133 delete[] co_arr;
134
135 end_time = time(nullptr);
136 end_clock = CALC_CLOCK_NOW();
137 printf("remove %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n", MAX_COROUTINE_NUMBER,
138 static_cast<int>(end_time - begin_time), CALC_MS_CLOCK(end_clock - begin_clock),
139 CALC_NS_AVG_CLOCK(end_clock - begin_clock, MAX_COROUTINE_NUMBER));
140
141 return 0;
142}
int main()
my_cotoutine_t::ptr_t * co_arr
#define CALC_NS_AVG_CLOCK(x, y)
copp::coroutine_context_container< copp::allocator::stack_allocator_malloc > my_cotoutine_t