New flat_stream example class:

fix #1108

The `flat_stream` is a stream wrapper designed to overcome a
performance limitation of the `boost::asio::ssl::stream`
implementation. Specifically, when writing buffer sequences having
length greater than one, the `ssl::stream` implementation does
not use scatter-gather I/O and instead performs a kernel transition
for every buffer in the sequence.

The wrapper addresses this problem by allocating memory and
presenting the buffer sequence into a single buffer, using
some logic to determine when this allocation is advantageous
versus simply passing the buffers through as-is.

See Also:
https://github.com/boostorg/asio/issues/100
https://stackoverflow.com/questions/50026167/performance-drop-on-port-from-beast-1-0-0-b66-to-boost-1-67-0-beast
This commit is contained in:
Vinnie Falco
2018-04-30 19:49:42 -07:00
parent dcfd07f6f2
commit c9a445a937
8 changed files with 879 additions and 13 deletions
+3 -1
View File
@@ -16,13 +16,15 @@ GroupSources(test/example/common "/")
add_executable (tests-example-common
${BOOST_BEAST_FILES}
${COMMON_FILES}
${EXTRAS_FILES}
${TEST_MAIN}
Jamfile
detect_ssl.cpp
flat_stream.cpp
root_certificates.cpp
server_certificate.cpp
session_alloc.cpp
ssl_stream.cpp
main.cpp
)
set_property(TARGET tests-example-common PROPERTY FOLDER "tests")
+1
View File
@@ -9,6 +9,7 @@
local SOURCES =
detect_ssl.cpp
flat_stream.cpp
root_certificates.cpp
server_certificate.cpp
session_alloc.cpp
+109
View File
@@ -0,0 +1,109 @@
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
// Test that header file is self-contained.
#include <example/common/flat_stream.hpp>
#include <boost/beast/test/websocket.hpp>
#include <boost/beast/test/yield_to.hpp>
#include <boost/beast/unit_test/suite.hpp>
#include <initializer_list>
#include <vector>
namespace boost {
namespace beast {
class flat_stream_test
: public unit_test::suite
, public test::enable_yield_to
{
public:
void
testSplit()
{
auto const check =
[&](
std::initializer_list<int> v0,
std::size_t limit,
unsigned long count,
bool copy)
{
std::vector<boost::asio::const_buffer> v;
v.reserve(v0.size());
for(auto const n : v0)
v.emplace_back("", n);
auto const result =
::detail::flat_stream_base::coalesce(v, limit);
BEAST_EXPECT(result.first == count);
BEAST_EXPECT(result.second == copy);
return result;
};
check({}, 1, 0, false);
check({1,2}, 1, 1, false);
check({1,2}, 2, 1, false);
check({1,2}, 3, 3, true);
check({1,2}, 4, 3, true);
check({1,2,3}, 1, 1, false);
check({1,2,3}, 2, 1, false);
check({1,2,3}, 3, 3, true);
check({1,2,3}, 4, 3, true);
check({1,2,3}, 7, 6, true);
check({1,2,3,4}, 3, 3, true);
}
void
testHttp()
{
pass();
}
void
testWebsocket()
{
{
error_code ec;
test::ws_echo_server es{log};
boost::asio::io_context ioc;
websocket::stream<flat_stream<test::stream>> ws{ioc};
ws.next_layer().next_layer().connect(es.stream());
ws.handshake("localhost", "/", ec);
BEAST_EXPECTS(! ec, ec.message());
ws.close({}, ec);
BEAST_EXPECTS(! ec, ec.message());
}
{
test::ws_echo_server es{log};
boost::asio::io_context ioc;
websocket::stream<flat_stream<test::stream>> ws{ioc};
ws.next_layer().next_layer().connect(es.stream());
ws.async_handshake("localhost", "/",
[&](error_code)
{
ws.async_close({},
[&](error_code)
{
});
});
ioc.run();
}
}
void
run() override
{
testSplit();
testHttp();
testWebsocket();
}
};
BEAST_DEFINE_TESTSUITE(beast,example,flat_stream);
} // beast
} // boost
-12
View File
@@ -1,12 +0,0 @@
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
int main()
{
}
@@ -0,0 +1,249 @@
//
// Copyright (w) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_TEST_WEBSOCKET_HPP
#define BOOST_BEAST_TEST_WEBSOCKET_HPP
#include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/websocket/stream.hpp>
#include <boost/beast/test/stream.hpp>
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/spawn.hpp>
#include <cstdlib>
#include <memory>
#include <ostream>
#include <thread>
namespace boost {
namespace beast {
namespace test {
class ws_echo_server
{
std::ostream& log_;
boost::asio::io_context ioc_;
boost::asio::executor_work_guard<
boost::asio::io_context::executor_type> work_;
multi_buffer buffer_;
test::stream ts_;
std::thread t_;
websocket::stream<test::stream&> ws_;
bool close_ = false;
public:
enum class kind
{
sync,
async,
async_client
};
explicit
ws_echo_server(
std::ostream& log,
kind k = kind::sync)
: log_(log)
, work_(ioc_.get_executor())
, ts_(ioc_)
, ws_(ts_)
{
beast::websocket::permessage_deflate pmd;
pmd.server_enable = true;
pmd.server_max_window_bits = 9;
pmd.compLevel = 1;
ws_.set_option(pmd);
switch(k)
{
case kind::sync:
t_ = std::thread{[&]{ do_sync(); }};
break;
case kind::async:
t_ = std::thread{[&]{ ioc_.run(); }};
do_accept();
break;
case kind::async_client:
t_ = std::thread{[&]{ ioc_.run(); }};
break;
}
}
~ws_echo_server()
{
work_.reset();
t_.join();
}
test::stream&
stream()
{
return ts_;
}
void
async_handshake()
{
ws_.async_handshake("localhost", "/",
std::bind(
&ws_echo_server::on_handshake,
this,
std::placeholders::_1));
}
void
async_close()
{
boost::asio::post(ioc_,
[&]
{
if(ws_.is_open())
{
ws_.async_close({},
std::bind(
&ws_echo_server::on_close,
this,
std::placeholders::_1));
}
else
{
close_ = true;
}
});
}
private:
void
do_sync()
{
try
{
ws_.accept();
for(;;)
{
ws_.read(buffer_);
ws_.text(ws_.got_text());
ws_.write(buffer_.data());
buffer_.consume(buffer_.size());
}
}
catch(system_error const& se)
{
boost::ignore_unused(se);
#if 0
if( se.code() != error::closed &&
se.code() != error::failed &&
se.code() != boost::asio::error::eof)
log_ << "ws_echo_server: " << se.code().message() << std::endl;
#endif
}
catch(std::exception const& e)
{
log_ << "ws_echo_server: " << e.what() << std::endl;
}
}
void
do_accept()
{
ws_.async_accept(std::bind(
&ws_echo_server::on_accept,
this,
std::placeholders::_1));
}
void
on_handshake(error_code ec)
{
if(ec)
return fail(ec);
do_read();
}
void
on_accept(error_code ec)
{
if(ec)
return fail(ec);
if(close_)
{
return ws_.async_close({},
std::bind(
&ws_echo_server::on_close,
this,
std::placeholders::_1));
}
do_read();
}
void
do_read()
{
ws_.async_read(buffer_,
std::bind(
&ws_echo_server::on_read,
this,
std::placeholders::_1));
}
void
on_read(error_code ec)
{
if(ec)
return fail(ec);
ws_.text(ws_.got_text());
ws_.async_write(buffer_.data(),
std::bind(
&ws_echo_server::on_write,
this,
std::placeholders::_1));
}
void
on_write(error_code ec)
{
if(ec)
return fail(ec);
buffer_.consume(buffer_.size());
do_read();
}
void
on_close(error_code ec)
{
if(ec)
return fail(ec);
}
void
fail(error_code ec)
{
boost::ignore_unused(ec);
#if 0
if( ec != error::closed &&
ec != error::failed &&
ec != boost::asio::error::eof)
log_ <<
"echo_server_async: " <<
ec.message() <<
std::endl;
#endif
}
};
} // test
} // beast
} // boost
#endif