前言
Webbench是一个网站压力测试的工具。由Lionbridge公司开发,Webbech的标准测试可以向我们展示服务器的两项内容,分别为每秒钟请求数和每秒钟传输数据量。如果你不清数访问的网站能承受多大的压力,或者对比两个网站的性能可以,考虑使用Webbench或者http_load来测试。
源码下载
webbench源码一共包含两个源文件:socket.c和webbench.c
下载地址:
http://home.tiscali.cz/~cz210552/webbench.html
工作原理
1.主函数进行参数命令行检查,并且进入bench开始压测。
2.bench函数使用fork模拟出多个客户端,调用socket并发请求,每个子进程记录自己的访问数据,并写入管道。
3.父进程从管道读取子进程的输出信息。
4.使用函数alarm进行时间控制,到时候后会发生SIGALRM信号,调用信号处理函数子进程停止。
Webbench原理图
源码分析
socket.c
/* $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连接
如果返回 -1,表示建立连接失联
***********************/
//以host和clientPort构成一对TCP的套接字(host支持域名)
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;
}
webbench.c
在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请求进行测试。
wenbench.c源代码及注释 :
/*
* (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;
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 */
/* 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; //只模拟一个客户端,并发数
int force=0; //是否等待服务器应答。默认为不等待
int force_reload=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;
}
//帮助信息
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;
}
//不能指定客户端数和请求时间为 0
if(clients==0) clients=1;
if(benchtime==0) benchtime=30;
/* Copyright */
fprintf(stderr,"Webbench - Simple Web Benchmark "PROGRAM_VERSION"\n"
"Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.\n"
);
//构造HTTP请求到request数组
build_request(argv[optind]);
// print request info ,do it in function build_request
/*printf("Benchmarking: ");
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");
*/
printf("Runing info: ");
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");
//开始压力测试,返回bench函数执行结果
return bench();
}
/*******************
功能:创建URL请求连接
url:url地址
返回值:无
********************/
void build_request(const char *url)
{
char tmp[10];
int i;
//请求地址和请求连接清零
//bzero(host,MAXHOSTNAMELEN);
//bzero(request,REQUEST_SIZE);
memset(host,0,MAXHOSTNAMELEN);//初始化
memset(request,0,REQUEST_SIZE);
//判断应该使用的HTTP协议,协议适配
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(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++;
}
}
测试
下载webbench源码完成,可以借助文件传输软件WinSCP工具,上传到你的LINUX发行版上。选择一个文件夹。把webbench源码放到当前文件夹。
解压:
tar -zxvf ./webbench-1.5.tar.gz
进入webbench,然后make
如果不知道怎么使用,可以使用./webbecnh -help
测试1:
报错的原因为不是有效的URL,想知道URL是什么,可以看这里URL是什么!
测试2:
提示说:只有HTTP协议是直接支持的,设为代理。
测试3:
提示:无效的URL语法-主机名不以’/'结尾
测试4:
测试5:
还可以用到-c和-t两个参数,还是测试百度网站的性能情况
-t表示测试的时间,-c表示并发访问网站的客户数。
返回的结果中有两个指标:
1.pages/min:每分输出的页面数;
2.bytes/sec:每秒传输的比特数;
3.succeed和failed表示请求的成功数目和失败数目;
来源:CSDN
作者:Minger~
链接:https://blog.csdn.net/chen1415886044/article/details/103939937