爱可以穿越时空,穿越维度 - 《星际穿越》

前言

Webbench是一款非常简单的网站压力测试工具,它是由Lionbridge公司开发的。由于它的简单性和轻量级,Webbench在互联网公司中广泛使用,特别是在进行网站压力测试时。

Webbench的主要功能是模拟大量的并发连接,以测试网站在高并发环境下的性能。它可以模拟多达3万个并发连接,这对于大多数网站来说已经足够了。

Webbench的使用也非常简单,你只需要在命令行中输入相应的命令,就可以开始测试。例如,以下命令会模拟1000个并发连接,持续60秒:

1
webbench -c 1000 -t 60 http://yourwebsite.com

测试结束后,Webbench会在命令行中显示测试结果,包括每秒钟处理的请求数(Requests per second)、每秒钟传输的数据量(Transfer rate)等。

总的来说,Webbench是一款简单、轻量级的网站压力测试工具,它可以帮助你评估你的网站在高并发环境下的性能。

官网:http://home.tiscali.cz/~cz210552/webbench.html

核心原理

父进程fork若干个子进程,每个子进程在用户要求时间或默认的时间内对目标web循环发出实际访问请求,父子进程通过管道进行通信,子进程通过管道写端向父进程传递在若干次请求访问完毕后记录到的总信息,父进程通过管道读端读取子进程发来的相关信息,子进程在时间到后结束,父进程在所有子进程退出后统计并给用户显示最后的测试结果,然后退出。

image-20240521215503291

源码分析

socket.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* $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文件中,包含了下面几个函数:

1
2
3
4
5
6
7
8
9
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源代码及注释 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
* (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++;
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(base) sv@sv-NF5280M5:/home/sv/pengeHome/webbench-c$ ./webbench -help
webbench [option]... URL
-f|--force Don't wait for reply from server.
-r|--reload Send reload request - Pragma: no-cache.
-t|--time <sec> Run benchmark for <sec> seconds. Default 30.
-p|--proxy <server:port> Use proxy server for request.
-c|--clients <n> Run <n> HTTP clients at once. Default one.
-9|--http09 Use HTTP/0.9 style requests.
-1|--http10 Use HTTP/1.0 protocol.
-2|--http11 Use HTTP/1.1 protocol.
--get Use GET request method.
--head Use HEAD request method.
--options Use OPTIONS request method.
--trace Use TRACE request method.
-?|-h|--help This information.
-V|--version Display program version.

简单测试下

1
2
3
4
5
6
7
8
9
(base) sv@sv-NF5280M5:/home/sv/pengeHome/webbench-c$ ./webbench  http://www.baidu.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.baidu.com/
1 client, running 30 sec.

Speed=356 pages/min, 2416135 bytes/sec.
Requests: 178 susceed, 0 failed.

-t表示测试的时间,-c表示并发访问网站的客户数。上述QPS=178/30

返回的结果中有两个指标:

1
2
3
1.pages/min:每分输出的页面数;
2.bytes/sec:每秒传输的比特数;
3.succeed和failed表示请求的成功数目和失败数目;

补充

Apache Bench 是 Apache 服务器自带的一个web压力测试=工具,简称 ab 。

ab的原理:ab命令会创建 多个并发 访问线程,模拟 多个访问者 同时对某一 URL地址 进行访问。

它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。

参考自: