博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【源码剖析】Webbench —— 简洁而优美的压力测试工具
阅读量:3704 次
发布时间:2019-05-21

本文共 14120 字,大约阅读时间需要 47 分钟。

Webbench 是一个古老而著名的网站压力工具,简单而实用。如果你不清楚你的网站能承受多大的压力,或者你想分析对比两个网站的性能,webbench 再好用不过了。

安装:

     很简单,cd 进项目主页后进行 make install clean 就好了。

     用法:

     想要知道用法可以在安装后直接输入 webbench 或 webbench -h 或 webbench --help. 可以看到:

[plain]   
 
  1. webbench [option]... URL  
  2.   -f|--force               Don't wait for reply from server.  
  3.   -r|--reload              Send reload request - Pragma: no-cache.  
  4.   -t|--time <sec>          Run benchmark for <sec> seconds. Default 30.  
  5.   -p|--proxy <server:port> Use proxy server for request.  
  6.   -c|--clients <n>         Run <n> HTTP clients at once. Default one.  
  7.   -9|--http09              Use HTTP/0.9 style requests.  
  8.   -1|--http10              Use HTTP/1.0 protocol.  
  9.   -2|--http11              Use HTTP/1.1 protocol.  
  10.   --get                    Use GET request method.  
  11.   --head                   Use HEAD request method.  
  12.   --options                Use OPTIONS request method.  
  13.   --trace                  Use TRACE request method.  
  14.   -?|-h|--help             This information.  
  15.   -V|--version             Display program version.  
     说一下主要的几个选项: 指定 -f 时不等待服务器数据返回,  -t 为指定压力测试运行时间, -c 指定由多少个客户端发起测试请求。

     -9 -1 -2 分别为指定 HTTP/0.9 HTTP/1.0 HTTP/1.1。

     示例:

     访问 http://baidu.com/ , 一个客户端,持续 60 秒。

[plain]   
 
  1. ➜  webbench-1.5  webbench -t 60 http://www.baidu.com/  
  2. Webbench - Simple Web Benchmark 1.5  
  3. Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.  
  4.   
  5. Benchmarking: GET http://www.baidu.com/  
  6. 1 client, running 60 sec.  
  7.   
  8. Speed=36 pages/min, 54150 bytes/sec.  
  9. Requests: 36 susceed, 0 failed.  
    (这校园网真是 ri le gou 了)

      访问 http://baidu.com/ , 10 个客户端,持续 60 秒,socket 提前关闭。

[plain]   
 
  1. ➜  webbench-1.5  webbench -f -c 10 -t 60 http://www.baidu.com/  
  2. Webbench - Simple Web Benchmark 1.5  
  3. Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.  
  4.   
  5. Benchmarking: GET http://www.baidu.com/  
  6. 10 clients, running 60 sec, early socket close.  
  7.   
  8. Speed=586 pages/min, 0 bytes/sec.  
  9. Requests: 586 susceed, 0 failed.  

     代码分析:

     其实我把项目 clone 之后做的第一件事是把代码风格全部调整了....强迫症面对不一样的缩进和花括号都不能忍...

     项目源码都位于 socket.c 和 webbench.c 两个 c 文件里面。其他都是说明文档或用于编译。

     可能涉及知识点:命令行参数解析(getopt_long) 信号处理(sigaction) socket  管道读写。这些都可以通过 man 相关函数或者直接百度,不赘述了。

     主要函数:

     alarm_handler 信号处理函数,时钟结束时进行调用。

     usage 输出 webbench 命令用法

     main 提供程序入口...

     build_request 构造 HTTP 请求

     bench 派生子进程,父子进程管道通信最后输出计算结果。

     benchcore 每个子进程的实际发起请求函数。

      主要流程:

      说一下程序执行的主要流程:

      1. 解析命令行参数,根据命令行指定参数设定变量,可以认为是初始化配置。

      2.根据指定的配置构造 HTTP 请求报文格式。

      3.开始执行 bench 函数,先进行一次 socket 连接建立与断开,测试是否可以正常访问。

      4.建立管道,派生根据指定进程数派生子进程。

      5.每个子进程调用 benchcore 函数,先通过 sigaction 安装信号,用 alarm 设置闹钟函数,接着不断建立 socket 进行通信,与服务器交互数据,直到收到信号结束访问测试。子进程将访问测试结果写进管道。

      6.父进程读取管道数据,汇总子进程信息,收到所有子进程消息后,输出汇总信息,结束。

      流程图:
