⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpperftest.cpp

📁 EP9315开发板的Wince6.0的BSP包文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			totalBytes += cbTotalTx;
		}

	  blocksLeft--;
	}
	endtime = GetTickCount( );
	endIdleTime=GetIdleTime();

	while ( blocksLeft > 0)
	{
        cbTotalTx = 0;
        do
		{
          cbXfer = sendto(sock, pBuf+cbTotalTx, BUFFER_SIZE-cbTotalTx, 0, 
            AI->ai_addr, AI->ai_addrlen);
		  if(cbXfer == SOCKET_ERROR)
		  {
            printf("ERROR: S4 sendto() Error=%d cbXfer=%d\r\n", WSAGetLastError(), cbXfer);
		   break;
		  }   
          cbTotalTx += cbXfer;
		} while(cbXfer > 0 && cbTotalTx<BUFFER_SIZE);

        if(cbTotalTx != BUFFER_SIZE)
		{
          printf("ERROR: S5 cbTotalTx=%d\r\n", cbTotalTx);
          break;
		} 
	
	    blocksLeft--;
	} //end for

    status=0;
	timeDiff=endtime-starttime;
	timeDiffSec = timeDiff/1000.0;
	TotalBit= totalBytes*8.0;
	MBitPerSec = (TotalBit/timeDiffSec)/1048576.0;

	idleDiff=endIdleTime - startIdleTime;
	idleDiffSec = idleDiff/1000.0;
	cpuUsage=((timeDiffSec*100.0)-(idleDiffSec*100.0))/timeDiffSec;

	printf("\r\n**************** Transmit Performance Test ********************\r\n");
	printf("starttime=%u msec, endtime=%u msec\r\n", starttime, endtime);
    printf("Time in msec = %u  Time in sec= %8.3f\r\n", timeDiff, timeDiffSec);
    printf("SUCCESS - Transmitted %u bytes to address %hs\r\n", totalBytes, server_addr);
	printf("MBitPerSec=%8.3f MBitPerSec \r\n\r\n", MBitPerSec);

    printf("startIdleTime=%u, endIdleTime=%u, idleDiffSec=%4.3f \r\n", startIdleTime, endIdleTime, idleDiffSec);
	printf("CPU Usage =%5.2f%% \r\n", cpuUsage);
	printf("\r\n*************** End of Transmit Performance Test ****************\r\n");


	return(status);
}

//*****************************************************************************
//*
//*  Do_PingPong_Perf_Test()
//*
//*****************************************************************************
int Do_PingPong_Perf_Test(void)
{
	unsigned long totalBytes=0;
	int i;
	unsigned long starttime, endtime;
	unsigned long timeDiff;
	double MBitPerSec, timeDiffSec, TotalBit;
    int cbXfer, cbTotalTx, cbTotalRecvd, cbRemoteAddrSize;
    SOCKADDR_STORAGE ssRemoteAddr;
	int status=-1;
	unsigned long startIdleTime, endIdleTime, idleDiff;
	double idleDiffSec, cpuUsage;

	printf("\r\nDoing TCP Ping-Pong Performance Test.... Packet size=%d \r\n", PINGPONG_FRAME_SIZE);

	cbRemoteAddrSize = sizeof(ssRemoteAddr);

    totalBytes=0;
	starttime = GetTickCount( );
	startIdleTime=GetIdleTime();

	for (i=1; i<PINGPONG_LOOP_COUNT; i++)
	{

		//Do Tx
        cbTotalTx = 0;
        do
		{
          cbXfer = sendto(sock, pBuf+cbTotalTx, PINGPONG_FRAME_SIZE-cbTotalTx, 0, 
            AI->ai_addr, AI->ai_addrlen);
		  if(cbXfer == SOCKET_ERROR)
		  {
            printf("ERROR: S2 sendto() Error=%d cbXfer=%d\r\n", WSAGetLastError(), cbXfer);
		   break;
		  }   
          cbTotalTx += cbXfer;
		} while(cbXfer > 0 && cbTotalTx<PINGPONG_FRAME_SIZE);

        if(cbTotalTx != PINGPONG_FRAME_SIZE)
		{
          printf("ERROR: S6 cbTotalTx=%d\r\n", cbTotalTx);
          break;
		} else {
			totalBytes += cbTotalTx;
		}
		 


        //Do Receive
        cbTotalRecvd = 0;
        do
		{
          cbXfer = recvfrom(sock, pBuf+cbTotalRecvd, PINGPONG_FRAME_SIZE-cbTotalRecvd, 0, 
            (SOCKADDR *)&ssRemoteAddr, &cbRemoteAddrSize);
		  if(cbXfer == SOCKET_ERROR)
		  {
            printf("ERROR: S2 recvfrom() Error=%d cbXfer=%d\r\n", WSAGetLastError(), cbXfer);
		   break;
		  }   
          cbTotalRecvd += cbXfer;
		} while(cbXfer > 0 && cbTotalRecvd<PINGPONG_FRAME_SIZE);

        if(cbTotalRecvd != PINGPONG_FRAME_SIZE)
		{
          printf("ERROR: S5 cbTotalRecvd=%d\r\n", cbTotalRecvd);
          break;
		} else {
			totalBytes += cbTotalRecvd;
		} 
	
	} //end for

	endtime = GetTickCount( );
	endIdleTime=GetIdleTime();


    status=0;

	timeDiff=endtime-starttime;
	timeDiffSec = timeDiff/1000.0;
	TotalBit= totalBytes*8.0;
	MBitPerSec = (TotalBit/timeDiffSec)/1048576.0;

	idleDiff=endIdleTime - startIdleTime;
	idleDiffSec = idleDiff/1000.0;
	cpuUsage=((timeDiffSec*100.0)-(idleDiffSec*100.0))/timeDiffSec;

	printf("\r\n**************** PingPong Performance Test ********************\r\n");
	printf("starttime=%u msec, endtime=%u msec\r\n", starttime, endtime);
    printf("Time in msec = %u  Time in sec= %8.3f\r\n", timeDiff, timeDiffSec);
    printf("SUCCESS - Ping-Pong %u bytes to address %hs\r\n", totalBytes, server_addr);
	printf("MBitPerSec=%8.3f MBitPerSec \r\n\r\n", MBitPerSec);

    printf("startIdleTime=%u. endIdleTime=%u, idleDiffSec=%4.3f \r\n", startIdleTime, endIdleTime, idleDiffSec);
	printf("CPU Usage=%5.2f%% \r\n\r\n", cpuUsage);
	printf("\r\n************** End of PingPong Performance Test ****************\r\n");

	return(status);
}

