Boost,通过TCP发送binary_oarchive,突然减速

时间:2018-03-01 12:02:35

标签: c++ boost boost-asio

我使用boost来序列化一个struct,然后通过tcp将该结构发送到另一个应用程序。 (两者都在同一台机器上,用于测试目的)。

这一切都运行良好,打包,发送和解包的总时间约为10毫秒。然而,现在它突然跳到了30ms。

我是否正确测量了延迟?如果是这样,可能导致这种放缓的原因是什么?我怎样才能恢复速度?

结构:

var hobbies = [{ "id": 1, "hobbies": [] }, { "id": 2, "hobbies": [ "football" ] }, { "id": 3, "hobbies": [ "football", "basketball" ] } ];

let result = [];

hobbies.forEach(function(profile){
  profile.hobbies.forEach(function(hobby){
    result.push(
            {
        "id": profile.id,
        "hobby": hobby
      }
    );
  });
});

console.log(result)

发送申请:

struct frame
{

    long milliseconds;
    vector<float> buff;
    template <typename Archive>
    void serialize(Archive& ar, const unsigned int version)
    {

        ar & milliseconds;
        ar & buff;
    }
};

收到申请表:

    frame data;

    static auto const flags = boost::archive::no_header | boost::archive::no_tracking;


    boost::asio::io_service ios;
    boost::asio::ip::tcp::endpoint endpoint
        = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 4444);
    boost::asio::ip::tcp::acceptor acceptor(ios, endpoint);
    boost::asio::ip::tcp::iostream stream;

    // Your program stops here until client connects.
    acceptor.accept(*stream.rdbuf());

    //get ms time to test latency
    pt::ptime current_date_microseconds = pt::microsec_clock::local_time();
    long milliseconds = current_date_microseconds.time_of_day().total_milliseconds();


    //add dummy data to vector
    std::vector<float> temp(100, 0.0);
    frm.buff = temp;

    //add milliseconds, for latency check
    data.milliseconds = milliseconds;

   //send data
    boost::archive::binary_oarchive archive(stream);
    archive << data;

0 个答案:

没有答案