主頁 > .NET開發 > HTTP壓力測驗工具使用boostasioasync_connect問題

HTTP壓力測驗工具使用boostasioasync_connect問題

2022-11-05 05:46:46 .NET開發

使用以下boost::asio代碼,我對 Docker node.js 簡單的 http 服務運行 1M 次連續 http 呼叫回圈,該服務生成亂數,但在幾千次呼叫后,我開始收到 async_connect 錯誤。node.js 部分沒有產生任何錯誤,我相信它可以正常作業。

為了避免在每次呼叫中決議主機并嘗試加速,我正在快取端點,這沒有區別,我已經測驗了兩種方式。

誰能看到我下面的代碼有什么問題?我缺少使用 asio 的壓力測驗工具的最佳實踐嗎?

//------------------------------------------------------------------------------
// https://www.boost.org/doc/libs/1_70_0/libs/beast/doc/html/beast/using_io/timeouts.html

HttpResponse HttpClientAsyncBase::_http(HttpRequest&& req)
{
    using namespace boost::beast;
    namespace net = boost::asio;
    using tcp = net::ip::tcp;

    HttpResponse res;
    req.prepare_payload();
    boost::beast::error_code ec = {};

    const HOST_INFO host = resolve(req.host(), req.port, req.resolve);

    net::io_context m_io;

    boost::asio::spawn(m_io, [&](boost::asio::yield_context yield)
    {
        size_t retries = 0;

        tcp_stream stream(m_io);
        
        if (req.timeout_seconds == 0) get_lowest_layer(stream).expires_never();
        else get_lowest_layer(stream).expires_after(std::chrono::seconds(req.timeout_seconds));
        
        get_lowest_layer(stream).async_connect(host, yield[ec]);
        if (ec) return;

        http::async_write(stream, req, yield[ec]);
        if (ec)
        {
            stream.close();
            return;
        }

        flat_buffer buffer;
        http::async_read(stream, buffer, res, yield[ec]);

        stream.close();
    });

    m_io.run();

    if (ec)
        throw boost::system::system_error(ec);

    return std::move(res);
}

我已經嘗試了 boost http 客戶端的同步/異步實作,但我遇到了完全相同的問題。

我得到的錯誤是“您沒有連接,因為網路上存在重復的名稱。如果加入域,請轉到控制面板中的系統更改計算機名稱并重試。如果加入作業組,請選擇另一個作業組名稱 [系統:52]"

uj5u.com熱心網友回復:

所以,我決定……試試看。我把你的代碼變成了獨立的例子:

#include <boost/asio/spawn.hpp>
#include <boost/beast.hpp>
#include <fmt/ranges.h>
#include <iostream>
namespace http = boost::beast::http;
//------------------------------------------------------------------------------
// https://www.boost.org/doc/libs/1_70_0/libs/beast/doc/html/beast/using_io/timeouts.html
struct HttpRequest : http::request<http::string_body> { // SEHE: don't do this
    using base_type = http::request<http::string_body>;
    using base_type::base_type;

    std::string host() const { return "127.0.0.1"; }
    uint16_t    port    = 80;
    bool        resolve = true;

    int timeout_seconds = 0;
};
using HttpResponse = http::response<http::vector_body<uint8_t> >; // Do this or aggregation instead

struct HttpClientAsyncBase {
    HttpResponse _http(HttpRequest&& req);

    using HOST_INFO = boost::asio::ip::tcp::endpoint;
    static HOST_INFO resolve(std::string const& host, uint16_t port, bool resolve) {
        namespace net = boost::asio;
        using net::ip::tcp;

        net::io_context ioc;
        tcp::resolver   r(ioc);
        using flags = tcp::resolver::query::flags;

        auto f = resolve ? flags::address_configured
                         : static_cast<flags>(flags::numeric_host | flags::numeric_host);

        tcp::resolver::query q(tcp::v4(), host, std::to_string(port), f);

        auto it = r.resolve(q);
        assert(it.size());
        return HOST_INFO{it->endpoint()};
    }
};

