webbench源码分析
来源:互联网 发布:paxos算法视频教程 编辑:程序博客网 时间:2024/05/22 00:12
根据提供的参数构造http请求的消息头,然后fork出N个子进程(也就是webbench时候提供的参数-c N),每个子进程根据根据参数-t提供的时间,持续这么多秒,这这么长的时间内利用socket创建一个tcp连接到我们想要的网址页面。然后每个进程统计自己再这个时间内取得了多少页面和多少数据,然后由父进程统计,最后发送的consel上面来。
我提取出主要的几个函数
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);
}
i=strstr(url,"://")-url+3;
if(strchr(url+i,'/')==NULL) {
fprintf(stderr,"\nInvalid URL syntax - hostname don't ends with '/'.\n");
exit(2);
}
if(proxyhost==NULL)
{
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);
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