注释源码:

     socket.c:

[cpp]   
 
  1. /* $Id: socket.c 1.1 1995/01/01 07:11:14 cthuang Exp $ 
  2.  * 
  3.  * This module has been modified by Radim Kolar for OS/2 emx 
  4.  */  
  5.   
  6. /*********************************************************************** 
  7.   module:       socket.c 
  8.   program:      popclient 
  9.   SCCS ID:      @(#)socket.c    1.5  4/1/94 
  10.   programmer:   Virginia Tech Computing Center 
  11.   compiler:     DEC RISC C compiler (Ultrix 4.1) 
  12.   environment:  DEC Ultrix 4.3  
  13.   description:  UNIX sockets code. 
  14.  ***********************************************************************/  
  15.    
  16. #include <sys/types.h>  
  17. #include <sys/socket.h>  
  18. #include <fcntl.h>  
  19. #include <netinet/in.h>  
  20. #include <arpa/inet.h>  
  21. #include <netdb.h>  
  22. #include <sys/time.h>  
  23. #include <string.h>  
  24. #include <unistd.h>  
  25. #include <stdio.h>  
  26. #include <stdlib.h>  
  27. #include <stdarg.h>  
  28.   
  29. int Socket(const char *host, int clientPort)  
  30. {  
  31.     int sock;  
  32.     unsigned long inaddr;  
  33.     struct sockaddr_in ad;  
  34.     struct hostent *hp;  
  35.   
  36.     /* 初始化地址 */  
  37.     memset(&ad, 0, sizeof(ad));  
  38.     ad.sin_family = AF_INET;  
  39.   
  40.     /* 尝试把主机名转化为数字 */  
  41.     inaddr = inet_addr(host);  
  42.     if (inaddr != INADDR_NONE)  
  43.         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));  
  44.     else  
  45.     {  
  46.         /* 取得 ip 地址 */  
  47.         hp = gethostbyname(host);  
  48.         if (hp == NULL)  
  49.             return -1;  
  50.         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);  
  51.     }  
  52.     ad.sin_port = htons(clientPort);  
  53.   
  54.     /* 建立 socket */  
  55.     sock = socket(AF_INET, SOCK_STREAM, 0);  
  56.     if (sock < 0)  
  57.         return sock;  
  58.     /* 建立链接 */  
  59.     if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)  
  60.         return -1;  
  61.     return sock;  
  62. }  

     webbench.c:

