📄 tcpip_bench.c
字号:
/* ==================== */
memset(&read_statistics,0,sizeof(bps_statistics_t));
memset(&send_statistics,0,sizeof(bps_statistics_t));
/* Loop for each iteration */
/* ======================= */
for (i=0;i<iterations;++i)
{
/* Trace debug */
/* ----------- */
print("Iteration %d of %lu\n",i+1,iterations);
/* Actually read the data from the server */
/* -------------------------------------- */
if (TCPIPi_Read_Data(sock_desc,data_size,packet_size)==-1)
{
print("--> Failed to read data from server, errno=%d !\n", errno);
#if defined(ST_OSLINUX)
close(sock_desc);
#else
closesocket(sock_desc);
#endif
return(TRUE);
}
/* Actually send the data to the server */
/* ------------------------------------ */
if (TCPIPi_Send_Data(sock_desc,data_size,packet_size)==-1)
{
print("--> Failed to send data to server, errno=%d !\n", errno);
#if defined(ST_OSLINUX)
close(sock_desc);
#else
closesocket(sock_desc);
#endif
return(TRUE);
}
}
/* Close the socket */
/* ================ */
#if defined(ST_OSLINUX)
close(sock_desc);
#else
closesocket(sock_desc);
#endif
/* Show the statistics */
/* =================== */
print("\n");
print("Minimum bitrates: RX: %.2lf Mbits/s TX: %.2lf Mbits/s\n" , read_statistics.wsf/1000000 , send_statistics.wsf/1000000);
print("Maximum bitrates: RX: %.2lf Mbits/s TX: %.2lf Mbits/s\n" , read_statistics.bsf/1000000 , send_statistics.bsf/1000000);
print("Average bitrates: RX: %.2lf Mbits/s TX: %.2lf Mbits/s\n\n" , read_statistics.avg/1000000 , send_statistics.avg/1000000);
/* Return no errors */
/* ================ */
return(FALSE);
}
#endif
/* ========================================================================
Name: TCPIPi_Read_Str_Token
Description: Read a token from the socket up to the specified size
======================================================================== */
#if defined(TCPIP)&&defined(TCPIP_MAX_NUMBER)
static int TCPIPi_Read_Str_Token(U32 sock_desc,char *token,U32 size)
{
char buffer;
U32 rc,i=0;
/* Loop to read all the bytes of the token */
/* --------------------------------------- */
while(i<size)
{
rc=0;
while(rc==0)
{
/* Read the next character from the socket */
#if defined(ST_OSLINUX)
rc=read(sock_desc,&buffer,1);
#else
rc=readsocket(sock_desc,&buffer,1);
#endif
if (rc==-1)
{
print("TCPIPi_Read_Str_Token():**ERROR** !!! Failed to read from socket, errno=%d !!!\n",errno);
return(-1);
}
}
/* Copy the buffer to the token */
if ((buffer=='\0')||(buffer =='\r')||(buffer=='\n'))
{
*token++='\0';
return(0);
}
/* Store byte and continue, need to wait for a return carrier byte or 0 byte */
else
{
*token++=buffer;
i++;
}
}
/* Return an error */
/* -------------- */
return(-1);
}
#endif
/* ========================================================================
Name: TCPIPi_Read_Exp_Token
Description: Read a token from the socket and compare it
======================================================================== */
#if defined(TCPIP)&&defined(TCPIP_MAX_NUMBER)
static int TCPIPi_Read_Exp_Token(U32 sock_desc,char *expected)
{
char token[80];
/* Attempt to read the token */
/* ------------------------- */
if (TCPIPi_Read_Str_Token(sock_desc,token,80)==-1)
{
print("TCPIPi_Read_Exp_Token():**ERROR** !!! Failed to read token !!!\n");
return(-1);
}
/* Compare the token read to what we expecte */
/* ----------------------------------------- */
if (strcmp (token, expected) != 0)
{
print("TCPIPi_Read_Exp_Token():**ERROR** !!! Read token '%s' but expected '%s' !!!\n",token,expected);
return(-1);
}
/* Return no errors */
/* ---------------- */
return(0);
}
#endif
/* ========================================================================
Name: TCPIPi_Send_Str_Token
Description: Write the specified string token to the specifed socket
======================================================================== */
#if defined(TCPIP)&&defined(TCPIP_MAX_NUMBER)
static int TCPIPi_Send_Str_Token(U32 sock_desc,char *token)
{
#if defined(ST_OSLINUX)
return(write(sock_desc,token,strlen(token)+1));
#else
return(writesocket(sock_desc,token,strlen(token)+1));
#endif
}
#endif
/* ========================================================================
Name: TCPIPi_Send_Int_Token
Description: Write the specified integer value to the specifed socket
======================================================================== */
#if defined(TCPIP)&&defined(TCPIP_MAX_NUMBER)
static int TCPIPi_Send_Int_Token(U32 sock_desc,U32 value)
{
char token[80];
sprintf(token,"%u",value);
return(TCPIPi_Send_Str_Token(sock_desc,token));
}
#endif
/* ========================================================================
Name: TCPIPi_Read_Data
Description: Read the specified amount of data specified in packet size
======================================================================== */
#if defined(TCPIP)&&defined(TCPIP_MAX_NUMBER)
static int TCPIPi_Read_Data(U32 sock_desc,U32 data_size,U32 packet_size)
{
U8 *buffer;
U32 start_time,todo,size,rc;
/* Output the status */
/* ----------------- */
print("\tReading %d bytes of data in %lu-byte packets...\r",data_size,packet_size);
/* Allocate a buffer to read the data into */
/* --------------------------------------- */
if ((buffer=malloc(packet_size))==NULL)
{
print("TCPIPi_Read_Data():**ERROR** !!! Failed to allocate packet buffer, errno=%d !!!\n",errno);
return(-1);
}
/* Send the 'start send' token */
/* --------------------------- */
if (TCPIPi_Send_Str_Token(sock_desc,"start send")==-1)
{
print("TCPIPi_Read_Data():**ERROR** !!! Failed to send 'start send' token, errno=%d !!!\n",errno);
free(buffer);
return(-1);
}
/* Get the current time */
/* -------------------- */
start_time=Time_Now();
/* Loop reading the data */
/* --------------------- */
for (todo=data_size;todo>0;)
{
/* Calculate how much data to process this time round */
size=(todo>packet_size)?packet_size:todo;
/* Actually read the data */
#if defined(ST_OSLINUX)
if ((rc=read(sock_desc,buffer,size))==-1)
#else
if ((rc=readsocket(sock_desc,buffer,size))==-1)
#endif
{
print("TCPIPi_Read_Data():**ERROR** !!! Failed to read datas, errno=%d !!!\n",errno);
free(buffer);
return(-1);
}
/* Subtract the amount of data read */
todo-=rc;
}
/* Update the statistics */
/* --------------------- */
TCPIPi_Update_Statistics(start_time,data_size,&read_statistics,"read");
/* Free off the buffer */
/* ------------------- */
free(buffer);
/* Return no errors */
/* ---------------- */
return(0);
}
#endif
/* ========================================================================
Name: TCPIPi_Send_Data
Description: Send the specified amount of data specified in packet size
======================================================================== */
#if defined(TCPIP)&&defined(TCPIP_MAX_NUMBER)
static int TCPIPi_Send_Data(U32 sock_desc,U32 data_size,U32 packet_size)
{
U8 *buffer;
U32 start_time,todo,size,rc;
/* Output the status */
/* ----------------- */
print("\tSending %d bytes of data in %lu-byte packets...\r",data_size,packet_size);
/* Allocate a buffer to send the data into */
/* --------------------------------------- */
if ((buffer=malloc(packet_size))==NULL)
{
print("TCPIPi_Send_Data():**ERROR** !!! Failed to allocate packet buffer, errno=%d !!!\n",errno);
return(-1);
}
/* Read the 'start send' token */
/* --------------------------- */
if (TCPIPi_Read_Exp_Token(sock_desc,"start send")==-1)
{
print("TCPIPi_Send_Data():**ERROR** !!! Failed to read 'start send' token, errno=%d !!!\n",errno);
free(buffer);
return(-1);
}
/* Get the current time */
/* -------------------- */
start_time=Time_Now();
/* Loop sending the data */
/* --------------------- */
for (todo=data_size;todo>0;)
{
/* Calculate how much data to process this time round */
size=(todo>packet_size)?packet_size:todo;
/* Actually send the data */
#if defined(ST_OSLINUX)
if ((rc=write(sock_desc,buffer,size))==-1)
#else
if ((rc=writesocket(sock_desc,buffer,size))==-1)
#endif
{
print("TCPIPi_Send_Data():**ERROR** !!! Failed to write datas, errno=%d !!!\n",errno);
free(buffer);
return(-1);
}
/* Subtract the amount of data sent */
todo-=rc;
}
/* Update the statistics */
/* --------------------- */
TCPIPi_Update_Statistics(start_time,data_size,&send_statistics,"write");
/* Free off the buffer */
/* ------------------- */
free(buffer);
/* Return no errors */
/* ---------------- */
return(0);
}
#endif
/* ========================================================================
Name: TCPIPi_Update_Statistics
Description: Update the statistics
======================================================================== */
#if defined(TCPIP)&&defined(TCPIP_MAX_NUMBER)
static void TCPIPi_Update_Statistics(U32 start_time,double data_size,bps_statistics_t *stats,char *direction)
{
double duration,bps;
/* Calculate the transfer duration in seconds */
/* ------------------------------------------ */
duration = (double)((double)Time_Now()-(double)start_time);
duration = duration/(double)ST_GetClocksPerSecond();
/* Calculate the bits per second */
/* ----------------------------- */
bps=(8.0*data_size)/duration;
/* If this is the first update then initialise bsf/wsf */
/* --------------------------------------------------- */
if (stats->iterations==0)
{
stats->bsf = bps;
stats->wsf = bps;
}
else
{
/* Check if this is the best bps */
if ((U32)bps>(U32)stats->bsf)
{
stats->bsf=bps;
}
/* Check if this is the worst bps */
if ((U32)bps<(U32)stats->wsf)
{
stats->wsf = bps;
}
}
/* Keep track of how many iterations */
/* --------------------------------- */
stats->iterations++;
/* Work out the average */
/* -------------------- */
stats->cum += bps;
stats->avg = stats->cum/stats->iterations;
/* Output the statistics for this iteration only */
/* --------------------------------------------- */
print("\tData %s in %.2f seconds at %.2lf Mbits/s (best=%.2lf Mbits/s,worst=%.2lf Mbits/s)\n",direction,duration,bps/1000000,stats->bsf/1000000,stats->wsf/1000000);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -