一、程序说明:
本程序对上述png服务器性能进行测试。(开发于freebsd,并可编译运行于windows cygwin环境)。
二、使用说明:(类似于ab)
st [options] [http://]hostname/path
-n requests number of requests to perform
-c concurrency number of multiple requests to make
-v print version number and exit
-h display usage information (this message)
三、源代码:
/******************************************************************************
* copyright (c) 2004-2005 xiongbin xiong all rights reserved
* references: stevens,w.r. 1992. advanced programming in the unix environment.
* addison-wesley.
* stevens,w.r. 1998. unix network programming volum1.
* prentice hall ptr.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <signal.h>
#define buffsize 65535
#define maxline 4096
static int requests=1; /* 在测试会话中所执行的请求个数,默认为一个 */
static int concurrency=1; /* 并发级别,一次产生的请求个数,默认是一次一个 */
static int done; /* 在测试会话中所完成的请求个数 */
static int recvdatas=0; /* 接收的总数据量 */
static char *hostname,*pathname; /* 主机名、请求路径名 */
static struct timeval take_time; /* 完成所有请求所需的时间 */
static int lflag; /* 子进程结束计数标志 */
int parse_url(char *); /* 将url分解为hostname和pathname */
int test(void); /* 开始进行测试 */
int con_test(int); /* 并发进程测试 */
int fork_do(void); /* 测试程序 */
int record(int); /* 记录测试数据 */
int lock_reg(int,int,int,off_t,int,off_t); /* 使用记录锁对共享文件进行保护 */
static void output_results(void); /* 输出结果 */
static void sig_chld(int); /* 处理子进程sigchld信号 */
static void usage(const char *); /* 提示信息 */
static void err(char *); /* 出错处理函数 */
static void copyright(void); /* 版本信息 */
int
main(int argc, char *argv[])
{
int flag;
char *url;
file *fp;
if(argc<2||argc>6){
printf("invalid input\n");
usage(argv[0]);
exit(1);
}
opterr=0;
while((flag=getopt(argc,argv,"n:c:hv"))!=eof){
switch(flag){
case 'n':
requests=atoi(optarg); /* 总请求数 */
if(requests<=0)
err("invalid number of requests");
break;
case 'c':
concurrency=atoi(optarg); /* 并发请求的级别,即每个并发请求中同时发起的请求数 */
if(concurrency<=0)
err("invalid number of concurrency");
break;
case 'h':
usage(argv[0]); /* 帮助信息 */
break;
case 'v':
copyright(); /* 版本信息 */
exit(0);
case '?':
printf("unrecognized option: -%c\n",optopt); /* 错误的参数 */
usage(argv[0]);
exit(1);
}
}
if(requests<concurrency) /* 并发级别要小于总请求数 */
err("invalid number of requests or concurrency");
done=requests;
fp=fopen("temp.log","wb");
fprintf(fp,"%d\t%d",done,recvdatas); /* 设置文件初值 */
fclose(fp);
if(optind>=argc)
err("invalid input");
else
url=argv[optind];
if(parse_url(url)) /* 将url分解为hostname和pathname */
err("invalid url address");
copyright();