Webbench 源码分析 阅读笔记
2016-01-05 12:58
603 查看
一.简介
Webbench是有名的网站压力测试工具,它是由 Lionbridge公司开发。Webbech能测试处在相同硬件上,不同服务的性能以及不同硬件上同一个服务的运行状况。webBech的标准测试可以向我们展示服务器的 两项 内容:每秒钟相应请求数和每秒钟传输数据量。webbench不但能具有便准静态页面的测试能力,还能对动态页面(ASP,PHP,JAVA,CGI)进 行测试的能力。还有就是他支持对含有SSL的安全网站例如电子商务网站进行静态或动态的性能测试。
Webbench最多可以模拟3万个并发连接去测试网站的负载能力,其源码仅仅500行左右。
二.源码解读
源码一共包含两个源文件:socket.c 和 webbench.c 。1. socket.c
本文件仅包含一个Socket函数:int Socket(const char *host, int clientPort) { /* * 以host和clientPort构成一对TCP的套接字(host支持域名) * 创建失败返回-1,成功返回一个sockt描述符 * * 涉及的知识点: * inet_addr:若字符串有效则将字符串转换为32位二进制 * 网络字节序的IPV4地址,否则为INADDR_NONE; * gethostbyname:返回对应于给定主机名的包含主机名字 * 和地址信息的hostent结构指针; * htons:将一个无符号短整型(s)的主机数值(h)转换为网络字节顺序(n); * socket:创建套接字 * connect:连接到相应的主机 */ int sock; unsigned long inaddr; struct sockaddr_in ad; struct hostent *hp; memset(&ad, 0, sizeof(ad)); ad.sin_family = AF_INET; inaddr = inet_addr(host); if (inaddr != INADDR_NONE) memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr)); else { hp = gethostbyname(host); if (hp == NULL) return -1; memcpy(&ad.sin_addr, hp->h_addr, hp->h_length); } ad.sin_port = htons(clientPort); 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; }
2.webbench.c
在webbench.c中,包含了下面几个函数:static void alarm_handler(int signal); static void usage(void); static void build_request(const char *url); static int bench(void); static void benchcore(const char *host, const int port, const char *req); int main(int argc, char *argv[]);
- Webbench工作流程:
进入main函数,解析命令行参数;调用build_request()函数,构造http request信息;
调用bench()函数,根据client大小fork子进程;
子进程调用benchcore()函数进行测试,挂接信号处理函数alarm_handler,将结果写入管道。
主进程从管道读取消息,并输出到stdout。
下面逐个部分进行分析:
- 全局变量
/* values */ /* 超时标识,由命令行参数-t指定,当被设置为1时,所有子进程退出 * volatile: * - 让系统总是从内存读取数据, * - 告诉编译器不要做任何优化, * - 变量会在程序外被改变 */ volatile int timerexpired=0; int speed=0; //子进程成功得到服务器响应的总数 int failed=0; //子进程请求失败总数 int bytes=0; //读取到的字节总数 /* globals */ int http10=1; /* 0 - http/0.9, 1 - http/1.0, 2 - http/1.1 */ //HTTP协议版本 /* Allow: GET, HEAD, OPTIONS, TRACE */ #define METHOD_GET 0 #define METHOD_HEAD 1 #define METHOD_OPTIONS 2 #define METHOD_TRACE 3 #define PROGRAM_VERSION "1.5" int method=METHOD_GET; //HTTP请求方法,默认为GET int clients=1; //并发数,即子进程个数,默认为1,可以由命令行参数-c指定 int force=0; //是否等待从服务器获取数据,0表示等待 int force_reload=0; //是否使用cache,0为使用 int proxyport=80; //代理服务器端口,默认为80 char *proxyhost=NULL; //代理服务器IP,默认为NULL /* * 执行时间,默认为30秒,可由命令行参数-t指定, * 当子进程执行时间超过这个秒数之后, * 发送 SIGALRM 信号,将 timerexpired 设置为 1, * 让所有子进程退出 */ int benchtime=30; /* internal */ int mypipe[2]; //创建管道(半双工),用于父子进程间通信 char host[MAXHOSTNAMELEN]; //服务器IP #define REQUEST_SIZE 2048 char request[REQUEST_SIZE]; //HTTP请求信息 /* 命令行的选项配置表,参见man文档:man getopt_long */ 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} };
- main()函数
int main(int argc, char *argv[]) { int opt=0; int options_index=0; char *tmp=NULL; if(argc==1) { usage(); return 2; } /* 参数检查,并设置相应选项,参见 man getopt_long */ 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; } *tmp='\0'; proxyport=atoi(tmp+1);break; case ':': case 'h': case '?': usage();return 2;break; case 'c': clients=atoi(optarg);break; } } /* 扫描参数选项时,optind标识下一个选项的索引; * 扫描结束后,标识第一个非选项参数索引; * 如果optind=argc,说明非选项参数即服务器URL缺失。 * 此变量是系统定义的。 */ if(optind==argc) { fprintf(stderr,"webbench: Missing URL!\n"); usage(); return 2; } if(clients==0) clients=1; if(benchtime==0) benchtime=60; /* Copyright */ fprintf(stderr,"Webbench - Simple Web Benchmark "PROGRAM_VERSION"\n" "Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.\n" ); build_request(argv[optind]); //最后为URL,所以optind应该是URL的位置 /* print bench info */ printf("\nBenchmarking: "); switch(method) { case METHOD_GET: default: printf("GET");break; case METHOD_OPTIONS: printf("OPTIONS");break; case METHOD_HEAD: printf("HEAD");break; case METHOD_TRACE: printf("TRACE");break; } printf(" %s",argv[optind]); switch(http10) { case 0: printf(" (using HTTP/0.9)");break; case 2: printf(" (using HTTP/1.1)");break; } printf("\n"); if(clients==1) printf("1 client"); else printf("%d clients",clients); printf(", running %d sec", benchtime); if(force) printf(", early socket close"); if(proxyhost!=NULL) printf(", via proxy server %s:%d",proxyhost,proxyport); if(force_reload) printf(", forcing reload"); printf(".\n"); return bench(); }
- build_request()函数
/**************** 创建URL请求连接,HTTP头 @url:url地址 创建好的请求放在全局变量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,"://")) //strstr(str1,str2)用于判断str2是否是str1的子串 { 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)) //比较前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) { //url+i指向http://后第一个位置 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,'/')) //判断url中是否指定了端口号 { 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); //端口号转换为int 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); }
- bench()函数
/********************** 创建管道和子进程,对http请求进行测试 **********************/ /* vraci system rc error kod */ 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; } /* not needed, since we have alarm() in childrens */ /* wait 4 next system clock tick */ /* cas=time(NULL); while(time(NULL)==cas) sched_yield(); */ /* fork childs */ for(i=0;i<clients;i++) { pid=fork(); if(pid <= (pid_t) 0) //pid=0->子进程,pid<0->error { /* child process or error*/ sleep(1); /* make childs faster */ break; /* 子进程跳出循环orfork出错父进程跳出循环 */ } } if( pid< (pid_t) 0) //fork出错 { fprintf(stderr,"problems forking worker no. %d\n",i); perror("fork failed."); return 3; } if(pid== (pid_t) 0) //子进程 { /* I am a child */ /* 子进程执行请求:尽可能多的发送请求,直到超时返回为止 */ if(proxyhost==NULL) benchcore(host,proxyport,request); else benchcore(proxyhost,proxyport,request); /* write results to pipe */ f=fdopen(mypipe[1],"w"); if(f==NULL) { perror("open pipe for writing failed."); return 3; } /* fprintf(stderr,"Child - %d %d\n",speed,failed); */ 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; } /* _IONBF(无缓冲):直接从流中读入数据或直接向流中写入数据,而没有缓冲区 */ 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; /* fprintf(stderr,"*Knock* %d %d read=%d\n",speed,failed,pid); */ 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()函数
/****************************** 真正测试http的地方 @host:地址 @port:端口 @req:http格式方法 ********************************/ 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 */ /* 当程序执行到指定的秒数之后,发送 SIGALRM 信号 */ sa.sa_handler=alarm_handler; sa.sa_flags=0; if(sigaction(SIGALRM,&sa,NULL)) exit(3); alarm(benchtime); rlen=strlen(req); /* 无限执行请求,直到接收到 SIGALRM 信号将 timerexpired 设置为 1 时 */ 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;} /* 如果是 http/0.9 则关闭 socket 的写操作 */ 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++; } }
三.测试
$ webbench www.jiangezh.com Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. www.jiangezh.com: is not a valid URL. $ webbench ftp://www.jiangezh.com Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Only HTTP protocol is directly supported, set --proxy for others. $ webbench http://www.jiangezh.com Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Invalid URL syntax - hostname don't ends with '/'. $ webbench http://www.jiangezh.com:8000/ Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://www.jiangezh.com:8000/ 1 client, running 30 sec. Connect to server failed. Aborting benchmark. $ webbench http://www.jiangezh.com/ Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://www.jiangezh.com/ 1 client, running 30 sec. Speed=126 pages/min, 83208 bytes/sec. Requests: 63 susceed, 0 failed. $ webbench -t 10 -c 20 webbench: Missing URL! webbench [option]... URL -f|--force Don't wait for reply from server. -r|--reload Send reload request - Pragma: no-cache. -t|--time <sec> Run benchmark for <sec> seconds. Default 30. -p|--proxy <server:port> Use proxy server for request. -c|--clients <n> Run <n> HTTP clients at once. Default one. -9|--http09 Use HTTP/0.9 style requests. -1|--http10 Use HTTP/1.0 protocol. -2|--http11 Use HTTP/1.1 protocol. --get Use GET request method. --head Use HEAD request method. --options Use OPTIONS request method. --trace Use TRACE request method. -?|-h|--help This information. -V|--version Display program version. $ webbench -t 10 -c 500 http://www.jiangezh.com/article/SYNFloodSrc.html Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://www.jiangezh.com/article/SYNFloodSrc.html 500 clients, running 10 sec. Speed=126 pages/min, 121629 bytes/sec. Requests: 21 susceed, 0 failed.
呜呼T^T
发现我的博客网站太菜了扛不起多个连接==
不过没关系!因为根本就没什么访问量!
相关文章推荐
- 为什么算法渐进复杂度中对数的底数总为2
- Git-大总结
- 系统研究Airbnb开源项目airflow
- 第一次打开Lightroom时的基本设置
- 算法分析中递推式的一般代数解法
- 计算实际上班时间
- Syslog协议介绍
- 深入理解Android之Xposed详解
- 抓取Js动态生成数据且以滚动页面方式分页的网页
- css3背景图片无间断滚动
- 主席树-区间第k大值(不带修改)
- 时间序列分析基础
- POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(数学)
- 第一次打开Photoshop时的基本设置
- mysql 数据库备份ubuntu
- 期望、方差、协方差及相关系数的基本运算
- 去掉CodeIgniter(CI)默认url中的index.php的步骤:
- sql server 查看表的死锁和Kill 死锁进程
- 图解Linux系统的系统架构
- 银行卡提现