-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cpp
495 lines (448 loc) · 12.3 KB
/
process.cpp
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
/*************************************************************************
> File Name: process.cpp
> Author: GZY
************************************************************************/
#include <netdb.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <sys/param.h>
#include <rpc/types.h>
#include <getopt.h>
#include <cstring>
#include <pthread.h>
#include <vector>
#include <string>
#include <time.h>
#include <signal.h>
#include <condition_variable>
#include <sys/types.h>
#include <sys/stat.h>
#include <exception>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "process.h"
#include <algorithm>
#include <atomic>
using namespace std;
using std::string;
using std::vector;
void *benchChild(void *p); //子线程函数
PROCESS process;
vector<pthread_t> tv;
static const struct option long_options[] = {
{"force", no_argument, &process.force, 1},
{"reload", no_argument, &process.force_reload, 1},
{"time", required_argument, NULL, 't'},
{"help", no_argument, NULL, '?'},
{"http11", no_argument, NULL, '2'},
{"get", no_argument, &process.method, METHOD_GET},
{"head", no_argument, &process.method, METHOD_HEAD},
{"version", no_argument, NULL, 'V'},
{"proxy", required_argument, NULL, 'p'},
{"clients", required_argument, NULL, 'c'},
{NULL, 0, NULL, 0}};
static void signalhandle(int signal)
{
process.timerecpired = 1;
}
void childToProcess(PROCESS &process, TTTHREAD &th)
{
int old = process.allspeed.load();
while(!process.allspeed.compare_exchange_weak(old, th.speed+old));
old = process.allfailed.load();
while(!process.allfailed.compare_exchange_weak(old, th.fail+old));
old = process.allbytes.load();
while(!process.allbytes.compare_exchange_weak(old, th.bytes+old));
}
PROCESS::PROCESS()
{
timerecpired = 0;
allspeed = 0;
allfailed = 0;
allbytes = 0;
method = METHOD_GET;
clients = 1;
force = 0;
force_reload = 0;
proxyhost = "";
proxyport = 80;
benchtime = 30;
host = "";
request = "";
}
// 解释命令行参数 并启动bench
int PROCESS::start(int argc, char **argv)
{
int opt = 0;
int options_index = 0;
int index;
string tempport;
if (argc == 1)
{
usage();
return 2;
}
while ((opt = getopt_long(argc, argv, "2Vfrt: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 '2':
httpv = 2;
break;
case 'V':
cout << PROGRAM_VERSION << endl;
exit(0);
case 't':
benchtime = atoi(optarg);
break;
case 'p':
proxyhost = optarg;
index = proxyhost.rfind(':', proxyhost.length() - 1); //找到最后一个:
if (index == string::npos)
return 2;
if (index == 0)
{
cout << "108The hostname of the proxy can't be found" << endl;
return 2;
}
if (index == proxyhost.length() - 1)
{
cout << "112Thi port of the proxy can't be found" << endl;
return 2;
}
proxyhost = proxyhost.substr(0, index);
tempport = proxyhost.substr(index + 1, proxyhost.length() - index - 1);
proxyport = atoi(tempport.c_str());
if (proxyhost.length() > MAXHOSTSIZE)
return 2;
if (proxyport > MAXPORT)
return 2;
cout << "120 proxyhost:proxyport = " << proxyhost << ":" << proxyport << endl;
break;
case ':':
case 'h':
case '?':
usage();
return 2;
break;
case 'c':
clients = atoi(optarg);
break;
}
}
if (optind == argc)
{
cout << "130Can'tfound the texted url!" << endl;
usage();
return 2;
}
if (clients <= 0)
clients = 1;
if (clients > MAXPTHREAD)
clients = MAXPTHREAD;
if (benchtime <= 0)
benchtime = 30;
build_request(argv[optind]);
cout << "Testing ..." << endl;
switch (method)
{
case METHOD_GET:
default:
cout << "GET" << endl;
break;
case METHOD_HEAD:
cout << "HEAD" << endl;
break;
}
switch (httpv)
{
default:
httpv = 2;
}
cout << "Client = " << clients << endl;
cout << "In the time is " << benchtime << endl;
if (force)
cout << "Early socket close" << endl;
if (proxyhost != "")
cout << "The proxy server is " << proxyhost << " : " << proxyport << endl;
if (force_reload)
cout << "Forcing reload" << endl;
return bench();
}
void PROCESS::build_request(char *url)
{
string surl = url;
char tmp[10];
int i;
if (httpv != 2)
httpv = 2; //只支持http1.1版本
//gzy:构建http包的请求类型
switch (method)
{
default:
case METHOD_GET:
request += "GET";
break;
case METHOD_HEAD:
request += "HEAD";
break;
}
request += " ";
//以下两个if语句监测url是否规范
if (surl.length() > MAXHOSTSIZE)
{
cout << "181The checked url is too long." << endl;
exit(2);
}
if (surl.find("://", 0) == string::npos)
{
cout << "184The url is not standard which should be http://*." << endl;
exit(2);
}
if (proxyhost == "")
//url头必须是http://开头
if (0 != surl.compare(0, 7, "http://"))
{
cout << "The only support protocol is http1.1" << endl;
exit(2);
}
i = surl.find("://", 0) + 3; //http://www.baidu.com的第一个w的位置
if (surl.find('/', i) <= i)
{
//url支持格式ex:
//http://12.123.123.123/
//http://12.123.123.123/1.*
cout << "Invalid url -hostname dont ends with '/'";
exit(2);
}
if (proxyhost == "")
{
//if是:http://12.123.123.123:5656/的格式
if ((surl.find(':', i) != string::npos) && (surl.find(':', i) < surl.find('/', i)))
{
host = host.assign(url + i, surl.find(':', i) - i);
//bzero(tmp,10);
//strncpy(tmp,index(url+i,':')+1,strchr(url+i,'/')-index(url+i,':')-1);
surl.assign(surl, surl.find(':', i) + 1, surl.find('/', i) - i - 1);
proxyport = atoi(surl.assign(surl, surl.find(':', i) + 1, surl.find('/', i) - i - 1).c_str());
if (proxyport <= 0)
proxyport = 80;
}
else
{
host = host.assign(url + i, surl.rfind('/', surl.length() - 1) - i);
}
request += url + i + strcspn(url + i, "/");
}
else
{
request += url;
}
request += " HTTP/1.1\r\n";
if (httpv > 0)
{
request += "User-Agent: WebBench";
request += "PROGRAM_VERSION\r\n";
}
if (proxyhost == "")
{
request += "Host: ";
request += host;
request += "\r\n";
}
if (force_reload && proxyhost != "")
{
request += "Pragma: no-cache\r\n";
}
request += "Connection:close\r\n";
request += "\r\n";
}
// 创建client个线程,每个线程运行benchChild函数
bool PROCESS::bench(void)
{
int tempspeed, tempfail, tempbytes;
FILE *f;
int fd;
/*我觉得人家封的socket更好*/
fd = mysocket(proxyhost == "" ? host : proxyhost, proxyport);
if (fd < 0)
{
cout << "Connection is fail" << endl;
return 1;
}
close(fd);
/*开始开线程了*/
int i = 0;
for (i = 0; i < clients; i++)
{
pthread_t thid;
pthread_create(&thid, NULL, benchChild, NULL);
tv.push_back(thid);
}
// allspeed=0;
// allfailed=0;
// allbytes=0;
for (auto &thr : tv)
{
pthread_join(thr, NULL);
}
cout << "线程都执行完毕了" << endl;
/*现在所有线程都执行完毕*/
cout << "****** url ******" << endl;
cout << host << endl;
cout << "speed = " << (allspeed + allfailed) / (benchtime / 60.0) << "pages/min" << endl;
cout << "suceed = " << allspeed << endl;
cout << "fail = " << allfailed << endl;
cout << "byte/sec = " << allbytes / (benchtime / 1.0) << endl;
return true;
}
// 子线程的执行函数,运行benchcore函数
// 运行后的结果,通过锁更新到全局变量中
void *benchChild(void *p)
{
FILE *fp;
int speed = 0;
int fail = 0;
int bytes = 0;
TTTHREAD temp(speed, fail, bytes);
if (process.proxyhost == "")
{
process.benchcore(temp);
}
else
{
process.benchcore(temp);
}
// 更新全局变量 通过互斥锁解决
try
{
childToProcess(process, temp);
}
catch (exception &e)
{
cout << e.what() << endl;
}
}
// 在规定时间内,尽可能多的链接与发送并接受数据
// sigalarm触发之后返回
void PROCESS::benchcore(TTTHREAD &t)
{
int rlen;
char buf[1500];
int fd, lenread;
struct sigaction sa;
sa.sa_handler = signalhandle;
sa.sa_flags = 0;
if (sigaction(SIGALRM, &sa, NULL))
exit(3);
alarm(benchtime);
rlen = request.length();
if (proxyhost != "")
host = proxyhost;
nexttry:
while (1)
{
//gzy:如果超时,就不给那个网站发数据了,也就是本分支的测试任务已经完成
if (timerecpired)
return;
fd = mysocket(host, proxyport);
//gzy:以下是各种发送出错情况
if (fd < 0)
{
t.fail++;
continue;
}
if (rlen != write(fd, request.c_str(), rlen))
{
t.fail++;
close(fd);
continue;
}
if (httpv != 2)
if (shutdown(fd, 1))
{
t.fail++;
close(fd);
continue;
}
if (force == 0) //
{
while (1)
{
if (timerecpired)
break;
lenread = read(fd, buf, 1500);
if (lenread < 0)
{
t.fail++;
close(fd);
//gzy:读失败了无所谓,进行下一次压力发送即可
goto nexttry;
}
else if (lenread == 0)
break;
//gzy:这里的bytes代表本分支一共接收到的bytes数量
else
t.bytes += lenread;
}
}
close(fd);
t.speed++;
}
}
// 封装socket
int mysocket(string host, int port)
{
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.c_str());
if (inaddr != INADDR_NONE)
memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
else
{
hp = gethostbyname(host.c_str());
if (hp == NULL)
return -1;
memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
}
ad.sin_port = htons(port);
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;
}
// 提示函数
void PROCESS::usage(void)
{
cout << "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 1.MAX 100\n"
" -2|--http11 Use HTTP/1.1 protocol.\n"
" --get Use GET request method.\n"
" --head Use HEAD request method.\n"
" -?|-h|--help This information.\n"
" -V|--version Display program version.\n"
<< endl;
};