Webbench
Webbench是一个在linux下使用的非常简单的网站压测工具。它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接去测试网站的负载能力。Webbench使用C语言编写, 代码实在太简洁,源码加起来不到600行。整个源码分析主要的分析都被我加在了代码的后面中文注释中,欢迎讨论~
webbench压测的命令:
webbench -c 300 -t 10 url
其中:-c  300 表示并发数(可以了理解成客户端),
        -t   10表示时间(秒)
        url   想要压测的url
下载链接:http://home.tiscali.cz/~cz210552/webbench.html
 
整个代码的流程图如下:

一、首先我们从主函数入手,前面几个都是初始化变量,没什么好说的,出现了一个getopt_long函数,应该有些人没有用过这个函数,我先来分析下这个函数吧~
  int getopt_long(int argc, char * const argv[],const char *optstring,const struct option *longopts, int *longindex);
  
  1、前两个参数,就是main函数的argc和argv,这两者直接传入即可,
  2、optstring的格式举例说明比较方便,例如:char *optstring = "abcd:";
上面这个optstring在传入之后,getopt函数将依次检查命令行是否指定了 -a, -b, -c及 -d,(这需要多次调用getopt函数,直到其返回-1),当检查到上面某一个参数被指定时,函数会返回被指定的参数名称(即该字母),最后一个参数d后面带有冒号,: 表示参数d是可以指定值的,如 -d 100 或 -d user。
  3、longopts指向的是一个由option结构体组成的数组,那个数组的每个元素,指明了一个“长参数”(即形如--name的参数)名称和性质:
           struct option {
               const char *name;
               int         has_arg;
               int        *flag;
               int         val;
           };
       name  是参数的名称
       has_arg 指明是否带参数值,其数值可选:
              no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name)
              required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name Bob)
              optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
       flag   当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0
       val    用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。
 
- static const struct option long_options[]=  
 
- {  
 
-  {"force",no_argument,&force,1},  
 
-  {"reload",no_argument,&force_reload,1},  
 
-  {"time",required_argument,NULL,‘t‘},               
 
-  {"help",no_argument,NULL,‘?‘},  
 
-  {"http09",no_argument,NULL,‘9‘},  
 
-  {"http10",no_argument,NULL,‘1‘},  
 
-  {"http11",no_argument,NULL,‘2‘},  
 
-  {"get",no_argument,&method,METHOD_GET},  
 
-  {"head",no_argument,&method,METHOD_HEAD},  
 
-  {"options",no_argument,&method,METHOD_OPTIONS},  
 
-  {"trace",no_argument,&method,METHOD_TRACE},  
 
-  {"version",no_argument,NULL,‘V‘},  
 
-  {"proxy",required_argument,NULL,‘p‘},  
 
-  {"clients",required_argument,NULL,‘c‘},  
 
-  {NULL,0,NULL,0}  
 
- };  
 
 
4、option_index指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。
 
例如对于
- while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)    
 
-    {    
 
-         printf("opt = %c\n", opt);           
 
-         printf("optarg = %s\n", optarg);     
 
-         printf("optind = %d\n", optind);          
 
-         printf("argv[optind - 1] = %s\n",  argv[optind - 1]);    
 
-         printf("option_index = %d\n", option_index);          
 
-    }    
 
 
输入命令行test_getopt_long  -reqarg 100
 
输出:
opt = reqarg 
optarg = 100  
optind = 3  
argv[optind - 1] = 100  
 
二、build_request函数
目的是对url进行处理,得到host,proxyport,request
其中request就是之后利用socket与host通信所要发送的报文。
 
- void build_request(const char *url)  
 
