这有点类似于boost::asio async performance。由于该问题没有确凿的答案,我发布了一个类似的问题,其中包含示例代码和统计信息以证明问题。
下面,我将对同步和异步服务器应用程序进行采样,这些应用程序在循环中连续向客户端发送25字节消息。在客户端,我检查它能够以什么速率接收消息。样本设置非常简单。在同步服务器的情况下,它为每个客户端连接生成一个新线程,并且线程继续在循环中发送25字节的消息。在异步服务器的情况下,它还会为每个客户端连接生成一个新线程,并且线程继续使用异步写入在循环中发送25字节的消息(主线程是调用ioservice.run()
的线程)。对于性能测试,我只使用一个客户端。
同步服务器代码:
#include <iostream>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
using boost::asio::ip::tcp;
class tcp_connection : public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
for (;;) {
try {
ssize_t len = boost::asio::write(socket_, boost::asio::buffer(message_));
if (len != message_.length()) {
std::cerr<<"Unable to write all the bytes"<<std::endl;
break;
}
if (len == -1) {
std::cerr<<"Remote end closed the connection"<<std::endl;
break;
}
}
catch (std::exception& e) {
std::cerr<<"Error while sending data"<<std::endl;
break;
}
}
}
private:
tcp_connection(boost::asio::io_service& io_service)
: socket_(io_service),
message_(25, 'A')
{
}
tcp::socket socket_;
std::string message_;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_service& io_service)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234))
{
start_accept();
}
private:
void start_accept()
{
for (;;) {
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
acceptor_.accept(new_connection->socket());
boost::thread(boost::bind(&tcp_connection::start, new_connection));
}
}
tcp::acceptor acceptor_;
};
int main()
{
try {
boost::asio::io_service io_service;
tcp_server server(io_service);
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
异步服务器代码:
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
using boost::asio::ip::tcp;
class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
while (socket_.is_open()) {
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
private:
tcp_connection(boost::asio::io_service& io_service)
: socket_(io_service),
message_(25, 'A')
{
}
void handle_write(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (error) {
if (socket_.is_open()) {
std::cout<<"Error while sending data asynchronously"<<std::endl;
socket_.close();
}
}
}
tcp::socket socket_;
std::string message_;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_service& io_service)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234))
{
start_accept();
}
private:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}
void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error)
{
if (!error) {
boost::thread(boost::bind(&tcp_connection::start, new_connection));
}
start_accept();
}
tcp::acceptor acceptor_;
};
int main()
{
try {
boost::asio::io_service io_service;
tcp_server server(io_service);
io_service.run();
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
客户代码:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/array.hpp>
int main(int argc, char* argv[])
{
if (argc != 3) {
std::cerr<<"Usage: client <server-host> <server-port>"<<std::endl;
return 1;
}
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(argv[1], argv[2]);
boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
boost::asio::ip::tcp::resolver::iterator end;
boost::asio::ip::tcp::socket socket(io_service);
boost::asio::connect(socket, it);
// Statscollector to periodically print received messages stats
// sample::myboost::StatsCollector stats_collector(5);
// sample::myboost::StatsCollectorScheduler statsScheduler(stats_collector);
// statsScheduler.start();
for (;;) {
boost::array<char, 25> buf;
boost::system::error_code error;
size_t len = socket.read_some(boost::asio::buffer(buf), error);
// size_t len = boost::asio::read(socket, boost::asio::buffer(buf));
if (len != buf.size()) {
std::cerr<<"Length is not "<< buf.size() << " but "<<len<<std::endl;
}
// stats_collector.incr_msgs_received();
}
}
问题:
当客户端针对同步服务器运行时,它能够接收大约700K msgs / sec,但是当它针对异步服务器运行时,性能会降低到大约100K-120K msgs / sec。我知道当我们拥有更多客户端时,应该使用异步IO来实现可伸缩性,而在上述情况下,我只使用单个客户端,异步IO的明显优势并不明显。但问题是,异步IO是否会对单个客户端案例造成如此严重的影响,或者我是否遗漏了一些与异步IO相关的明显最佳实践?性能的显着下降是因为ioservice线程(在上述情况下是主线程)和连接线程之间的线程切换?
设定: 我在Linux机器上使用BOOST 1.47。
答案 0 :(得分:2)
这不是应该如何使用异步发送:这样连接的线程就会在asio队列中放入越来越多的写请求,并且同时调用ioservice.run()
的线程将它们出列。
低性能很可能是因为主线程(生产者)和运行ioservice
(消费者)的线程上的ioservice工作队列存在高争用。
此外,如果你监视你的记忆,你应该看到它的增长,最终阻止你的系统:我确实希望生产者比消费者更快。
只报告两种相关方法的正确方法(未经测试)应该是这样的:
void start()
{
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_write(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (error) {
if (socket_.is_open()) {
std::cout<<"Error while sending data asynchronously"<<std::endl;
socket_.close();
}
}
if (socket_.is_open()) {
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
也就是说,连接线程只“点燃”事件驱动的循环,即运行ioservice
的线程的一部分:一旦执行写入,调用回调以发布下一个回调。