-
Notifications
You must be signed in to change notification settings - Fork 1
/
seastar_skynet_benchmark.cpp
132 lines (111 loc) · 4.42 KB
/
seastar_skynet_benchmark.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
#include <boost/iterator/counting_iterator.hpp>
#include "core/app-template.hh"
#include "core/sleep.hh"
#include "core/gate.hh"
#include "core/thread.hh"
// seastar::future<> service_loop() {
// return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234})),
// [] (auto& listener) {
// return seastar::keep_doing([&listener] () {
// return listener.accept().then(
// [] (seastar::connected_socket s, seastar::socket_address a) {
// std::cout << "Accepted connection from " << a << "\n";
// });
// });
// });
// }
// seastar::future<> f() {
// return seastar::parallel_for_each(boost::irange<unsigned>(0, seastar::smp::count),
// [] (unsigned c) {
// return seastar::smp::submit_to(c, [] {
// std::cout << seastar::thread::running_in_thread() << std::endl;
// });
// });
// }
seastar::future<std::uint64_t> skynet(std::uint64_t num, std::uint64_t size, std::uint64_t div) {
if ( size != 1) {
size /= div;
std::vector<seastar::future<std::uint64_t> > results;
results.reserve(div);
for (std::uint64_t i = 0; i != div; ++i) {
std::uint64_t sub_num = num + i * size;
results.emplace_back(
skynet(sub_num, size, div)
);
}
auto p = seastar::make_shared(std::move(results));
return seastar::when_all(p->begin(), p->end()).then(
[p] (std::vector<seastar::future<uint64_t>> ret) {
std::uint64_t sum = 0;
for (auto& r: ret) {
sum += r.get0();
}
return sum;
}
);
}
return seastar::make_ready_future<std::uint64_t>(num);
}
seastar::future<std::uint64_t> skynet_start(std::uint64_t num, std::uint64_t size, std::uint64_t div) {
const auto parallelism_factor = seastar::smp::count;
if (div % parallelism_factor != 0) {
std::cout << "Parallelism may be unfair!" <<std::endl;
}
size /= div;
std::vector<seastar::future<std::uint64_t> > results;
results.reserve(parallelism_factor);
for (std::uint64_t i = 0; i != div; ++i) {
std::uint64_t sub_num = num + i * size;
auto shard = parallelism_factor == 1 ? 0 : i % parallelism_factor;
results.emplace_back(
seastar::smp::submit_to(shard, [=] {return skynet(sub_num, size, div);})
);
}
auto p = seastar::make_shared(std::move(results));
return seastar::when_all(p->begin(), p->end()).then(
[p] (std::vector<seastar::future<uint64_t>> ret) {
std::uint64_t sum = 0;
for (auto& r: ret) {
sum += r.get0();
}
return sum;
}
);
}
using clock_type = std::chrono::steady_clock;
using duration_type = clock_type::duration;
using time_point_type = clock_type::time_point;
int main(int argc, char** argv) {
seastar::app_template app;
app.run(argc, argv, [] {
std::cout << "Paralellism factor: " << seastar::smp::count << "\n";
const std::size_t size{ 10'000'000 };
const std::size_t div{ 10 };
time_point_type start{ clock_type::now() };
return skynet_start(0, size, div).then(
[start] (std::uint64_t result) {
std::cout << result << std::endl;
auto duration = clock_type::now() - start;
std::cout << "duration: " << duration.count() / 1'000'000 << " ms" << std::endl;
if (49999995000000 != result) {
throw std::runtime_error("invalid result");
}
}
);
// return seastar::check_direct_io_support("/home/max")
// .then([] {
// std::cout << "Ok, homedir is with DMA support" << std::endl;
// }).handle_exception([](auto exception_ptr){
// std::cout << "Homdir is NOT support DMA" << std::endl;
// }).then([] {
// return seastar::file_system_at("/");
// }).then([] (auto fs_type) {
// std::cout << std::boolalpha << "FS type is Ext4: " << (fs_type == seastar::fs_type::ext4) << std::endl;
// }).then([] {
// return f();
// // return seastar::async([] {
// // std::cout << seastar::thread::running_in_thread() << std::endl;
// // });
// });
});
}