• 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;
    }
    

      

  • 相关阅读:
    【CF1023D】Array Restoration(构造,线段树)
    【CF1020E】Sergey's problem(构造)
    【CF1020D】The hat(交互,二分)
    【CF1017F】The Neutral Zone(Bitset,埃氏筛)
    【CF1016F】Road Projects(贪心)
    【ZOJ4063】Tournament(构造)
    EQueue
    领域驱动设计(DDD)部分核心概念的个人理解
    DDD CQRS架构和传统架构的优缺点比较
    限流算法-三种思路
  • 原文地址:https://www.cnblogs.com/itdef/p/6219170.html
Copyright © 2020-2023  润新知