【问题标题】:Boost::Asio HTTP Server extremely slowBoost::Asio HTTP Server 极慢
【发布时间】:2013-09-01 15:19:37
【问题描述】:

我目前正在尝试使用 Boost.Asio 创建一个 http 服务器,我将其设置为 HTTP Server 3。

目前我只是阅读了请求并总是返回一个 OK 消息。所以没有什么特别或耗时的。

我遇到的问题是,使用 12 线程(16 核 @ 2.53GHz)运行服务器,服务器每秒处理大约 200-300 个请求。

我在 C# 中使用 HttpListener 做了同样的事情,运行 12 个线程,它处理大约 5000-7000 个请求。

Boost.Asio 到底在做什么?

在 Visual Studio 中使用 Instrumentation Profiling 获得以下“大多数个人工作的功能”:

Name                         Exclusive Time %
GetQueuedCompletionStatus               44,46
std::_Lockit::_Lockit                   14,54
std::_Container_base12::_Orphan_all      3,46
std::_Iterator_base12::~_Iterator_base12 2,06

编辑 1:

if (!err) {
  //Add data to client request
  if(client_request_.empty())
    client_request_ = std::string(client_buffer_.data(), bytes_transferred);
  else
    client_request_ += std::string(client_buffer_.data(), bytes_transferred);
  //Check if headers complete
  client_headerEnd_ = client_request_.find("\r\n\r\n");
  if(client_headerEnd_ == std::string::npos) {
    //Headers not yet complete, read again
    client_socket_.async_read_some(boost::asio::buffer(client_buffer_),
        boost::bind(&session::handle_client_read_headers, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred)); 
  } else { 
    //Search Cookie 
    std::string::size_type loc=client_request_.find("Cookie"); 
    if(loc != std::string::npos) {
    //Found Cookie
    std::string::size_type locend=client_request_.find_first_of("\r\n", loc);
    if(locend != std::string::npos) {
      std::string lCookie = client_request_.substr(loc, (locend-loc));            loc = lCookie.find(": ");           if(loc != std::string::npos) {
        std::string sCookies = lCookie.substr(loc+2);
        std::vector<std::string> vCookies;
        boost::split(vCookies, sCookies, boost::is_any_of(";"));
        for (std::size_t i = 0; i < vCookies.size(); ++i) {
          std::vector<std::string> vCookie;
          boost::split(vCookie, vCookies[i], boost::is_any_of("="));
          if(vCookie[0].compare("sessionid") == 0) {
            if(vCookie.size() > 1) {
              client_sessionid_ = vCookie[1];
              break;
            }
          }
        }             }
    }         }
    //Search Content-Length
    loc=client_request_.find("Content-Length");
    if(loc == std::string::npos) {
      //No Content-Length, no Content? -> stop further reading
      send_bad_request();
      return;
    }
    else {
      //Parse Content-Length, for further body reading
      std::string::size_type locend=client_request_.find_first_of("\r\n", loc);
      if(locend == std::string::npos) {
        //Couldn't find header end, can't parse Content-Length -> stop further reading
        send_bad_request();
        return;
      }
      std::string lHeader = client_request_.substr(loc, (locend-loc));
      loc = lHeader.find(": ");
      if(loc == std::string::npos) {
        //Couldn't find colon, can't parse Content-Length -> stop further reading
        send_bad_request();
        return;
      }
      //Save Content-Length
      client_request_content_length_ = boost::lexical_cast<std::string::size_type>(lHeader.substr(loc+2));
      //Check if already read complete body
      if((client_request_.size()-(client_headerEnd_)) < client_request_content_length_) {
        //Content-Length greater than current body, start reading.
        client_socket_.async_read_some(boost::asio::buffer(client_buffer_),
            boost::bind(&session::handle_client_read_body, shared_from_this(),
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
      }
      else {
        //Body is complete, start handling
        handle_request();
      }
    }
  }
}

编辑 2:

用于测试的客户端是一个简单的 C# 应用程序,它启动 128 个线程,每个线程迭代 1000 次,没有任何睡眠。

System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(BaseUrl);
req.Method = "POST";
byte[] buffer = Encoding.ASCII.GetBytes("{\"method\":\"User.Login\",\"params\":[]}");
req.GetRequestStream().Write(buffer, 0, buffer.Length);
req.GetRequestStream().Close();

【问题讨论】:

  • 简单地重写代码。这不是最佳的。示例中的 request_parser 解析每个字节并将其 push_back 为字符串,没有任何保留。 asio::strand 还有一些未记录的问题。
  • 很难在没有看到一些真实代码的情况下判断..您是否尝试过对其进行分析?另外,您是否对其进行了优化编译?
  • 这通常是保持 Nagle 开启的问题。见:stackoverflow.com/questions/2039224/poor-boost-asio-performance/…
  • @ForEveR 即使 request_parse 不是最优的,它也不能解释为什么它如此之慢。
  • @RaphaelH 如果 C# 客户端和服务器都使用 HTTP keepalive,这可能很重要。 AFAIK Boost.Asio HTTP 示例在每次请求后关闭套接字。

标签: c++ multithreading boost boost-asio


【解决方案1】:

缓慢的原因可能是 Boost::Asio HTTP Server 3 示例总是在每次响应后关闭连接,强制客户端为每个请求创建一个新连接。在每个请求上打开和关闭连接都需要大量时间。显然,这无法胜过任何支持 HTTP/1.1 和 Keep-alive 的服务器(基本上,不会关闭客户端连接并允许客户端将其重用于后续请求)。

您的 C# 服务器 System.Net.HttpListener 确实支持 Keep-alive。客户端 System.Net.HttpWebRequest 也默认启用 Keep-alive。因此,连接在此配置中被重用。

向 HTTP Server 3 示例添加 keep-alive 很简单:

  1. inside connection::handle_read() 检查请求是否客户端请求 Keep-alive 并将此标志存储在连接中

  2. 更改 connection::handle_write() 以便它仅在客户端不支持 Keep-alive 时启动优雅的连接关闭,否则只需像您在 connection::start() 中所做的那样启动 async_read_some():

    socket_.async_read_some(boost::asio::buffer(buffer_),
        strand_.wrap(
            boost::bind(&connection::handle_read, shared_from_this(),
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred)));
    

并且不要忘记在调用 async_read_some() 之前清除您的请求/回复并重置 request_parser。

【讨论】:

  • 正如我已经在主 cmets 中编写的那样,我尝试使用 .net TcpListener 并像在 C++ 中那样解析请求,但我仍然可以轻松达到每秒 4500 个请求。而且我还关闭了每一个连接。
  • 我仍然不明白在没有 keepalive 的情况下测试 HTTP 服务器的性能有什么意义。没有一个网络服务器以这种方式工作,因为它太慢了。
  • 我理解您的反对意见,但令人困惑的是 .Net 速度更快。
  • 如果你有兴趣比较苹果和苹果,要么在 C# 和 Boost.Asio 中都使用 keepalive,要么在两者中都禁用它。
  • 正如我所说,我尝试使用 TcpListener 而不是 HttpListener 进行比较,我认为它不支持 HTTP Keep Alive。
【解决方案2】:

似乎client_request_.find("\r\n\r\n"); 被重复调用——从每个循环的字符串开头寻找结束标记。使用起始位置位置。比如client_request_.find("\r\n\r\n", lastposition);(使用bytes_transferred)

可以使用asycn_read_until( ,"\r\n\r\n"); 找到here

或async_read 应该读取所​​有(而不是一些)。

【讨论】:

    【解决方案3】:

    关于 HTTP 服务器 3 示例。查看request_parser 源代码。方法解析/使用。这确实不是最佳选择,因为它从缓冲区逐字节获取数据并处理每个字节;使用 push_back 推入 std::string 等等。这只是一个例子。

    另外,如果您使用的是 asio::strand,请注意它使用互斥锁“strand implementation”。对于 HTTP 服务器,它很容易完全删除 asio::strand,所以我建议这样做。如果您想保留 strands - 为了避免锁定延迟,您可以在编译时设置这些定义:

    -DBOOST_ASIO_STRAND_IMPLEMENTATIONS=30000 -DBOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION
    

    【讨论】:

    • 刚刚删除了所有“链”,但仍然非常慢。我没有使用教程 request_parser,添加了我正在使用的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    相关资源
    最近更新 更多