[cpp]   
 
  1. /* 
  2.  * (C) Radim Kolar 1997-2004 
  3.  * This is free software, see GNU Public License version 2 for 
  4.  * details. 
  5.  * 
  6.  * Simple forking WWW Server benchmark: 
  7.  * 
  8.  * Usage: 
  9.  *   webbench --help 
  10.  * 
  11.  * Return codes: 
  12.  *    0 - sucess 
  13.  *    1 - benchmark failed (server is not on-line) 
  14.  *    2 - bad param 
  15.  *    3 - internal error, fork failed 
  16.  * 
  17.  */  
  18. #include "socket.c"  
  19. #include <unistd.h>  
  20. #include <sys/param.h>  
  21. #include <rpc/types.h>  
  22. #include <getopt.h>  
  23. #include <strings.h>  
  24. #include <time.h>  
  25. #include <signal.h>  
  26.   
  27. /* values */  
  28. volatile int timerexpired = 0;  
  29. int speed = 0;  
  30. int failed = 0;  
  31. int bytes = 0;  
  32.   
  33. /* globals */  
  34. int http10 = 1; /* 0 - http/0.9, 1 - http/1.0, 2 - http/1.1 */  
  35.   
  36. /* Allow: GET, HEAD, OPTIONS, TRACE */  
  37. #define METHOD_GET 0  
  38. #define METHOD_HEAD 1  
  39. #define METHOD_OPTIONS 2  
  40. #define METHOD_TRACE 3  
  41. #define PROGRAM_VERSION "1.5"  
  42.   
  43. /* 默认设置 */  
  44. int method = METHOD_GET; /* GET 方式 */  
  45. int clients = 1; /* 只模拟一个客户端 */  
  46. int force = 0; /* 等待响应 */  
  47. int force_reload = 0; /* 失败时重新请求 */  
  48. int proxyport = 80; /* 访问端口 */  
  49. char *proxyhost = NULL; /* 代理服务器 */  
  50. int benchtime = 30; /* 模拟请求时间 */  
  51.   
  52. /* internal */  
  53. int mypipe[2]; /* 管道 */  
  54. char host[MAXHOSTNAMELEN]; /* 网络地址 */  
  55. #define REQUEST_SIZE 2048  
  56. char request[REQUEST_SIZE]; /* 请求 */  
  57.   
  58. static const struct option long_options[]=  
  59. {  
  60.     {
    "force",no_argument,&force,1},  
  61.     {
    "reload",no_argument,&force_reload,1},  
  62.     {
    "time",required_argument,NULL,'t'},  
  63.     {
    "help",no_argument,NULL,'?'},  
  64.     {
    "http09",no_argument,NULL,'9'},  
  65.     {
    "http10",no_argument,NULL,'1'},  
  66.     {
    "http11",no_argument,NULL,'2'},  
  67.     {
    "get",no_argument,&method,METHOD_GET},  
  68.     {
    "head",no_argument,&method,METHOD_HEAD},  
  69.     {
    "options",no_argument,&method,METHOD_OPTIONS},  
  70.     {
    "trace",no_argument,&method,METHOD_TRACE},  
  71.     {
    "version",no_argument,NULL,'V'},  
  72.     {
    "proxy",required_argument,NULL,'p'},  
  73.     {
    "clients",required_argument,NULL,'c'},  
  74.     {NULL,0,NULL,0}  
  75. };  
  76.   
  77. /* prototypes */  
  78. static void benchcore(const char* host,const int port, const char *request);  
  79. static int bench(void);  
  80. static void build_request(const char *url);  
  81.   
  82. static void alarm_handler(int signal)  
  83. {  
  84.     timerexpired = 1;  
  85. }  
  86.   
  87. /* help 信息 */  
  88. static void usage(void)  
  89. {  
  90.    fprintf(stderr,  
  91.     "webbench [option]... URL\n"  
  92.     "  -f|--force               Don't wait for reply from server.\n"  
  93.     "  -r|--reload              Send reload request - Pragma: no-cache.\n"  
  94.     "  -t|--time <sec>          Run benchmark for <sec> seconds. Default 30.\n"  
  95.     "  -p|--proxy <server:port> Use proxy server for request.\n"  
  96.     "  -c|--clients <n>         Run <n> HTTP clients at once. Default one.\n"  
  97.     "  -9|--http09              Use HTTP/0.9 style requests.\n"  
  98.     "  -1|--http10              Use HTTP/1.0 protocol.\n"  
  99.     "  -2|--http11              Use HTTP/1.1 protocol.\n"  
  100.     "  --get                    Use GET request method.\n"  
  101.     "  --head                   Use HEAD request method.\n"  
  102.     "  --options                Use OPTIONS request method.\n"  
  103.     "  --trace                  Use TRACE request method.\n"  
  104.     "  -?|-h|--help             This information.\n"  
  105.     "  -V|--version             Display program version.\n"  
  106.     );  
  107. };  
  108.   
  109. int main(int argc, char *argv[])  
  110. {  
  111.     int opt = 0;  
  112.     int options_index = 0;  
  113.     char *tmp = NULL;  
  114.   
  115.     /* 不带参数时直接输出 help 信息 */  
  116.     if(argc == 1)  
  117.     {  
  118.         usage();  
  119.         return 2;  
  120.     }  
  121.   
  122.     /* getopt_long 为命令行解析的库函数,可通过 man 3 getopt_long 查看 */  
  123.     while((opt = getopt_long(argc,argv,"912Vfrt:p:c:?h",long_options,&options_index)) != EOF )  
  124.     {  
  125.         /* 如果有返回对应的命令行参数 */  
  126.         switch(opt)  
  127.         {  
  128.             case  0 : break;  
  129.             case 'f': force = 1;break;  
  130.             case 'r': force_reload = 1;break;  
  131.             case '9': http10 = 0;break;  
  132.             case '1': http10 = 1;break;  
  133.             case '2': http10 = 2;break;  
  134.             case 'V':  
  135.                       printf(PROGRAM_VERSION"\n");  
  136.                       exit(0);  
  137.             case 't':  
  138.                       benchtime = atoi(optarg);  
  139.                       break;  
  140.             case 'p':  
  141.                       /* proxy server parsing server:port */  
  142.                       tmp = strrchr(optarg,':');  
  143.                       proxyhost = optarg;  
  144.                       if(tmp == NULL)  
  145.                       {  
  146.                           break;  
  147.                       }  
  148.                       if(tmp == optarg)  
  149.                       {  
  150.                           fprintf(stderr,"Error in option --proxy %s: Missing hostname.\n",optarg);  
  151.                           return 2;  
  152.                       }  
  153.                       if(tmp == optarg + strlen(optarg)-1)  
  154.                       {  
  155.                           fprintf(stderr,"Error in option --proxy %s Port number is missing.\n",optarg);  
  156.                           return 2;  
  157.                       }  
  158.                       *tmp = '\0';  
  159.                       proxyport = atoi(tmp+1);  
  160.                       break;  
  161.             case ':':  
  162.             case 'h':  
  163.             case '?': usage();return 2;break;  
  164.             case 'c': clients = atoi(optarg);break;  
  165.         }  
  166.     }  
  167.   
  168.     /* optind 被 getopt_long 设置为命令行参数中未读取的下一个元素下标值 */  
  169.     if(optind == argc)  
  170.     {  
  171.         fprintf(stderr,"webbench: Missing URL!\n");  
  172.         usage();  
  173.         return 2;  
  174.     }  
  175.   
  176.     /* 不能指定客户端数和请求时间为 0 */  
  177.     if(clients == 0) clients = 1;  
  178.     if(benchtime == 0) benchtime = 60;  
  179.   
  180.     /* Copyright */  
  181.     fprintf(stderr,"Webbench - Simple Web Benchmark "PROGRAM_VERSION"\n"  
  182.      "Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.\n"  
  183.      );  
  184.   
  185.     /* 构造 HTTP 请求到 request 数组 */  
  186.     build_request(argv[optind]);  
  187.   
  188.     /* 以下到函数结束为输出提示信息 */  
  189.     /* print bench info */  
  190.     printf("\nBenchmarking: ");  
  191.     switch(method)  
  192.     {  
  193.         case METHOD_GET:  
  194.         default:  
  195.             printf("GET");break;  
  196.         case METHOD_OPTIONS:  
  197.             printf("OPTIONS");break;  
  198.         case METHOD_HEAD:  
  199.             printf("HEAD");break;  
  200.         case METHOD_TRACE:  
  201.             printf("TRACE");break;  
  202.     }  
  203.   
  204.     printf(" %s",argv[optind]);  
  205.     switch(http10)  
  206.     {  
  207.         case 0: printf(" (using HTTP/0.9)");break;  
  208.         case 2: printf(" (using HTTP/1.1)");break;  
  209.     }  
  210.   
  211.     printf("\n");  
  212.   
  213.     if(clients == 1) printf("1 client");  
  214.     else  
  215.         printf("%d clients",clients);  
  216.   
  217.     printf(", running %d sec", benchtime);  
  218.     if(force) printf(", early socket close");  
  219.     if(proxyhost != NULL) printf(", via proxy server %s:%d",proxyhost,proxyport);  
  220.     if(force_reload) printf(", forcing reload");  
  221.     printf(".\n");  
  222.   
  223.     /* 开始压力测试,返回 bench 函数执行结果 */  
  224.     return bench();  
  225. }  
  226.   
  227. void build_request(const char *url)  
  228. {  
  229.     char tmp[10];  
  230.     int i;  
  231.   
  232.     /* 初始化 */  
  233.     bzero(host,MAXHOSTNAMELEN);  
  234.     bzero(request,REQUEST_SIZE);  
  235.   
  236.     /* 判断应该使用的 HTTP 协议 */  
  237.     if(force_reload && proxyhost != NULL && http10 < 1) http10 = 1;  
  238.     if(method == METHOD_HEAD && http10 < 1) http10 = 1;  
  239.     if(method == METHOD_OPTIONS && http10 < 2) http10 = 2;  
  240.     if(method == METHOD_TRACE && http10 < 2) http10 = 2;  
  241.   
  242.     /*填写 method 方法 */  
  243.     switch(method)  
  244.     {  
  245.         default:  
  246.         case METHOD_GET: strcpy(request,"GET");break;  
  247.         case METHOD_HEAD: strcpy(request,"HEAD");break;  
  248.         case METHOD_OPTIONS: strcpy(request,"OPTIONS");break;  
  249.         case METHOD_TRACE: strcpy(request,"TRACE");break;  
  250.     }  
  251.   
  252.     strcat(request," ");  
  253.   
  254.     /* URL 合法性判断 */  
  255.     if(NULL == strstr(url,"://"))  
  256.     {  
  257.         fprintf(stderr, "\n%s: is not a valid URL.\n",url);  
  258.         exit(2);  
  259.     }  
  260.   
  261.     if(strlen(url)>1500)  
  262.     {  
  263.         fprintf(stderr,"URL is too long.\n");  
  264.         exit(2);  
  265.     }  
  266.   
  267.     if(proxyhost == NULL)  
  268.         if(0 != strncasecmp("http://",url,7))  
  269.         {  
  270.             /* 只支持 HTTP 地址 */  
  271.             fprintf(stderr,"\nOnly HTTP protocol is directly supported, set --proxy for others.\n");  
  272.             exit(2);  
  273.         }  
  274.   
  275.     /* 找到主机名开始的地方 */  
  276.     /* protocol/host delimiter */  
  277.     i = strstr(url,"://")-url+3;  
  278.   
  279.     /* 必须以 / 结束*/  
  280.     /* printf("%d\n",i); */  
  281.     if(strchr(url+i,'/')==NULL)  
  282.     {  
  283.         fprintf(stderr,"\nInvalid URL syntax - hostname don't ends with '/'.\n");  
  284.         exit(2);  
  285.     }  
  286.   
  287.     if(proxyhost == NULL)  
  288.     {  
  289.         /* get port from hostname */  
  290.         if(index(url+i,':') != NULL && index(url+i,':') < index(url+i,'/'))  
  291.         {  
  292.             strncpy(host,url+i,strchr(url+i,':')-url-i);  
  293.             /* 端口 */  
  294.             bzero(tmp,10);  
  295.             strncpy(tmp,index(url+i,':')+1,strchr(url+i,'/')-index(url+i,':')-1);  
  296.             /* printf("tmp=%s\n",tmp); */  
  297.             /* 设置端口 */  
  298.             proxyport = atoi(tmp);  
  299.             if(proxyport==0) proxyport=80;  
  300.   
  301.         } else {  
  302.             strncpy(host,url+i,strcspn(url+i,"/"));  
  303.         }  
  304.         // printf("Host=%s\n",host);  
  305.   
  306.         strcat(request+strlen(request),url+i+strcspn(url+i,"/"));  
  307.     } else {  
  308.         // printf("ProxyHost=%s\nProxyPort=%d\n",proxyhost,proxyport);  
  309.         strcat(request,url);  
  310.     }  
  311.   
  312.     if(http10 == 1)  
  313.         strcat(request," HTTP/1.0");  
  314.     else if (http10==2)  
  315.         strcat(request," HTTP/1.1");  
  316.     strcat(request,"\r\n");  
  317.   
  318.     if(http10 > 0)  
  319.         strcat(request,"User-Agent: WebBench "PROGRAM_VERSION"\r\n");  
  320.   
  321.     if(proxyhost == NULL && http10 > 0)  
  322.     {  
  323.         strcat(request,"Host: ");  
  324.         strcat(request,host);  
  325.         strcat(request,"\r\n");  
  326.     }  
  327.   
  328.     if(force_reload && proxyhost != NULL)  
  329.     {  
  330.         strcat(request,"Pragma: no-cache\r\n");  
  331.     }  
  332.   
  333.     if(http10 > 1)  
  334.         strcat(request,"Connection: close\r\n");  
  335.   
  336.     /* add empty line at end */  
  337.     if(http10>0) strcat(request,"\r\n");  
  338.     // printf("Req=%s\n",request);  
  339. }  
  340.   
  341. /* vraci system rc error kod */  
  342. static int bench(void)  
  343. {  
  344.     int i,j,k;  
  345.     pid_t pid = 0;  
  346.     FILE *f;  
  347.   
  348.     /* 作为测试地址是否合法 */  
  349.     /* check avaibility of target server */  
  350.     i = Socket(proxyhost == NULL?host:proxyhost, proxyport);  
  351.     if(i < 0){  
  352.         fprintf(stderr,"\nConnect to server failed. Aborting benchmark.\n");  
  353.         return 1;  
  354.     }  
  355.     close(i);  
  356.   
  357.     /* 建立管道 */  
  358.     /* create pipe */  
  359.     if(pipe(mypipe))  
  360.     {  
  361.         perror("pipe failed.");  
  362.         return 3;  
  363.     }  
  364.   
  365.     /* not needed, since we have alarm() in childrens */  
  366.     /* wait 4 next system clock tick */  
  367.     /* 
  368.      * cas=time(NULL); 
  369.      * while(time(NULL)==cas) 
  370.      * sched_yield(); 
  371.      * */  
  372.   
  373.     /* 派生子进程 */  
  374.     /* fork childs */  
  375.     for(i = 0;i < clients;i++)  
  376.     {  
  377.         pid = fork();  
  378.         if(pid <= (pid_t)0)  
  379.         {  
  380.             /* child process or error*/  
  381.             sleep(1); /* make childs faster */  
  382.             break/* 子进程立刻跳出循环,要不就子进程继续 fork 了 */  
  383.         }  
  384.     }  
  385.   
  386.     if( pid < (pid_t)0)  
  387.     {  
  388.         fprintf(stderr,"problems forking worker no. %d\n",i);  
  389.         perror("fork failed.");  
  390.         return 3;  
  391.     }  
  392.   
  393.     if(pid == (pid_t)0)  
  394.     {  
  395.         /* 子进程发出实际请求 */  
  396.         /* I am a child */  
  397.         if(proxyhost == NULL)  
  398.             benchcore(host,proxyport,request);  
  399.         else  
  400.             benchcore(proxyhost,proxyport,request);  
  401.   
  402.         /* 打开管道写 */  
  403.         /* write results to pipe */  
  404.         f = fdopen(mypipe[1],"w");  
  405.         if(f == NULL)  
  406.         {  
  407.             perror("open pipe for writing failed.");  
  408.             return 3;  
  409.         }  
  410.   
  411.         /* fprintf(stderr,"Child - %d %d\n",speed,failed); */  
  412.         fprintf(f,"%d %d %d\n",speed,failed,bytes);  
  413.         fclose(f);  
  414.   
  415.         return 0;  
  416.   
  417.     } else {  
  418.         /* 父进程打开管道读 */  
  419.         f = fdopen(mypipe[0],"r");  
  420.         if(f == NULL)  
  421.         {  
  422.             perror("open pipe for reading failed.");  
  423.             return 3;  
  424.         }  
  425.   
  426.         setvbuf(f,NULL,_IONBF,0);  
  427.         speed = 0; /* 传输速度 */  
  428.         failed = 0; /* 失败请求数 */  
  429.         bytes = 0; /* 传输字节数 */  
  430.   
  431.         while(1)  
  432.         {  
  433.             pid = fscanf(f,"%d %d %d",&i,&j,&k);  
  434.             if(pid<2)  
  435.             {  
  436.                 fprintf(stderr,"Some of our childrens died.\n");  
  437.                 break;  
  438.             }  
  439.             speed += i;  
  440.             failed += j;  
  441.             bytes += k;  
  442.             /* fprintf(stderr,"*Knock* %d %d read=%d\n",speed,failed,pid); */  
  443.             /* 子进程是否读取完 */  
  444.             if(--clients == 0) break;  
  445.         }  
  446.         fclose(f);  
  447.   
  448.         /* 结果计算 */  
  449.         printf("\nSpeed=%d pages/min, %d bytes/sec.\nRequests: %d susceed, %d failed.\n",  
  450.                 (int)((speed+failed)/(benchtime/60.0f)),  
  451.                 (int)(bytes/(float)benchtime),  
  452.                 speed,  
  453.                 failed);  
  454.     }  
  455.     return i;  
  456. }  
  457.   
  458. void benchcore(const char *host,const int port,const char *req)  
  459. {  
  460.     int rlen;  
  461.     char buf[1500];  
  462.     int s,i;  
  463.     struct sigaction sa;  
  464.   
  465.     /*安装信号 */  
  466.     /* setup alarm signal handler */  
  467.     sa.sa_handler = alarm_handler;  
  468.     sa.sa_flags = 0;  
  469.     if(sigaction(SIGALRM,&sa,NULL))  
  470.         exit(3);  
  471.   
  472.     /* 设置闹钟函数 */  
  473.     alarm(benchtime);  
  474.   
  475.     rlen = strlen(req);  
  476.   
  477. nexttry:  
  478.     while(1){  
  479.         /* 收到信号则 timerexpired = 1 */  
  480.         if(timerexpired)  
  481.         {  
  482.             if(failed > 0)  
  483.             {  
  484.                 /* fprintf(stderr,"Correcting failed by signal\n"); */  
  485.                 failed--;  
  486.             }  
  487.             return;  
  488.         }  
  489.         /* 建立 socket, 进行 HTTP 请求 */  
  490.         s = Socket(host,port);  
  491.         if(s < 0)  
  492.         {  
  493.             failed++;  
  494.             continue;  
  495.         }  
  496.         if(rlen!=write(s,req,rlen))  
  497.         {  
  498.             failed++;  
  499.             close(s);  
  500.             continue;  
  501.         }  
  502.         /* HTTP 0.9 的处理 */  
  503.         if(http10==0)  
  504.             /* 如果关闭不成功 */  
  505.             if(shutdown(s,1))  
  506.             {  
  507.                 failed++;  
  508.                 close(s);  
  509.                 continue;  
  510.             }  
  511.   
  512.         /* -f 选项时不读取服务器回复 */  
  513.         if(force == 0)  
  514.         {  
  515.             /* read all available data from socket */  
  516.             while(1)  
  517.             {  
  518.                 if(timerexpired) break;  
  519.                 i = read(s,buf,1500);  
  520.                 /* fprintf(stderr,"%d\n",i); */  
  521.                 if(i<0)  
  522.                 {  
  523.                     failed++;  
  524.                     close(s);  
  525.                     goto nexttry;  
  526.                 }  
  527.                 else  
  528.                     if(i == 0) break;  
  529.                     else bytes+=i;  
  530.             }  
  531.         }  
  532.         if(close(s))  
  533.         {  
  534.             failed++;  
  535.             continue;  
  536.         }  
  537.         speed++;  
  538.     }  
  539. }  
你可能感兴趣的文章
mouse冒泡事件
查看>>
forEach、some和every关于提前退出
查看>>
改变input (radio,check)的样式
查看>>
vue单选、多选切换,v-model字符串、数组切换
查看>>
滑动鼠标,固定导航栏一直闪烁
查看>>
奇怪!为什么@media起作用,而@media screen and不起作用
查看>>
顶部固定导航栏随着横向滚动条的滚动一起运动
查看>>
jquery获得滚动距离的方法
查看>>
js获取当前点击的元素
查看>>
为什么js代码放在head中报错,放在body中却能正常调用
查看>>
jquery中text()、html()、val()的区别
查看>>
swiper导致图片文字变模糊
查看>>
当图片被压缩时,图片变得模糊
查看>>
js外部访问局部变量
查看>>
两栏布局的几种方式
查看>>
css实现旋转、放大
查看>>
jQuery常用方法总结
查看>>
js实现正则去除字符串空格
查看>>
css隐藏自带的滚动轴,兼容IE,FF,Webkit 和 O
查看>>
form表单提交的几种方式
查看>>