对webbench工具的代码分析

前言 Webbench是一个在linux下使用的非常简单的网站压测工具。它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接去测试网站的负载能力。Webbench均使用C语言编写, 代码实在太简洁,源码加起来不到600行。
源文件下载地址;http://home.tiscali.cz/~cz210...
对webbench工具的代码分析
文章图片

从官网介绍可以看出,该文件最后更新于2004年(那时候我才四岁),可谓非常的经典
下载后的文件:
对webbench工具的代码分析
文章图片

webbench.c分析 在qt环境下打开webbench.c,根据第82行的代码注释,可以看出这个文件中的function(功能)
对webbench工具的代码分析
文章图片

翻译成熟悉的中文:
参数介绍:

-f :不等待服务器数据返回 -r :发送重新加载请求 -t: 运行总时长,默认30秒 -p: 设计代码服务器 -c : 设置运行多少个客户端进程 -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) { case0 : 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,':')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工具的代码分析】这个函数作为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; }

    推荐阅读