码迷,mamicode.com
首页 > 其他好文 > 详细

MyTtcp 测试网络带宽

时间:2016-12-25 11:43:30      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:分享   har   blog   long   point   复用   git   else   node   

网络编程学习

注意的指标
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 测试网络带宽

标签:分享   har   blog   long   point   复用   git   else   node   

原文地址:http://www.cnblogs.com/itdef/p/6219170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!