MyTtcp 测试网络带宽
Posted itdef
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyTtcp 测试网络带宽相关的知识,希望对你有一定的参考价值。
网络编程学习
注意的指标
MB/S 带宽
每秒处理的信息 查询等 messages/s queries/s transaction/s
延时
cpu使用率
ttcp测试网络 读写读写 循环 测试网络带宽
正确关闭TCP shutdown_write read->0 closesocket
TCP_NODELAY 延时发送小数据包 认为设置 不等待 即时发送 开启
reuseaddr socket复用
完整代码见
https://git.oschina.net/def/cppPratice/tree/master/MyTtcpV0.1
#include "RecvMessageLoop.h" #include <boost/asio.hpp> #include <iostream> #include <chrono> #include "Common.h" using boost::asio::ip::tcp; void RecvMessageLoop(const std::string& ipStr, const std::string& portStr, const std::string& MessageBlockLen) { //std::cout << "RecvMessageLoop" << std::endl; boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), std::atoi(portStr.c_str()))); tcp::socket socket(io_service); acceptor.accept(socket); //SetSocketNoDelay(socket,true); boost::system::error_code ec; long recvBlocklen = 0; size_t len = socket.read_some(boost::asio::buffer(&recvBlocklen,sizeof(recvBlocklen)), ec); if (len != sizeof(recvBlocklen) || ec) { PRINTERRORCODE(ec); return; } std::cout << "recvlen is: " << recvBlocklen << std::endl; int testLoopTimes = defaultSendLength / recvBlocklen; len = boost::asio::write(socket,boost::asio::buffer(&testLoopTimes, sizeof(testLoopTimes)), ec); if (len != sizeof(testLoopTimes) || ec) { PRINTERRORCODE(ec); return; } std::unique_ptr<char[]> testBuf(new char[recvBlocklen]);//ok for (int i = 0; i < recvBlocklen; ++i) { testBuf[i] = \'\\0\'; } auto start = std::chrono::steady_clock::now(); while(1) { len = boost::asio::read(socket, boost::asio::buffer(testBuf.get(), recvBlocklen),ec); if (ec == boost::asio::error::eof) { break; }else if (len != recvBlocklen || ec) { PRINTERRORCODE(ec); return; } //std::cout << "read success" << std::endl; int i = len; len = boost::asio::write(socket, boost::asio::buffer(&i, sizeof(i)), ec); if (len != sizeof(i) || ec) { PRINTERRORCODE(ec); return; } //std::cout << "write success" << std::endl; } //socket.shutdown(boost::asio::socket_base::shutdown_send); //len = boost::asio::read(socket, // boost::asio::buffer(&len, sizeof(len)), ec); //socket.close(); auto end = std::chrono::steady_clock::now(); std::chrono::duration<double> diff = end - start; std::cout << "=>Time Elapse " << diff.count() << " s" << std::endl; std::cout << "Total Length is " << defaultSendLength / 1024 / 1024 << "MB" << std::endl; std::cout << "Speed is " << defaultSendLength / 1024 / 1024/ diff.count() << "MB" << std::endl; }
以上是关于MyTtcp 测试网络带宽的主要内容,如果未能解决你的问题,请参考以下文章