Web Beanch源码剖析
1、Web Bench是什么
Linux下的一个网站压力测试的工具。
Web bench的标准测试可以向我们展示服务器的两项内容,分别为每秒钟请求数和每秒钟传输数据量。
Web bench官网:Web bench源码官网
截图:
参考他人的博文:
https://blog.csdn.net/chen1415886044/article/details/103939937
https://blog.csdn.net/snow_5288/article/details/77892971
https://www.cnblogs.com/xuning/p/3888699.html
2、原理
Webbench的代码实现相当简单,就是一个父进程fork出多个子进程,子进程分别去执行http测试,最后把执行结果汇总写入管道,父进程读取管道数据然后进行最终测试结果的计算。
webbench主要的工作原理:
- 主函数进行必要的准备工作【解析参数,构建请求信息】,最后进入bench()函数开始压测。
- bench()函数使用fork模拟出多个客户端,调用socket并发请求,每个子进程记录自己的数据,最后写入管道。子进程中使用alarm函数进行时间控制,到时间后会设置一个变量,变量为真时使子进程退出。
- 父进程从管道读取子进程的输出信息
- 最后只留下父进程将所有子进程的输出数据汇总计算,输出到屏幕上
3、理解源码你要知道什么?
http请求格式
c语言的字符串函数,主要在string.h 和 strings.h头文件的函数
linux系统中的fork函数,alarm函数,socket编程中涉及的知识点,getopt_long函数等等
4、源码分析
源码解压后的目录:
如图只有两个源文件:
socket.c
webbench.c
4.1 socket.c分析
简析:
int Socket(const char *host, int clientPort)
{
//以host为服务器端地址,clientPort为服务器端口号建立socket连接
//连接类型为TCP,使用IPv4网域
//一旦出错,返回-1
//正常连接,则返回socket描述符
}
源码:
/* $Id: socket.c 1.1 1995/01/01 07:11:14 cthuang Exp $
*
* This module has been modified by Radim Kolar for OS/2 emx
*/
/***********************************************************************
module: socket.c
program: popclient
SCCS ID: @(#)socket.c 1.5 4/1/94
programmer: Virginia Tech Computing Center
compiler: DEC RISC C compiler (Ultrix 4.1)
environment: DEC Ultrix 4.3
description: UNIX sockets code.
***********************************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/***********************
功能:通过地址和端口建立网络连接
host:网络地址,支持域名
clientPort:端口
返回值:
正数:建立的socket连接
负数:表示失败
***********************/
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;
inaddr = inet_addr(host);//将点分的十进制的IP转为无符号长整型
if (inaddr != INADDR_NONE)
memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
else //如果host是域名
{
hp = gethostbyname(host);//通过域名获取ip地址
if (hp == NULL)
return -1;
memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
}
ad.sin_port = htons(clientPort); //将一个无符号短整型(s)的主机数值(h)转换为网络字节顺序(n)
//创建客户端套接字
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;//正常返回建立连接的socket
}
涉及知识点:
- gethostbyname函数:通过域名获取ip地址
#include <netdb.h>
#include<sys/socket.h>
struct hostent *gethostbyname(const char *name);
返回hostent结构体类型指针:
struct hostent
{
char *h_name; //主机的规范名
char **h_aliases; //主机的别名
int h_addrtype; //主机ip地址的类型,主机ip地址的类型,ipv4(AF_INET)或ipv6(AF_INET6)
int h_length; //主机ip地址的长度
char **h_addr_list; //主机的ip地址,以网络字节序存储,如果需要打印,需调用inet_ntop()函数,切记不能用printf函数直接打印。
#define h_addr h_addr_list[0]
};
- sockaddr_in结构:
struct sockaddr_in
{
short sin_family;/*Address family一般来说AF_INET(地址族)PF_INET(协议族)*/
unsigned short sin_port;/*Port number(必须要采用网络数据格式,普通数字可以用htons()函数转换成网络数据格式的数字)*/
struct in_addr sin_addr;/*IP address in network byte order(Internet address)*/
unsigned char sin_zero[8];/*Same size as struct sockaddr没有实际意义,只是为了 跟SOCKADDR结构在内存中对齐*/
};
4.2 webbench.c分析
这个文件中包含了以下几个函数,我们一一列举出来:
static void alarm_handler(int signal) //信号处理函数,时钟结束时进行调用
static void usage(void) //是在使用出错时提示怎么使用本程序。
void build_request(const char *url) //是用来创建http连接请求的。
static int bench(void) //中创建管道和子进程,调用测试http函数。
void benchcore(const char *host,const int port,const char *req)//对http请求进行测试。
int main(int argc, char *argv[]); //main函数
源码:
/*
* (C) Radim Kolar 1997-2004
* This is free software, see GNU Public License version 2 for
* details.
*
* Simple forking WWW Server benchmark:
*
* Usage:
* webbench --help
*
* Return codes:
* 0 - sucess
* 1 - benchmark failed (server is not on-line)
* 2 - bad param
* 3 - internal error, fork failed
*
*/
#include "socket.c"
#include <unistd.h>
#include <sys/param.h>
#include <rpc/types.h>
#include <getopt.h>
#include <strings.h>
#include <time.h>
#include <signal.h>
/* values */
volatile int timerexpired=0;//判断压测时长是否已经到达设定时间的变量,注意volatile
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版本,0表示http0.9,1表示http1.0,2表示http1.1
/* 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; //默认请求方式为GET,也支持HEAD、OPTIONS、TRACE
int clients=1; //并发数目,默认只有1个进程发请求,通过-c参数设置
int force=0; //是否需要等待读取从server返回的数据,0表示要等待读取
int force_reload=0; //是否使用缓存,1表示不缓存,0表示可以缓存页面
int proxyport=80; //代理服务器应答,访问端口为80
char *proxyhost=NULL; //代理服务器的地址
int benchtime=30; //模拟请求时间
/* internal */
int mypipe[2]; //管道,用于父子进程间通信
char host[MAXHOSTNAMELEN]; //网络地址
#define REQUEST_SIZE 2048
char request[REQUEST_SIZE]; //HTTP请求信息
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}
};
/* prototypes */
static void benchcore(const char* host,const int port, const char *request);
static int bench(void);
static void build_request(const char *url);
/*
子进程的信号处理函数
*/
static void alarm_handler(int signal)
{
timerexpired=1;
}
/*
webbeanch使用的帮助信息
*/
static void usage(void)
{
fprintf(stderr,
"webbench [option]... URL\n"
" -f|--force Don't wait for reply from server.\n"
" -r|--reload Send reload request - Pragma: no-cache.\n"
" -t|--time <sec> Run benchmark for <sec> seconds. Default 30.\n"
" -p|--proxy <server:port> Use proxy server for request.\n"
" -c|--clients <n> Run <n> HTTP clients at once. Default one.\n"
" -9|--http09 Use HTTP/0.9 style requests.\n"
" -1|--http10 Use HTTP/1.0 protocol.\n"
" -2|--http11 Use HTTP/1.1 protocol.\n"
" --get Use GET request method.\n"
" --head Use HEAD request method.\n"
" --options Use OPTIONS request method.\n"
" --trace Use TRACE request method.\n"
" -?|-h|--help This information.\n"
" -V|--version Display program version.\n"
);
}
/*
主函数
*/
int main(int argc, char *argv[])
{
int opt=0;
int options_index=0;
char *tmp=NULL;
//不带参数时直接输出帮助信息
if(argc==1)
{
usage();
return 2;
}
//getopt_log 为命令行解析的库函数
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 被 getopt_long设置为命令行参数中未读取的下一个元素下标值
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]);
/* 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();
}
/*
功能:创建URL请求连接
url:url地址
*/
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;
//填写method方式
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," ");
//URL 合法性判断
if(NULL==strstr(url,"://")) //找://”在URL中的位置
{
fprintf(stderr, "\n%s: is not a valid URL.\n",url);
exit(2);
}
if(strlen(url)>1500) //url是否太长
{
fprintf(stderr,"URL is too long.\n");
exit(2);
}
if (0!=strncasecmp("http://",url,7)) //比较前7个字符串
{
//只支持HTTP地址
fprintf(stderr,"\nOnly HTTP protocol is directly supported, set --proxy for others.\n");
exit(2);
}
//找到主机名开始的地方
/* protocol/host delimiter */
i=strstr(url,"://")-url+3; //i指向http://后第一个位置
//必须以/结束
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,'/')) //判断url中是否指定了端口号
{
strncpy(host,url+i,strchr(url+i,':')-url-i); //取出主机地址
//bzero(tmp,10);
memset(tmp,0,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("\nRequest:\n%s\n",request);
}
/*
功能:创建管道和子进程,对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)
{
/* child process or error*/
sleep(1); /* make childs faster */
break; //子进程立刻跳出循环,要不就子进程继续fork
}
}
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;
}
//[setvbuf定义流应如何缓冲](https://www.runoob.com/cprogramming/c-function-setvbuf.html)
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;
}
/*
功能:测试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 */
sa.sa_handler=alarm_handler;//定时器方法
sa.sa_flags=0;
if(sigaction(SIGALRM,&sa,NULL))
exit(3);
//设置闹钟函数
alarm(benchtime); // after benchtime,then exit
rlen=strlen(req);
//无限执行请求,直到接收到SIGALRM信号将timerexpired设置为1时
nexttry:while(1)
{
if(timerexpired)//定时器到时后,也就是收到信号则后,会设定timerexpired=1,函数就会返回
{
if(failed>0)
{
/* fprintf(stderr,"Correcting failed by signal\n"); */
failed--;
}
return;
}
//连接远程服务器 ,进行HTTP请求
s=Socket(host,port); //创建连接
if(s<0) {
failed++;continue;} //连接失败,failed加1
//发送请求
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++;
}
}
涉及知识点:
2.getopt_long函数,from这个blog
getopt函数只能处理短选项,而getopt_long函数两者都可以,可以说getopt_long已经包含了getopt_long的功能。因此,这里就只介绍getopt_long函数。
#include <unistd.h>
extern char *optarg;
extern int optind, opterr, optopt;
#include <getopt.h>
int getopt(int argc, char * const argv[],const char *optstring);
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
struct option
{
const char *name;
int has_arg;
int *flag;
int val;
};