HttpResponse HttpClientAsyncBase::_http(HttpRequest&& req) {
    using namespace boost::beast;
    namespace net = boost::asio;
    using net::ip::tcp;

    HttpResponse res;
    req.prepare_payload();
    boost::beast::error_code ec = {};

    const HOST_INFO host = resolve(req.host(), req.port, req.resolve);

    net::io_context m_io;

    spawn(m_io, [&](net::yield_context yield) {
        // size_t retries = 0;

        tcp_stream stream(m_io);

        if (req.timeout_seconds == 0)
            get_lowest_layer(stream).expires_never();
        else
            get_lowest_layer(stream).expires_after(std::chrono::seconds(req.timeout_seconds));

        get_lowest_layer(stream).async_connect(host, yield[ec]);
        if (ec)
            return;

        http::async_write(stream, req, yield[ec]);
        if (ec) {
            stream.close();
            return;
        }

        flat_buffer buffer;
        http::async_read(stream, buffer, res, yield[ec]);

        stream.close();
    });

    m_io.run();

    if (ec)
        throw boost::system::system_error(ec);

    return res;
}

int main() {
    for (int i = 0; i<100'000;   i) {
        HttpClientAsyncBase hcab;
        HttpRequest         r(http::verb::get, "/bytes/10", 11);
        r.timeout_seconds = 0;
        r.port            = 80;
        r.resolve         = false;

        auto res = hcab._http(std::move(r));
        std::cout << res.base() << "\n";
        fmt::print("Data: {::02x}\n", res.body());
    }
}

(旁注,這是docker run -p 80:80 kennethreitz/httpbin用于運行服務器端)

雖然這比在 bash 回圈中執行等效請求快約 10 倍curl,但這些都不是特別緊張。它沒有任何異步,而且資源使用似乎是溫和而穩定的,例如記憶體分析:

HTTP壓力測驗工具使用boost asio async_connect問題

(為了完整性,我用 驗證了相同的結果timeout_seconds = 1

由于您所做的實際上與異步 IO 正好相反,因此我會寫得更簡單:

struct HttpClientAsyncBase {
    net::io_context m_io;

    HttpResponse _http(HttpRequest&& req);

    static auto resolve(std::string const& host, uint16_t port, bool resolve);
};

HttpResponse HttpClientAsyncBase::_http(HttpRequest&& req) {
    HttpResponse res;
    req.requestObject.prepare_payload();

    const auto host = resolve(req.host(), req.port, req.resolve);

    beast::tcp_stream stream(m_io);

    if (req.timeout_seconds == 0)
        stream.expires_never();
    else
        stream.expires_after(std::chrono::seconds(req.timeout_seconds));

    stream.connect(host);

    write(stream, req.requestObject);

    beast::flat_buffer buffer;
    read(stream, buffer, res);

    stream.close();

    return res;
}

這只是更簡單,運行速度更快,并且做同樣的事情,直到例外。

但是,您可能正試圖造成壓力,也許您需要重用一些連接和多執行緒?

你可以在這里看到一個非常完整的例子: 如何使這個 HTTPS 連接在 Beast 中持久化?

它包括重新連接斷開的連接、與不同主機的連接、不同的請求等。

uj5u.com熱心網友回復:

Alan 的評論給了我正確的指導,我很快發現netstat -a這是一個埠泄漏問題,在運行代碼一段時間后,數千個埠處于 TIME_WAIT 狀態。

根本原因在客戶端和服務器上:

  1. 在 node.js 服務器中,我必須確保回應通過添加來關閉連接

    response.setHeader("connection", "close");

  2. 在 boost::asio C 代碼中,我替換stream.close()

    stream.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);

    這似乎使一切變得不同。我也確保使用

    req.set(boost::beast::http::field::connection, "close"); 在我的要求中。

我驗證了該工具運行了 5 個多小時,完全沒有問題,所以我想問題已經解決了!

uj5u.com熱心網友回復:

使用 boost::asio 實作“Abortive TCP/IP Close”以處理 EADDRNOTAVAIL 和 TIME_WAIT 用于 HTTP 客戶端壓力測驗工具

