- 论坛徽章:
- 0
|
使用的操作系统分别是
FreeBSD 4.10
NetBSD 2.0
Slackware 10.0
环境:
vmware-4.5.2,每个虚拟机均分配4G硬盘和48M内存。
三次测试结果(s):
FreeBSD 10. 583041 10.460132 10.526190
NetBSD 4.584173 4.739391 4.557778
Slackware 2.104959 2.103009 2.131719
结论:
Linux >; NetBSD >; FreeBSD
测试方法(220.181.30.11是网易的主机):
$./nonblock 220.181.30.11 80 1000000
测试程序:
- // nonblock.cpp
- #include <sys/types.h>;
- #include <sys/socket.h>;
- #include <unistd.h>;
- #include <stdio.h>;
- #include <arpa/inet.h>;
- #include <netinet/in.h>;
- #include <stdlib.h>;
- #include <string.h>;
- #include <fcntl.h>;
- #include <sys/time.h>;
- void tv_sub(timeval *out, timeval *in)
- {
- if (out->;tv_usec < in->;tv_usec) {
- out->;tv_usec += 1000000;
- out->;tv_usec -= in->;tv_usec;
- out->;tv_sec--;
- } else {
- out->;tv_usec -= in->;tv_usec;
- }
- out->;tv_sec -= in->;tv_sec;
- }
- int main(int argc, char *argv[])
- {
- if (argc != 4) {
- printf("Usage: %s <ip>; <port>; <count>;\n", argv[0]);
- return -1;
- }
- const char *host = argv[1];
- unsigned short port = atoi(argv[2]);
- int count = atoi(argv[3]);
- sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(host);
- addr.sin_port = htons(port);
- int fd = socket(AF_INET, SOCK_STREAM, 0);
- if (fd == -1) {
- printf("Failed to create socket!\n");
- return -1;
- }
-
- int flag = fcntl(fd, F_GETFL, 0);
- fcntl(fd, F_SETFL, flag | O_NONBLOCK);
-
-
- connect(fd, (sockaddr*)&addr, sizeof(addr));
-
- fd_set rset, wset;
- FD_ZERO(&rset);
- FD_SET(fd, &rset);
- wset = rset;
- select(fd + 1, &rset, &wset, NULL, NULL);
- if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
- printf("%s is connected!\n", host);
- timeval begin, end;
- gettimeofday(&begin, NULL);
- char buf[1024];
- for (int i = 0; i < count; i++) {
- read(fd, buf, sizeof(buf));
- }
- gettimeofday(&end, NULL);
- tv_sub(&end, &begin);
- printf("time escaped: %d.%06d\n", end.tv_sec, end.tv_usec);
- }
- close(fd);
- return 0;
- }
复制代码 |
|