Last active
April 23, 2020 10:23
-
-
Save jamboree/a59ba5427beba4529bd245e20cfbc111 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <boost/asio/io_context.hpp> | |
#include <boost/asio/experimental/co_spawn.hpp> | |
#include <boost/asio/experimental/detached.hpp> | |
#include <boost/beast/experimental/test/stream.hpp> | |
#include <art/detached_task.hpp> | |
#include <art/sync/channel.hpp> | |
#include <act/stream.hpp> | |
#include <benchmark/benchmark.h> | |
namespace asio = boost::asio; | |
namespace test = boost::beast::test; | |
using boost::asio::experimental::co_spawn; | |
using boost::asio::experimental::detached; | |
namespace this_coro = boost::asio::experimental::this_coro; | |
template <typename T> | |
using awaitable = boost::asio::experimental::awaitable< | |
T, boost::asio::io_context::executor_type>; | |
struct cps_reader | |
{ | |
struct state | |
{ | |
test::stream s; | |
char buf[1024]; | |
}; | |
static void read_next(std::unique_ptr<state> self) | |
{ | |
auto p = self.get(); | |
p->s.async_read_some(boost::asio::buffer(p->buf), [self = std::move(self)](act::error_code ec, std::size_t n) mutable | |
{ | |
if (n) | |
{ | |
//std::cout.write(self->buf, n); | |
read_next(std::move(self)); | |
} | |
}); | |
} | |
static void run(test::stream&& s) | |
{ | |
read_next(std::unique_ptr<state>(new state{std::move(s)})); | |
} | |
}; | |
struct cps_writer | |
{ | |
struct state | |
{ | |
test::stream s; | |
std::string_view str = "Hello"; | |
int i = 0; | |
}; | |
static void write_next(std::unique_ptr<state> self) | |
{ | |
auto p = self.get(); | |
p->s.async_write_some(boost::asio::buffer(p->str), [self = std::move(self)](act::error_code ec, std::size_t n) mutable | |
{ | |
if (++self->i != 5) | |
write_next(std::move(self)); | |
}); | |
} | |
static void run(test::stream&& s) | |
{ | |
write_next(std::unique_ptr<state>(new state{std::move(s)})); | |
} | |
}; | |
awaitable<void> asio_reader(test::stream s) | |
{ | |
auto token = co_await this_coro::token(); | |
char buf[1024]; | |
while (auto n = co_await s.async_read_some(boost::asio::buffer(buf), token)) | |
{ | |
//std::cout.write(buf, n); | |
} | |
} | |
awaitable<void> asio_writer(test::stream s) | |
{ | |
auto token = co_await this_coro::token(); | |
std::string_view str = "Hello"; | |
for (int i = 0; i != 5; ++i) | |
{ | |
co_await s.async_write_some(asio::buffer(str), token); | |
} | |
} | |
art::detached_task act_reader(test::stream s) | |
{ | |
char buf[1024]; | |
act::error_code ec; | |
while (auto n = co_await act::read_some(s, asio::buffer(buf), ec)) | |
{ | |
//std::cout.write(buf, n); | |
} | |
} | |
art::detached_task act_writer(test::stream s) | |
{ | |
std::string_view str = "Hello"; | |
for (int i = 0; i != 5; ++i) | |
{ | |
co_await act::write_some(s, asio::buffer(str)); | |
} | |
} | |
static void act_usage(benchmark::State& state) | |
{ | |
asio::io_context io; | |
while (state.KeepRunning()) | |
{ | |
test::stream sw{io}; | |
act_reader(connect(sw)); | |
act_writer(std::move(sw)); | |
io.run(); | |
io.restart(); | |
} | |
} | |
static void asio_usage(benchmark::State& state) | |
{ | |
asio::io_context io; | |
while (state.KeepRunning()) | |
{ | |
test::stream sw{io}; | |
co_spawn(io, [sr = connect(sw)]() mutable | |
{ | |
return asio_reader(std::move(sr)); | |
}, detached); | |
co_spawn(io, [sw = std::move(sw)]() mutable | |
{ | |
return asio_writer(std::move(sw)); | |
}, detached); | |
io.run(); | |
io.restart(); | |
} | |
} | |
static void cps_usage(benchmark::State& state) | |
{ | |
asio::io_context io; | |
while (state.KeepRunning()) | |
{ | |
test::stream sw{io}; | |
cps_reader::run(connect(sw)); | |
cps_writer::run(std::move(sw)); | |
io.run(); | |
io.restart(); | |
} | |
} | |
//int main() | |
//{ | |
// asio::io_context io; | |
// test::stream sw{io}; | |
// cps_reader::run(connect(sw)); | |
// cps_writer::run(std::move(sw)); | |
// io.run(); | |
//} | |
BENCHMARK(act_usage); | |
BENCHMARK(asio_usage); | |
BENCHMARK(cps_usage); | |
BENCHMARK_MAIN(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Run on (8 X 3392 MHz CPU s) | |
CPU Caches: | |
L1 Data 32K (x4) | |
L1 Instruction 32K (x4) | |
L2 Unified 262K (x4) | |
L3 Unified 8388K (x1) | |
-------------------------------------------------- | |
Benchmark Time CPU Iterations | |
-------------------------------------------------- | |
act_usage 6908 ns 6975 ns 112000 | |
asio_usage 18444 ns 18415 ns 37333 | |
cps_usage 6256 ns 6278 ns 112000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment