IPC Performance : Socket on ARM

Test Environment

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

Test Code

  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h> 
  4. #include <netinet/in.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9.  
  10. #define CLTIP "127.0.0.1"
  11. #define SRVPORT 10172
  12. #define MAX_NUM 1024
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16. 	struct timeval tv_sta, tv_end;
  17.  
  18. 	gettimeofday(&tv_sta, NULL);
  19.  
  20. 	int i = 0;
  21. 	int total = 1;
  22. 	int is_printf = 1;
  23. 	if (argc > 1) {
  24. 		total = atoi(argv[1]);
  25. 	}
  26. 	if (argc > 2) {
  27. 		is_printf = atoi(argv[2]);
  28. 	}
  29.  
  30. 	for (i = 0; i < total; i++) {
  31.  
  32. 		int clientsock = socket(PF_INET, SOCK_STREAM, 0);
  33. 		if (clientsock < 0)
  34. 		{
  35. 			printf("socket creation failed\n");
  36. 			exit(-1);
  37. 		}
  38. 		if (is_printf)
  39. 			printf("socket create successfully.\n");
  40.  
  41. 		struct sockaddr_in clientAddr;
  42. 		clientAddr.sin_family = AF_INET;
  43. 		clientAddr.sin_port = htons((u_short)SRVPORT);
  44. 		clientAddr.sin_addr.s_addr = inet_addr(CLTIP);
  45.  
  46. 		int flags = fcntl(clientsock, F_GETFL, 0);
  47. 		fcntl(clientsock, F_SETFL, flags | O_NONBLOCK);
  48.  
  49. 		int n;
  50. 		if ((n = connect(clientsock, (struct sockaddr*) & clientAddr, sizeof(struct sockaddr))) < 0)
  51. 		{
  52. 			if (errno != EINPROGRESS) {
  53. 				printf("Connect error.IP[%s], port[%d]\n", CLTIP, clientAddr.sin_port);
  54. 				exit(-1);
  55. 			}
  56. 		}
  57.  
  58. 		if (n != 0) {
  59.  
  60. 			fd_set rset, wset;
  61. 			FD_ZERO(&rset);
  62. 			FD_SET(clientsock, &rset);
  63. 			wset = rset;
  64. 			struct timeval tval;
  65. 			tval.tv_sec = 1;
  66. 			tval.tv_usec = 0;
  67. 			if ((n = select(clientsock + 1, &rset, &wset, NULL, 3 ? &tval : NULL)) == 0) {
  68. 				printf("Connect TIMEOUT.IP[%s], port[%d]\n", CLTIP, clientAddr.sin_port);
  69. 				exit(-1);
  70. 			}
  71. 			if (FD_ISSET(clientsock, &rset) || FD_ISSET(clientsock, &wset)) {
  72. 				int error;
  73. 				int len = sizeof(error);
  74. 				if (getsockopt(clientsock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
  75. 					printf("pending error");
  76. 					exit(-1);
  77. 				}
  78. 			}
  79. 			else {
  80. 				printf("select error: clientsock not set");
  81. 				exit(-1);
  82. 			}
  83.  
  84. 		}
  85. 		fcntl(clientsock, F_SETFL, flags);
  86.  
  87. 		if (is_printf)
  88. 			printf("Connect to IP[%s], port[%d]\n", CLTIP, clientAddr.sin_port);
  89.  
  90. 		const char* sendBuf = "{\"signalType\":\"CONFIG\", \"signalData\":[{\"id\":\"CONF_NAME\", \"value\":\"PPS_MChip1\"}]}";
  91. 		char recvBuf[MAX_NUM] = { 0 };
  92.  
  93. 		//while(gets(sendBuf) != '\0')
  94. 		{
  95. 			if (send(clientsock, sendBuf, strlen(sendBuf), 0) == -1)
  96. 			{
  97. 				printf("send error!\n");
  98. 			}
  99. 			if (is_printf)
  100. 				printf("send to server:%s\n", sendBuf);
  101. 			//memset(sendBuf, 0, sizeof(sendBuf));
  102.  
  103. 			{
  104. 				int timeOut = 3000;
  105. 				int nRet = 0;
  106. 				struct timeval timeout;
  107. 				fd_set recvset;
  108. 				FD_ZERO(&recvset);
  109. 				FD_SET(clientsock, &recvset);
  110. 				timeout.tv_sec = timeOut / 1000;
  111. 				timeout.tv_usec = timeOut % 1000;
  112. 				nRet = select(clientsock + 1, &recvset, 0, 0, &timeout);
  113. 				if (nRet <= 0)
  114. 				{
  115. 					printf("clientsock recv select is error!\n");
  116. 					exit(-1);
  117. 				}
  118. 			}
  119.  
  120. 			if (recv(clientsock, recvBuf, MAX_NUM, 0) == -1)
  121. 			{
  122. 				printf("rev error!\n");
  123. 			}
  124. 			if (is_printf)
  125. 				printf("receive from server:%s\n", recvBuf);
  126. 			if (strcmp(recvBuf, "Goodbye,my dear client!") == 0)
  127. 			{
  128. 				//break;
  129. 			}
  130. 			memset(recvBuf, 0, sizeof(recvBuf));
  131. 		}
  132. 		close(clientsock);
  133.  
  134. 	}
  135.  
  136. 	gettimeofday(&tv_end, NULL);
  137.  
  138. 	int total_millisecond = (tv_end.tv_sec * 1000 + tv_end.tv_usec / 1000) - (tv_sta.tv_sec * 1000 + tv_sta.tv_usec / 1000);
  139. 	int average_millisecond = (total_millisecond / total);
  140.  
  141. 	printf("total_millisecond : %ld total_nums : %d\n", total_millisecond, total);
  142. 	printf("average_millisecond   : %ld\n", average_millisecond);
  143.  
  144. 	return 0;
  145. }

Test Result

  1. root@imx6ul7d:~/tmp# ./a.out 1 0
  2. total_millisecond : 13 total_nums : 1
  3. average_millisecond   : 13
  4. root@imx6ul7d:~/tmp# ./a.out 10 0
  5. total_millisecond : 104 total_nums : 10
  6. average_millisecond   : 10
  7. root@imx6ul7d:~/tmp# ./a.out 100 0 
  8. total_millisecond : 1023 total_nums : 100
  9. average_millisecond   : 10
  10. root@imx6ul7d:~/tmp# ./a.out 1000 0
  11. total_millisecond : 10801 total_nums : 1000
  12. average_millisecond   : 10

Leave a Reply

Your email address will not be published. Required fields are marked *