- {  
 
-   char tmp[10];  
 
-   int i;  
 
-   
 
-   bzero(host,MAXHOSTNAMELEN);    
 
-   bzero(request,REQUEST_SIZE);  
 
-   
 
-   if(force_reload && proxyhost!=NULL && http10<1) http10=1;  
 
-   if(method==METHOD_HEAD && http10<1) http10=1;  
 
-   if(method==METHOD_OPTIONS && http10<2) http10=2;  
 
-   if(method==METHOD_TRACE && http10<2) http10=2;  
 
-   
 
-   switch(method)  
 
-   {  
 
-       default:  
 
-       case METHOD_GET: strcpy(request,"GET");break;  
 
-       case METHOD_HEAD: strcpy(request,"HEAD");break;  
 
-       case METHOD_OPTIONS: strcpy(request,"OPTIONS");break;  
 
-       case METHOD_TRACE: strcpy(request,"TRACE");break;  
 
-   }  
 
-             
 
-   strcat(request," ");  
 
-   
 
-   if(NULL==strstr(url,"://"))  
 
-   {  
 
-       fprintf(stderr, "\n%s: is not a valid URL.\n",url);  
 
-       exit(2);  
 
-   }  
 
-   if(strlen(url)>1500)  
 
-   {  
 
-          fprintf(stderr,"URL is too long.\n");  
 
-      exit(2);  
 
-   }  
 
-   if(proxyhost==NULL)  
 
-        if (0!=strncasecmp("http://",url,7))   
 
-        {   
 
-              fprintf(stderr,"\nOnly HTTP protocol is directly supported, set --proxy for others.\n");  
 
-              exit(2);  
 
-            }  
 
-   
 
-   i=strstr(url,"://")-url+3;                              //找到url中:‘//‘的出现的位置  
 
-   
 
-   
 
-   if(strchr(url+i,‘/‘)==NULL) {                                                                                     
 
-             fprintf(stderr,"\nInvalid URL syntax - hostname don‘t ends with ‘/‘.\n");  
 
-             exit(2);  
 
-                               }  
 
-   if(proxyhost==NULL)                                                                                       
 
-   {                                                                                                         
 
-    
 
-    if(index(url+i,‘:‘)!=NULL &&  
 
-       index(url+i,‘:‘)<index(url+i,‘/‘))                                                   
 
-    {  
 
-        strncpy(host,url+i,strchr(url+i,‘:‘)-url-i);        
 
-                                   
 
-        bzero(tmp,10);  
 
-        strncpy(tmp,index(url+i,‘:‘)+1,strchr(url+i,‘/‘)-index(url+i,‘:‘)-1);  
 
-        
 
-        proxyport=atoi(tmp);  
 
-        if(proxyport==0) proxyport=80;  
 
-    } else  
 
-    {  
 
-      strncpy(host,url+i,strcspn(url+i,"/"));  
 
-    }  
 
-    
 
-    strcat(request+strlen(request),url+i+strcspn(url+i,"/"));  
 
-   } else  
 
-   {  
 
-    
 
-    strcat(request,url);  
 
-   }  
 
-   if(http10==1)  
 
-       strcat(request," HTTP/1.0");  
 
-   else if (http10==2)  
 
-       strcat(request," HTTP/1.1");  
 
-   strcat(request,"\r\n");  
 
-   if(http10>0)  
 
-       strcat(request,"User-Agent: WebBench "PROGRAM_VERSION"\r\n");  
 
-   if(proxyhost==NULL && http10>0)  
 
-   {  
 
-       strcat(request,"Host: ");  
 
-       strcat(request,host);  
 
-       strcat(request,"\r\n");  
 
-   }  
 
-   if(force_reload && proxyhost!=NULL)  
 
-   {  
 
-       strcat(request,"Pragma: no-cache\r\n");  
 
-   }  
 
-   if(http10>1)  
 
-       strcat(request,"Connection: close\r\n");  
 
-   
 
-   if(http10>0) strcat(request,"\r\n");   
 
-   
 
- }  
 
 
 
三、bench函数  
该函数主要采用fork出子进程来测试网站,并且利用主进程来读取所有子进程写入的数据,每个子进程调用benchcore函数来测试存到全局变量speed faulted,
最后主进程汇总各个子线程的数据显示出来~
 
- static int bench(void)  
 
- {  
 
-   int i,j,k;      
 
-   pid_t pid=0;  
 
-   FILE *f;  
 
-   
 
-   
 
-   i=Socket(proxyhost==NULL?host:proxyhost,proxyport);       
 
-   if(i<0) {   
 
-        fprintf(stderr,"\nConnect to server failed. Aborting benchmark.\n");  
 
-            return 1;  
 
-          }  
 
-   close(i);  
 
-   
 
-   if(pipe(mypipe))  
 
-   {  
 
-       perror("pipe failed.");  
 
-       return 3;  
 
-   }  
 
-   
 
-   
 
-   
 
-   
 
-   
 
-   
 
-   for(i=0;i<clients;i++)  
 
-   {  
 
-        pid=fork();     
 
-                       
 
-                       
 
-        if(pid <= (pid_t) 0)  
 
-        {  
 
-            
 
-                sleep(1); 
 
-            break;  
 
-        }  
 
-   }  
 
-   
 
-   if( pid< (pid_t) 0)  
 
-   {  
 
-           fprintf(stderr,"problems forking worker no. %d\n",i);  
 
-       perror("fork failed.");  
 
-       return 3;  
 
-   }  
 
-   
 
-   if(pid== (pid_t) 0)  
 
-   {  
 
-     
 
-     if(proxyhost==NULL)  
 
-       benchcore(host,proxyport,request);          
 
-          else  
 
-       benchcore(proxyhost,proxyport,request);  
 
-   
 
-          
 
-      f=fdopen(mypipe[1],"w");  
 
-      if(f==NULL)  
 
-      {  
 
-          perror("open pipe for writing failed.");  
 
-          return 3;  
 
-      }  
 
-      
 
-      fprintf(f,"%d %d %d\n",speed,failed,bytes);        
 
-      fclose(f);  
 
-      return 0;  
 
-   } else  
 
-   {  
 
-       f=fdopen(mypipe[0],"r");  
 
-       if(f==NULL)   
 
-       {  
 
-           perror("open pipe for reading failed.");  
 
-           return 3;  
 
-       }  
 
-       setvbuf(f,NULL,_IONBF,0);        
 
-                       
 
-                       
 
-       speed=0;  
 
-           failed=0;  
 
-           bytes=0;  
 
-   
 
-       while(1)  
 
-       {  
 
-           pid=fscanf(f,"%d %d %d",&i,&j,&k);        
 
-           if(pid<2)  
 
-                   {  
 
-                        fprintf(stderr,"Some of our childrens died.\n");  
 
-                        break;  
 
-                   }  
 
-           speed+=i;  
 
-           failed+=j;  
 
-           bytes+=k;  
 
-           
 
-           if(--clients==0) break;            
 
-       }  
 
-       fclose(f);  
 
-   
 
-   printf("\nSpeed=%d pages/min, %d bytes/sec.\nRequests: %d susceed, %d failed.\n",  
 
-           (int)((speed+failed)/(benchtime/60.0f)),  
 
-           (int)(bytes/(float)benchtime),  
 
-           speed,  
 
-           failed);                              
 
-   }  
 
-   return i;  
 
- }  
 
 
 
 
四、benchcore函数  
该函数主要采用socket连接、发送request、接收来测试网站,测试结果存在全局变量speed faulted,bytes
定时时间结束则退出函数~
其中关于sigaction函数的使用:
int sigaction(int signo,const struct sigaction *restrict act,struct sigaction *restrict oact);
其中signo的信息可参考:http://blog.csdn.net/liucimin/article/details/40507443
其中结构sigaction定义如下:
 
- struct sigaction{  
 
-   void (*sa_handler)(int);  
 
-    sigset_t sa_mask;  
 
-   int sa_flag;  
 
-   void (*sa_sigaction)(int,siginfo_t *,void *);  
 
- };   
 
 
sa_handler字段包含一个信号捕捉函数的地址
sa_flag标志。
 
 
- void benchcore(const char *host,const int port,const char *req)  
 
- {  
 
-  int rlen;  
 
-  char buf[1500];  
 
-  int s,i;  
 
-    
 
-   
 
-  struct sigaction sa;  
 
-   
 
-  
 
-  sa.sa_handler=alarm_handler;  
 
-  sa.sa_flags=0;  
 
-  if(sigaction(SIGALRM,&sa,NULL))                    
 
-     exit(3);  
 
-  alarm(benchtime);                                  
 
-                                                     
 
-                                                     
 
-   
 
-  rlen=strlen(req);  
 
-  nexttry:while(1)  
 
-  {  
 
-     if(timerexpired)                            
 
-     {  
 
-        if(failed>0)  
 
-        {  
 
-           
 
-           failed--;  
 
-        }  
 
-        return;  
 
-     }  
 
-     s=Socket(host,port);                          
 
-     if(s<0) { failed++;continue;}   
 
-     if(rlen!=write(s,req,rlen)) {failed++;close(s);continue;}       
 
-     if(http10==0)   
 
-         if(shutdown(s,1)) { failed++;close(s);continue;}  
 
-     if(force==0)                                  
 
-     {  
 
-             
 
-         while(1)                                          
 
-         {  
 
-               if(timerexpired) break;   
 
-           i=read(s,buf,1500);                       
 
-               
 
-           if(i<0)                          
 
-               {   
 
-                  failed++;  
 
-                  close(s);  
 
-                  goto nexttry;  
 
-               }  
 
-            else  
 
-                if(i==0) break;           
 
-                else  
 
-                    bytes+=i;  
 
-         }  
 
-     }  
 
-     if(close(s)) {failed++;continue;}  
 
-     speed++;  
 
-  }  
 
- }  
 
 webbench
原文:http://www.cnblogs.com/yuankaituo/p/4274796.html