前言
Webbench是一个在linux下应用的非常简单的网站压测工具。它应用fork()模仿多个客户端同时拜访咱们设定的URL,测试网站在压力下工作的性能,最多能够模仿3万个并发连贯去测试网站的负载能力。Webbench均应用C语言编写, 代码切实太简洁,源码加起来不到600行。
源文件下载地址;http://home.tiscali.cz/~cz210...
从官网介绍能够看出,该文件最初更新于2004年(那时候我才四岁),堪称十分的经典
下载后的文件:
webbench.c剖析
在qt环境下关上webbench.c,依据第82行的代码正文,能够看出这个文件中的function(性能)
翻译成相熟的中文:
参数介绍:
-f :不期待服务器数据返回-r :发送从新加载申请-t <sec> : 运行总时长,默认30秒-p <sever:port> : 设计代码服务器-c <clients> : 设置运行多少个客户端过程-9/-1/-2: HTTP协定版本–get : get申请–head: head 申请–options: options 申请–trace: trace申请-?|-h|–help: 帮忙信息-V|–version: 版本信息
函数主体局部用于解析各参数,而后执行接下来的各个自定义函数,从而实现网站测压工作。
int main(int argc, char *argv[]){ int opt=0; int options_index=0; char *tmp=NULL; if(argc==1) { usage(); return 2; } while((opt=getopt_long(argc,argv,"912Vfrt:p:c:?h",long_options,&options_index))!=EOF ) { switch(opt) { case 0 : break; case 'f': force=1;break; case 'r': force_reload=1;break; case '9': http10=0;break; case '1': http10=1;break; case '2': http10=2;break; case 'V': printf(PROGRAM_VERSION"\n");exit(0); case 't': benchtime=atoi(optarg);break; case 'p': /* proxy server parsing server:port */ tmp=strrchr(optarg,':'); proxyhost=optarg; if(tmp==NULL) { break; } if(tmp==optarg) { fprintf(stderr,"Error in option --proxy %s: Missing hostname.\n",optarg); return 2; } if(tmp==optarg+strlen(optarg)-1) { fprintf(stderr,"Error in option --proxy %s Port number is missing.\n",optarg); return 2; }
这个函数的目标就是为了创立http申请,而后把获取的内容返回存储到全局变量request里。
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); } /* protocol/host delimiter */ i=strstr(url,"://")-url+3; /* printf("%d\n",i); */ if(strchr(url+i,'/')==NULL) { fprintf(stderr,"\nInvalid URL syntax - hostname don't ends with '/'.\n"); exit(2); } if(proxyhost==NULL) { /* get port from hostname */ 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); /* printf("tmp=%s\n",tmp); */ proxyport=atoi(tmp); if(proxyport==0) proxyport=80; } else { strncpy(host,url+i,strcspn(url+i,"/")); } // printf("Host=%s\n",host); strcat(request+strlen(request),url+i+strcspn(url+i,"/")); } else { // printf("ProxyHost=%s\nProxyPort=%d\n",proxyhost,proxyport); 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"); /* add empty line at end */ if(http10>0) strcat(request,"\r\n"); // printf("Req=%s\n",request);}/* vraci system rc error kod */
这部分的用处在于申请socket链接,为了确保链接通顺,因而退出了判断,如果未连贯,执行进行返回。
如果连贯胜利,调用pipe函数进行管道创立。
对于pipe函数的具体解释:https://cpp.hotexamples.com/e...
static int bench(void){ int i,j,k; pid_t pid=0; FILE *f; /* check avaibility of target server */ i=Socket(proxyhost==NULL?host:proxyhost,proxyport); if(i<0) { fprintf(stderr,"\nConnect to server failed. Aborting benchmark.\n"); return 1; } close(i); /* create pipe */ if(pipe(mypipe)) { perror("pipe failed."); return 3; }
这是本程序代码最初一部分,最初一个函数,这部分函数里有一个alam_handler零碎函数,在本程序中,其作用你能够了解为(定时报警执行),具体规范解释为:https://cloud.tencent.com/dev...,一旦超过就会中断。
void benchcore(const char *host,const int port,const char *req){ int rlen; char buf[1500]; int s,i; struct sigaction sa; /* setup alarm signal handler */ 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) { /* fprintf(stderr,"Correcting failed by signal\n"); */ 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) { /* read all available data from socket */ while(1) { if(timerexpired) break; i=read(s,buf,1500); /* fprintf(stderr,"%d\n",i); */ if(i<0) { failed++; close(s); goto nexttry; } else if(i==0) break; else bytes+=i; } } if(close(s)) {failed++;continue;} speed++; }}
socket.c剖析
这个函数作为webbench的辅助和须要的头文件,其性能为
// socket描述符,次要以host和clientPort形成一对TCP的套接字(host反对域名),创立失败返回-1,胜利返回一个int Socket(const char *host, int clientPort){ int sock; unsigned long inaddr; struct sockaddr_in ad; struct hostent *hp; memset(&ad, 0, sizeof(ad)); ad.sin_family = AF_INET; // 若字符串无效,则将字符串转换为32位二进制。网络字节序的IPV4地址,否则为INADDR_NONe inaddr = inet_addr(host); if (inaddr != INADDR_NONE) memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr)); else { // 返回对应于给定主机名的蕴含主机名字和地址信息的hostent构造指针 hp = gethostbyname(host); if (hp == NULL) return -1; memcpy(&ad.sin_addr, hp->h_addr, hp->h_length); } // 将一个无符号短整型的主机数值转换为网络字节程序 ad.sin_port = htons(clientPort); // 创立socket套接字 sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) return sock; // 连贯到相应的主机 if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0) return -1; return sock;}