-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
558 lines (462 loc) · 17.5 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#include <algorithm>
#include <chrono>
#include <exception>
#include <iostream>
#include <vector>
#include <boost/context/continuation.hpp>
#include <boost/fiber/all.hpp>
#include "coro.h"
#include <pth.h>
size_t num_fibers_tests[] = {1, 2, 10, 100, 1000, 10000, 100000};
const size_t STACK_SIZE = 16 * 1024;
boost::context::continuation coro_warmup(boost::context::continuation&& cont)
{
cont = cont.resume();
return std::move(cont);
}
static void warmup()
{
std::cout << "------------------------------------------" << std::endl;
boost::context::continuation ctx;
auto before = std::chrono::high_resolution_clock::now();
auto after = before;
ctx = boost::context::callcc(coro_warmup);
ctx = ctx.resume();
after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
std::cout << "Warmup time: " << duration.count() << "us" << std::endl;
}
static void inc_yield(size_t& v, bool &run)
{
while (run)
{
v++;
boost::this_fiber::yield();
}
}
static void test_yield()
{
std::cout << "------------------------------------------" << std::endl;
for (auto num_fibers : num_fibers_tests)
{
std::cout << "------------------------------------------" << std::endl;
std::cout << "Testing yield for " << num_fibers << " boost fibers" << std::endl;
std::vector<boost::fibers::fiber> fibers(num_fibers);
std::vector<size_t> values(num_fibers, 0);
bool run = true;
for (size_t i = 0; i < num_fibers; i++)
fibers[i] = boost::fibers::fiber(inc_yield, std::ref(values[i]), std::ref(run));
auto before = std::chrono::high_resolution_clock::now();
boost::this_fiber::sleep_for(std::chrono::seconds(1));
run = false;
for (size_t i = 0; i < num_fibers; i++)
fibers[i].join();
auto after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
size_t total = std::accumulate(values.begin(), values.end(), 0);
size_t min_value = *std::min_element(values.begin(), values.end());
size_t max_value = *std::max_element(values.begin(), values.end());
std::cout << "Total ops per second: " << total / duration.count() * 1000000
<< " (" << (total / duration.count()) << " Mrps)" << std::endl;
std::cout << "Ops per fiber: min: " << min_value
<< " max: " << max_value << std::endl;
}
}
static void inc(size_t& v)
{
v++;
}
static void create_inc_yield(size_t& v, bool &run)
{
while (run)
{
boost::fibers::fiber f(inc, std::ref(v));
f.join();
boost::this_fiber::yield();
}
}
static void test_create()
{
std::cout << "------------------------------------------" << std::endl;
for (auto num_fibers : num_fibers_tests)
{
std::cout << "------------------------------------------" << std::endl;
std::cout << "Testing create+yield for " << num_fibers << " boost fibers" << std::endl;
std::vector<boost::fibers::fiber> fibers(num_fibers);
std::vector<size_t> values(num_fibers, 0);
bool run = true;
auto before = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < num_fibers; i++)
fibers[i] = boost::fibers::fiber(create_inc_yield, std::ref(values[i]), std::ref(run));
boost::this_fiber::sleep_for(std::chrono::seconds(1));
run = false;
for (size_t i = 0; i < num_fibers; i++)
fibers[i].join();
auto after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
size_t total = std::accumulate(values.begin(), values.end(), 0);
size_t min_value = *std::min_element(values.begin(), values.end());
size_t max_value = *std::max_element(values.begin(), values.end());
std::cout << "Total ops per second: " << total / duration.count() * 1000000
<< " (" << (total / duration.count() * 1000) << " krps)" << std::endl;
std::cout << "Ops per fiber: min: " << min_value
<< " max: " << max_value << std::endl;
}
}
struct TestExc : public std::exception
{
TestExc() { std::cout << "TestExc ctor" << std::endl; }
~TestExc() { std::cout << "TestExc dtor" << std::endl; }
TestExc(const TestExc&) { std::cout << "TestExc copy ctor" << std::endl; }
TestExc(TestExc&&) { std::cout << "TestExc move ctor" << std::endl; }
void operator=(const TestExc&) { std::cout << "TestExc assign" << std::endl; }
void operator=(TestExc&&) { std::cout << "TestExc move assign" << std::endl; }
};
static void throwing(std::exception_ptr &ptr)
{
try {
throw TestExc();
}
catch(...)
{
ptr = std::current_exception();
}
}
static void test_exception()
{
std::cout << "------------------------------------------" << std::endl;
std::cout << "------------------------------------------" << std::endl;
std::cout << "Test exceptions" << std::endl;
std::exception_ptr eptr;
boost::fibers::fiber f(throwing, std::ref(eptr));
f.join();
if (eptr)
{
try {
rethrow_exception(eptr);
} catch (TestExc&)
{
std::cout << "Exception catched" << std::endl;
}
}
}
static void suspended_fiber(boost::fibers::promise<void> &promise_wait,
boost::fibers::promise<void> &promise_wakeup,
size_t &v, bool &run)
{
while (run)
{
promise_wait.get_future().wait();
boost::fibers::promise<void>().swap(promise_wait);
v++;
promise_wakeup.set_value();
}
}
static void test_suspend()
{
std::cout << "------------------------------------------" << std::endl;
for (auto num_fibers : num_fibers_tests)
{
if (num_fibers == 1)
continue;
std::cout << "------------------------------------------" << std::endl;
std::cout << "Testing suspend for " << num_fibers << " boost fibers" << std::endl;
std::vector<boost::fibers::fiber> fibers(num_fibers);
std::vector<boost::fibers::promise<void>> promises(num_fibers);
size_t total = 0;
bool run = true;
for (size_t i = 0; i < num_fibers; i++)
fibers[i] = boost::fibers::fiber(suspended_fiber,
std::ref(promises[i]),
std::ref(promises[(i + 1) % num_fibers]),
std::ref(total), std::ref(run));
auto before = std::chrono::high_resolution_clock::now();
promises[0].set_value();
boost::this_fiber::sleep_for(std::chrono::seconds(1));
run = false;
for (size_t i = 0; i < num_fibers; i++)
fibers[i].join();
auto after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
std::cout << "Total ops per second: " << total / duration.count() * 1000000
<< " (" << (total / duration.count()) << " Mrps)" << std::endl;
}
}
static void call_inc(size_t &v) __attribute__ ((noinline));
static void call_inc(size_t &v)
{
v++;
}
static void test_call()
{
std::cout << "------------------------------------------" << std::endl;
std::cout << "Testing usual function call" << std::endl;
size_t total = 0;
auto before = std::chrono::high_resolution_clock::now();
auto after = before;
while (true)
{
for (size_t i = 0; i < 10000; i++)
call_inc(total);
after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
if (duration.count() >= 1000000)
break;
}
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
std::cout << "Total ops per second: " << total / duration.count()
<< " (" << (total / duration.count()) << " Mrps)" << std::endl;
}
size_t coro_total;
bool coro_run = true;
size_t coro_fin;
boost::context::continuation coro(boost::context::continuation&& cont)
{
while (coro_run)
{
coro_total++;
cont = cont.resume();
}
coro_fin++;
return std::move(cont);
}
static void test_context_switch()
{
std::cout << "------------------------------------------" << std::endl;
for (auto num_fibers : num_fibers_tests)
{
std::cout << "------------------------------------------" << std::endl;
std::cout << "Testing context switch for " << num_fibers << " boost contexts" << std::endl;
coro_total = 0;
coro_run = true;
coro_fin = 0;
std::vector<boost::context::continuation> ctxs(num_fibers);
auto before = std::chrono::high_resolution_clock::now();
auto after = before;
for (size_t i = 0; i < num_fibers; i++)
ctxs[i] = boost::context::callcc(coro);
while (true)
{
size_t j_lim = 1000000 / num_fibers;
j_lim = j_lim ? j_lim : 1;
for (size_t j = 0; j < j_lim; j++)
for (size_t i = 0; i < num_fibers; i++)
ctxs[i] = ctxs[i].resume();
after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
if (duration.count() >= 1000000)
break;
}
coro_run = false;
for (size_t i = 0; i < num_fibers; i++)
ctxs[i] = ctxs[i].resume();
assert(coro_fin == num_fibers);
size_t op_total = coro_total * 2; // 2 switched per coro
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
std::cout << "Total ops per second: " << op_total / duration.count() << " Mrps)" << std::endl;
}
}
coro_context main_ctx;
static void my_coro_func(void *smth)
{
coro_context *this_ctx = (coro_context *)smth;
while (coro_run)
{
coro_total++;
coro_transfer(this_ctx, &main_ctx);
}
coro_fin++;
coro_transfer(this_ctx, &main_ctx);
}
static void test_coro()
{
std::cout << "------------------------------------------" << std::endl;
#ifdef CORO_ASM
std::cout << "CORO ASM!" << std::endl;
#endif
for (auto num_fibers : num_fibers_tests)
{
std::cout << "------------------------------------------" << std::endl;
std::cout << "Testing context switch for " << num_fibers << " coroutines from tarantool" << std::endl;
coro_total = 0;
coro_run = true;
coro_fin = 0;
std::vector<coro_context> ctxs(num_fibers);
std::vector<coro_stack> stacks(num_fibers);
auto before = std::chrono::high_resolution_clock::now();
auto after = before;
for (size_t i = 0; i < num_fibers; i++)
{
if (!coro_stack_alloc(&stacks[i], STACK_SIZE))
{
std::cout << "coro_stack_alloc failed #" << i << std::endl;
for (size_t j = 0; j < i; j++)
coro_stack_free(&stacks[j]);
return;
}
coro_create(&ctxs[i], my_coro_func, &ctxs[i], stacks[i].sptr, stacks[i].ssze);
}
while (true)
{
size_t j_lim = 1000000 / num_fibers;
j_lim = j_lim ? j_lim : 1;
for (size_t j = 0; j < j_lim; j++)
for (size_t i = 0; i < num_fibers; i++)
{
coro_transfer(&main_ctx, &ctxs[i]);
}
after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
if (duration.count() >= 1000000)
break;
}
coro_run = false;
for (size_t i = 0; i < num_fibers; i++)
{
coro_transfer(&main_ctx, &ctxs[i]);
(void)coro_destroy(&ctxs[i]);
coro_stack_free(&stacks[i]);
}
assert(coro_fin == num_fibers);
size_t op_total = coro_total * 2; // 2 switched per coro
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
std::cout << "Total ops per second: " << op_total / duration.count() << " Mrps)" << std::endl;
}
(void)coro_destroy(&main_ctx);
}
static void* inc_pth_yield(void *arg)
{
std::pair<size_t, bool*>& targ = *(std::pair<size_t, bool*>*)arg;
bool& run = *targ.second;
while (run)
{
targ.first++;
pth_yield(NULL);
}
return NULL;
}
static void pth_yield()
{
if (pth_init() != TRUE)
std::cout << "FATAL!" << std::endl;
std::cout << "------------------------------------------" << std::endl;
for (auto num_fibers : num_fibers_tests)
{
std::cout << "------------------------------------------" << std::endl;
std::cout << "Testing yield for " << num_fibers << " pth fibers" << std::endl;
std::vector<pth_t > fibers(num_fibers);
std::vector<std::pair<size_t, bool*>> values(num_fibers);
bool run = true;
for (size_t i = 0; i < num_fibers; i++)
{
values[i].first = 0;
values[i].second = &run;
pth_attr_t attr = pth_attr_new();
pth_attr_set(attr, PTH_ATTR_STACK_SIZE, STACK_SIZE);
pth_attr_set(attr, PTH_ATTR_JOINABLE, TRUE);
fibers[i] = pth_spawn(attr, inc_pth_yield, &values[i]);
}
pth_yield(NULL);
auto before = std::chrono::high_resolution_clock::now();
pth_nap(pth_time(1, 0));
run = false;
size_t total = 0;
size_t min_value = values[0].first;
size_t max_value = values[0].first;
for (size_t i = 0; i < num_fibers; i++)
{
pth_join(fibers[i], NULL);
total += values[i].first;
if (values[i].first < min_value)
min_value = values[i].first;
if (values[i].first > max_value)
max_value = values[i].first;
}
auto after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
std::cout << "Total ops per second: " << total / duration.count() * 1000000
<< " (" << (total / duration.count()) << " Mrps)" << std::endl;
std::cout << "Ops per fiber: min: " << min_value
<< " max: " << max_value << std::endl;
}
pth_kill();
}
pth_uctx_t main_pth_ctx;
static void pth_coro_func(void *smth)
{
pth_uctx_t *this_ctx = (pth_uctx_t *)smth;
while (coro_run)
{
coro_total++;
pth_uctx_switch(*this_ctx, main_pth_ctx);
}
coro_fin++;
pth_uctx_switch(*this_ctx, main_pth_ctx);
}
static void test_pth_context()
{
std::cout << "------------------------------------------" << std::endl;
pth_uctx_create(&main_pth_ctx);
pth_uctx_make(main_pth_ctx, nullptr, 0, nullptr, 0, nullptr, nullptr);
for (auto num_fibers : num_fibers_tests)
{
std::cout << "------------------------------------------" << std::endl;
std::cout << "Testing pth context switch for " << num_fibers << " pth contexts" << std::endl;
coro_total = 0;
coro_run = true;
coro_fin = 0;
std::vector<pth_uctx_t> ctxs(num_fibers);
auto before = std::chrono::high_resolution_clock::now();
auto after = before;
for (size_t i = 0; i < num_fibers; i++)
{
pth_uctx_create(&ctxs[i]);
pth_uctx_make(ctxs[i], nullptr, STACK_SIZE, nullptr, pth_coro_func, &ctxs[i], nullptr);
pth_uctx_switch(main_pth_ctx, ctxs[i]);
}
while (true)
{
size_t j_lim = 1000000 / num_fibers;
j_lim = j_lim ? j_lim : 1;
for (size_t j = 0; j < j_lim; j++)
for (size_t i = 0; i < num_fibers; i++)
{
pth_uctx_switch(main_pth_ctx, ctxs[i]);
}
after = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
if (duration.count() >= 1000000)
break;
}
coro_run = false;
for (size_t i = 0; i < num_fibers; i++)
{
pth_uctx_switch(main_pth_ctx, ctxs[i]);
pth_uctx_destroy(ctxs[i]);
}
assert(coro_fin == num_fibers);
size_t op_total = coro_total * 2; // 2 switched per coro
auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(after - before);
std::cout << "Total ops per second: " << op_total / duration.count() << " Mrps)" << std::endl;
}
pth_uctx_destroy(main_pth_ctx);
}
int main(int n, const char**)
{
#ifdef BOOST_FIBERS_NO_ATOMICS
std::cout << "BOOST_FIBERS_NO_ATOMICS is set" << std::endl;
#endif
for (size_t i = 0; i < 1000000000; i++)
n = n * 13 + 17;
warmup();
test_yield();
test_create();
test_suspend();
test_exception();
test_call();
pth_yield();
test_context_switch();
test_coro();
test_pth_context();
return n;
}