我正在重新審視這個問題,以提供一個實際上效果更好的替代方案。提醒一下,目標是開發一個壓力測驗工具,用于以 100 萬個請求訪問服務器。即使我之前的解決方案在 Windows 上運行,當我在 Docker/Alpine 上加載可執行檔案時,它開始崩潰并出現我無法追蹤的 SEGFAULT 錯誤。根本原因似乎與boost::asio::spawn(m_io, [&](boost::asio::yield_context yield)但時間迫使我解決 HTTP 問題有關。

我決定使用同步 HTTP 并處理EADDRNOTAVAILTIME_WAIT錯誤,方法是遵循Disable TIME_WAIT with boost socketsTIME_WAIT with boost asio和來自https://www.boost.org/doc/libs/1_80_0/libs/beast/的模板代碼示例/http/client/sync/http_client_sync.cpp

對于任何擁有帶有 boost::asio 的 EADDRNOTAVAIL 和 TIME_WAIT 的人來說,對我有用的解決方案實際上比以前在 Windows、Linux 和 Dockers 上都要快得多,如下所示:

HttpResponse HttpClientSyncBase::_http(HttpRequest&& req)
{
    namespace beast = boost::beast;
    namespace http = beast::http;
    namespace net = boost::asio;
    using tcp = net::ip::tcp;

    HttpResponse res;
    req.prepare_payload();

    const auto host = req.host();
    const auto port = req.port;
    const auto target = req.target();
    const bool abortive_close = boost::iequals(req.header("Connection"), "close");
    const bool download_large_file = boost::iequals(req.header("X-LARGE-FILE-HINT"), "YES"); 

    beast::error_code ec;
    net::io_context ioc;

    // Resolve host:port for IPv4
    tcp::resolver resolver(ioc);
    const auto endpoints = resolver.resolve(boost::asio::ip::tcp::v4(), host, port);

    // Create stream and set timeouts
    beast::tcp_stream stream(ioc);  
    if (req.timeout_seconds == 0) boost::beast::get_lowest_layer(stream).expires_never();
    else boost::beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(req.timeout_seconds));

    // Caution: we can get address_not_available[EADDRNOTAVAIL] due to TIME_WAIT port exhaustion
    stream.connect(endpoints, ec);
    if (ec == boost::system::errc::address_not_available)
        throw beast::system_error{ ec };

    // Write HTTP request
    http::write(stream, req);

    // Read HTTP response (or download large file >8MB) 
    beast::flat_buffer buffer;
    if (download_large_file)
    {       
        _HttpResponse tmp;
        boost::beast::http::response_parser<boost::beast::http::string_body> parser{ std::move(tmp) };      
        parser.body_limit(boost::none);             
        boost::beast::http::read(stream, buffer, parser);       
        res = HttpResponse(std::move(parser.release()));
    }
    else
    {       
        http::read(stream, buffer, res);
    }

    // Try to shut down socket gracefully   
    stream.socket().shutdown(tcp::socket::shutdown_both, ec);

    if (abortive_close)
    {
        // Read until no more data are in socket buffers
        // https://stackoverflow.com/questions/58983527/disable-time-wait-with-boost-sockets
        try
        {
            http::response<http::dynamic_body> res;
            beast::flat_buffer buffer;
            http::read(stream, buffer, res);
        }
        catch (...)
        {
            // should get end of stream here, ignore it
        }

        // Perform "Abortive TCP/IP Close" to minimize TIME_WAIT port exhaustion
        // https://stackoverflow.com/questions/35006324/time-wait-with-boost-asio   
        try
        {
            // enable linger with timeout 0 to force abortive close
            boost::asio::socket_base::linger option(true, 0);
            stream.socket().set_option(option);
            stream.close();
        }
        catch (...)
        {
        }
    }
    else
    {
        try { stream.close(); } catch (...) {}      
    }

    // Ignore not_connected and end_of_stream errors, handle the rest
    if (ec && ec != beast::errc::not_connected && ec != beast::http::error::end_of_stream)
        throw beast::system_error{ ec };

    return std::move(res);
}

在上面的示例中,我應該在 write 中添加錯誤處理,但我想任何人都可以做到。_HttpResponse 如下,是 HttpResponse 的基礎。

using _HttpRequest = boost::beast::http::message<true, boost::beast::http::string_body, boost::beast::http::fields>;
using _HttpResponse = boost::beast::http::message<false, boost::beast::http::string_body, boost::beast::http::fields>;
using HttpHeaders = boost::beast::http::header<1, boost::beast::http::basic_fields<std::allocator<char>>>;

值得一提的是,當我開始這項作業的估計是 5-7 天。在我之前的解決方案中使用 connetion=close 可以縮短到 7-8 小時。使用Abortive TCP/IP Close我減少了 1.5 小時。

有趣的是,服務器也 boost::asio 可以處理壓力,而原來的壓力工具不能。最后,服務器及其壓力測驗工具都可以正常作業!該代碼還演示了如何下載一個大檔案(超過 8MB),這是另一個問題,因為我需要從服務器下載測驗結果。

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/527721.html

標籤:C http升压-asio

上一篇:如何使用TypeScriptObsidian庫發送多部分/表單資料有效負載?

下一篇:nuget打包靜態資源的問題

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more