早有听闻WebBench和TinyHttpd是工程师不得不了解的十大优秀C语言开源项目
出乎我意料的是,两个项目的代目都十分精简约在400行左右,而且稳定性极高,将近十年未更新
任务:
1.编译运行WebBench并了解注释WebBench源代码
2.编译运行TinyHttpd并了解注释TinyHttpd源代码
3.用编译的WebBench测试编译的TinyHttpd
那就先说WebBench咯~
WebBench
源码在此
http://home.tiscali.cz/~cz210552/webbench.html
编译运行
步骤如下
1.在网站上下载源代码
wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz
2.解压
tar zxvf webbench-1.5.tar.gz
//tinyhttpd同理
3.
cd webbench-1.5
4.make文件
make
(1)
出现找不到ctags或gcc
yum install ctags
5.
make install
(2)error 1
mkdir -m 644 -p /usr/local/man/man1
WebBench使用
webbench -c 1000 -t 60 http://www.baidu.com/index.html
webbench -c 并发数 -t 运行测试时间 URL
x可以直接输3w或者IP地址
通过ping获取IP地址
开始测试baidu服务器的抗压性能
注意IP地址跟域名是一样的意思
咱们先试一下100个用户30s测试
可以看出全都成功了
1000个用户30s测试
=v=结果如图
100个用户60s测试
10%左右的成功率。。
前方高能WebBench400多行的
!!!源码分析!!!!
//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>
//连接类型为TCP,使用IPv4网域
//host为服务器ip,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);
if (inaddr != INADDR_NONE)
memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
else
{
hp = gethostbyname(host); //域名获取IP
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; //出错返回-1
return sock; //正常连接,返回socket连接符
}
------------------------
//webbench.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; //方法
int clients=1; //并发数
int force=0; //等待服务器应答
int force_reload=0;
int proxyport=80; //代理服务器端口
char *proxyhost=NULL; //代理服务器地址
int benchtime=30; //运行时间
/* internal */
int mypipe[2];
char host[MAXHOSTNAMELEN];
#define REQUEST_SIZE 2048
char request[REQUEST_SIZE];
//方法选择
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;
}
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; //命令后的参数,atoi字符转换长整型
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