Water's Home

Just another Life Style

0%

IPC Performance : Socket on ARM

Test Environment

CPU: Freescale i.MX6UL rev1.1 528 MHz DRAM: 512 MiB

Test Code

#include #include #include #include #include #include #include #include #define CLTIP “127.0.0.1”
#define SRVPORT 10172
#define MAX_NUM 1024

int main(int argc, char* argv[])
{
struct timeval tv_sta, tv_end;

gettimeofday(&tv_sta, NULL);

int i = 0;
int total = 1;
int is_printf = 1;
if (argc > 1) {
total = atoi(argv[1]);
}
if (argc > 2) {
is_printf = atoi(argv[2]);
}

for (i = 0; i < total; i++) {

int clientsock = socket(PF_INET, SOCK_STREAM, 0);
if (clientsock < 0)
{
printf(“socket creation failed\n”);
exit(-1);
}
if (is_printf)
printf(“socket create successfully.\n”);

struct sockaddr_in clientAddr;
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons((u_short)SRVPORT);
clientAddr.sin_addr.s_addr = inet_addr(CLTIP);

int flags = fcntl(clientsock, F_GETFL, 0);
fcntl(clientsock, F_SETFL, flags O_NONBLOCK);

int n;
if ((n = connect(clientsock, (struct sockaddr*) & clientAddr, sizeof(struct sockaddr))) < 0)
{
if (errno != EINPROGRESS) {
printf(“Connect error.IP[%s], port[%d]\n”, CLTIP, clientAddr.sin_port);
exit(-1);
}
}

if (n != 0) {

fd_set rset, wset;
FD_ZERO(&rset);
FD_SET(clientsock, &rset);
wset = rset;
struct timeval tval;
tval.tv_sec = 1;
tval.tv_usec = 0;
if ((n = select(clientsock + 1, &rset, &wset, NULL, 3 ? &tval : NULL)) == 0) {
printf(“Connect TIMEOUT.IP[%s], port[%d]\n”, CLTIP, clientAddr.sin_port);
exit(-1);
}
if (FD_ISSET(clientsock, &rset) FD_ISSET(clientsock, &wset)) {
int error;
int len = sizeof(error);
if (getsockopt(clientsock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
printf(“pending error”);
exit(-1);
}
}
else {
printf(“select error: clientsock not set”);
exit(-1);
}

}
fcntl(clientsock, F_SETFL, flags);

if (is_printf)
printf(“Connect to IP[%s], port[%d]\n”, CLTIP, clientAddr.sin_port);

const char* sendBuf = “{\“signalType\“:\“CONFIG\“, \“signalData\“:[{\“id\“:\“CONF_NAME\“, \“value\“:\“PPS_MChip1\“}]}”;
char recvBuf[MAX_NUM] = { 0 };

//while(gets(sendBuf) != ‘\0’)
{
if (send(clientsock, sendBuf, strlen(sendBuf), 0) == -1)
{
printf(“send error!\n”);
}
if (is_printf)
printf(“send to server:%s\n”, sendBuf);
//memset(sendBuf, 0, sizeof(sendBuf));

{
int timeOut = 3000;
int nRet = 0;
struct timeval timeout;
fd_set recvset;
FD_ZERO(&recvset);
FD_SET(clientsock, &recvset);
timeout.tv_sec = timeOut / 1000;
timeout.tv_usec = timeOut % 1000;
nRet = select(clientsock + 1, &recvset, 0, 0, &timeout);
if (nRet <= 0)
{
printf(“clientsock recv select is error!\n”);
exit(-1);
}
}

if (recv(clientsock, recvBuf, MAX_NUM, 0) == -1)
{
printf(“rev error!\n”);
}
if (is_printf)
printf(“receive from server:%s\n”, recvBuf);
if (strcmp(recvBuf, “Goodbye,my dear client!”) == 0)
{
//break;
}
memset(recvBuf, 0, sizeof(recvBuf));
}
close(clientsock);

}

gettimeofday(&tv_end, NULL);

int total_millisecond = (tv_end.tv_sec * 1000 + tv_end.tv_usec / 1000) - (tv_sta.tv_sec * 1000 + tv_sta.tv_usec / 1000);
int average_millisecond = (total_millisecond / total);

printf(“total_millisecond : %ld total_nums : %d\n”, total_millisecond, total);
printf(“average_millisecond : %ld\n”, average_millisecond);

return 0;
}

Test Result

root@imx6ul7d:/tmp# ./a.out 1 0
total_millisecond : 13 total_nums : 1
average_millisecond : 13
root@imx6ul7d:
/tmp# ./a.out 10 0
total_millisecond : 104 total_nums : 10
average_millisecond : 10
root@imx6ul7d:/tmp# ./a.out 100 0
total_millisecond : 1023 total_nums : 100
average_millisecond : 10
root@imx6ul7d:
/tmp# ./a.out 1000 0
total_millisecond : 10801 total_nums : 1000
average_millisecond : 10