//*****************************************************************************
//*
//*  main()
//*
//*****************************************************************************
int main(int argc,char *argv[], char *envp[])
{
    int nFamily = DEFAULT_FAMILY;
    int nSockType = DEFAULT_SOCKTYPE;
    char szRemoteName[64];
    char *szPort = DEFAULT_PORT;
    int cbXfer;
    WSADATA wsaData;
    ADDRINFO Hints, *AddrInfo = NULL;
    char szRemoteAddrString[128];
    fd_set fdReadSet;
    TIMEVAL timeout = {TIMEOUT_SECS, TIMEOUT_USECS};
	int status;

	status=parse_arguments(argc, argv);
	if (status != 0)
	{
      print_usage();
	  return -1;
	}

	memset(szRemoteName, 0, 64);
	memcpy((char *)szRemoteName, (char *)&server_addr[0], strlen(server_addr));
    szRemoteName[63] = _T('\0');
    printf("Will Communicate with server - %s\r\n", szRemoteName);
 
	if(WSAStartup(MAKEWORD(2,2), &wsaData))
    {
        // WSAStartup failed
	    printf("WSAStartup() Failed\r\n");
        return 1;
    }

    //
    // Resolve the server name/address
    //

    memset(&Hints, 0, sizeof(Hints));
    Hints.ai_family = nFamily;
    Hints.ai_socktype = nSockType;

    if(getaddrinfo(szRemoteName, szPort, &Hints, &AddrInfo))
    {
        printf("ERROR: Couldn't get resolve the server name/address!\r\n");
        goto Cleanup;
    }

    //
    // Attempt to connect to each address until we find one that succeeds
    //

    for(AI = AddrInfo; AI != NULL; AI = AI->ai_next) 
    {
        if(AI->ai_family == PF_INET) // only want PF_INET or PF_INET6
        {
            sock = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
            if (sock != INVALID_SOCKET)
            {
                if (AI->ai_socktype == SOCK_STREAM)
                {
                    if(connect(sock, AI->ai_addr, AI->ai_addrlen) == SOCKET_ERROR)
                    {
                        // Connect failed, let's close this socket and try again on the next address in the list
                        closesocket(sock);
                        continue;
                    } else {
                        printf("Successfully connected to Server. \r\n");
					}
                }

                // connect() succeeded or we're a UDP socket
                break;
            }
        }
    }
    

    if (AI == NULL) 
    {
        printf("ERROR: Unable to connect to any of the server's addresses!\r\n");
        goto Cleanup;
    }

    //
    // Send data to the server
    //

    cbXfer = 0;
    cbXfer = sendto(sock, pBuf, 1024, 0, AI->ai_addr, AI->ai_addrlen);

    if(cbXfer != 1024)
    {
        printf("ERROR: S1 sendto() error=%d cbXfer=%d\r\n", WSAGetLastError(), cbXfer);
        goto Cleanup;
    }

    if (getnameinfo(AI->ai_addr, AI->ai_addrlen,
        szRemoteAddrString, sizeof(szRemoteAddrString), NULL, 0, NI_NUMERICHOST) != 0)
        strcpy(szRemoteAddrString, "");
    

    printf("SUCCESS - Sent %d bytes to address %hs\r\n", cbXfer, szRemoteAddrString);

    //
    // Receive the  data back from the server
    //

    FD_ZERO(&fdReadSet);
    FD_SET(sock, &fdReadSet);

    if(select(0, &fdReadSet, NULL, NULL, &timeout) != 1)
    {
        printf("ERROR: Server hasn't responded in %d milliseconds\r\n", 
            ((timeout.tv_sec * 1000) + (timeout.tv_sec / 1000)));
        goto Cleanup;
    }


	status=Do_Rx_Perf_Test();
	if (status == -1)
	{
		goto Cleanup;
	}

	status=Do_Tx_Perf_Test();
	if (status == -1)
	{
		goto Cleanup;
	}

	status=Do_PingPong_Perf_Test();
	if (status == -1)
	{
		goto Cleanup;
	}


Cleanup:

    if(sock != INVALID_SOCKET)
    {
        shutdown(sock, SD_BOTH);
        closesocket(sock);
    }

    if(AddrInfo)
        freeaddrinfo(AddrInfo);

    WSACleanup();

    return 0;
} 
 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -