这与boost::asio异步性能有些相似。由于那个问题没有定论,我将发布一个类似的问题并附带样本代码和统计数据以演示问题。
下面是我准备的同步和异步服务器应用程序样例,它们不断循环地向客户端发送25字节的消息。在客户端端,我正在检查它接收消息的速度。该样例设置非常简单。在同步服务器情况下,它为每个客户端连接生成一个新线程,并且该线程保持循环发送25字节的消息。在异步服务器情况下,它也为每个客户端连接生成一个新线程,并且该线程使用异步写入循环发送25字节的消息(主线程调用
下面是我准备的同步和异步服务器应用程序样例,它们不断循环地向客户端发送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个消息/秒,但当它运行在异步服务器上时,性能下降到大约100K-120K个消息/秒。我知道当我们有更多的客户端时,应该使用异步IO来提高可扩展性,在上述情况下,由于我只使用了一个单一的客户端,因此异步IO的显而易见的优势并不明显。但问题是,异步IO是否预期会对单个客户端案例的性能产生如此严重的影响,或者我是否忽略了一些明显的最佳实践来使用异步IO?性能的显著下降是由于ioservice线程(在上述情况中是主线程)和连接线程之间的线程切换引起的吗?
设置: 我在Linux机器上使用BOOST 1.47。
ioservice.run()
。请参阅教程。 - ted