2019-07-28 10:31:56 +08:00
|
|
|
|
#include "stdafx.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
using namespace acl;
|
|
|
|
|
|
|
|
|
|
static int __loop_count = 10;
|
|
|
|
|
static connect_pool* __conn_pool = NULL;
|
|
|
|
|
static acl_pthread_pool_t* __thr_pool = NULL;
|
|
|
|
|
static bool __unzip = false;
|
2020-03-14 20:52:16 +08:00
|
|
|
|
static bool __debug = false;
|
|
|
|
|
static acl::string __host;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static void init(const char* addr, int count)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳض<D3B3><D8B6><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
__conn_pool = new http_request_pool(addr, count);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
__thr_pool = acl_thread_pool_create(count, 60);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static void end(void)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl_pthread_pool_destroy(__thr_pool);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
delete __conn_pool;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static bool http_get(http_request* conn, int n)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
http_header& header = conn->request_header();
|
2020-03-14 20:52:16 +08:00
|
|
|
|
header.set_url("/")
|
2014-11-19 00:25:21 +08:00
|
|
|
|
.set_keep_alive(true)
|
|
|
|
|
.set_method(HTTP_METHOD_GET)
|
|
|
|
|
.accept_gzip(__unzip);
|
2020-03-14 20:52:16 +08:00
|
|
|
|
if (!__host.empty()) {
|
|
|
|
|
header.set_host(__host);
|
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-03-14 20:52:16 +08:00
|
|
|
|
if (__debug) {
|
|
|
|
|
printf("%lu--%d: begin send request\r\n",
|
|
|
|
|
(unsigned long) acl_pthread_self(), n);
|
|
|
|
|
}
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD> HTTP <20><>Ӧͷ
|
2020-03-14 20:52:16 +08:00
|
|
|
|
if (conn->request(NULL, 0) == false) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
printf("%lu--%d: send GET request error\r\n",
|
|
|
|
|
(unsigned long) acl_pthread_self(), n);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char buf[8192];
|
|
|
|
|
int ret, length = 0;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-03-14 20:52:16 +08:00
|
|
|
|
while (true) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
ret = conn->read_body(buf, sizeof(buf));
|
2020-03-14 20:52:16 +08:00
|
|
|
|
if (ret == 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
break;
|
2020-03-14 20:52:16 +08:00
|
|
|
|
} else if (ret < 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
printf("%lu--%d: error, length: %d\r\n",
|
|
|
|
|
(unsigned long) acl_pthread_self(), n, length);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
length += ret;
|
2020-03-14 20:52:16 +08:00
|
|
|
|
if (__debug) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
printf("%lu--%d: read length: %d, %d\r\n",
|
|
|
|
|
(unsigned long) acl_pthread_self(), n, length, ret);
|
2020-03-14 20:52:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (__debug) {
|
|
|
|
|
printf("%lu--%d: read body over, length: %d\r\n",
|
|
|
|
|
(unsigned long) acl_pthread_self(), n, length);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static void thread_main(void*)
|
|
|
|
|
{
|
2020-03-14 20:52:16 +08:00
|
|
|
|
for (int i = 0; i < __loop_count; i++) {
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3>л<EFBFBD>ȡһ<C8A1><D2BB> HTTP <20><><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
http_request* conn = (http_request*) __conn_pool->peek();
|
2020-03-14 20:52:16 +08:00
|
|
|
|
if (conn == NULL) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
printf("peek connect failed\r\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>Ҫ<EFBFBD>Ի<EFBFBD><D4BB>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵<EFBFBD><CCB5><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
2020-03-14 20:52:16 +08:00
|
|
|
|
else {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
conn->reset();
|
2020-03-14 20:52:16 +08:00
|
|
|
|
}
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD>µ<EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-03-14 20:52:16 +08:00
|
|
|
|
if (http_get(conn, i) == false) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
printf("one request failed, close connection\r\n");
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ر<EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
__conn_pool->put(conn, false);
|
2020-03-14 20:52:16 +08:00
|
|
|
|
} else {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
__conn_pool->put(conn, true);
|
2020-03-14 20:52:16 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void run(int cocurrent)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>̳߳<DFB3><CCB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-03-14 20:52:16 +08:00
|
|
|
|
for (int i = 0; i < cocurrent; i++) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl_pthread_pool_add(__thr_pool, thread_main, NULL);
|
2020-03-14 20:52:16 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s -h [help]\r\n"
|
|
|
|
|
" -s http_server_addr [www.sina.com.cn:80]\r\n"
|
2020-03-14 20:52:16 +08:00
|
|
|
|
" -H host\r\n"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
" -z [unzip response body, default: false]\r\n"
|
|
|
|
|
" -c cocurrent [default: 10]\r\n"
|
2020-03-14 20:52:16 +08:00
|
|
|
|
" -D [if in debug mode]\r\n"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
" -n loop_count[default: 10]\r\n", procname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
2021-08-22 23:15:38 +08:00
|
|
|
|
int ch, cocurrent = 1;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
string addr("www.sina.com.cn:80");
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC> acl <20><>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::acl_cpp_init();
|
2021-08-22 23:15:38 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-03-14 20:52:16 +08:00
|
|
|
|
while ((ch = getopt(argc, argv, "hs:H:n:c:zD")) > 0) {
|
|
|
|
|
switch (ch) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 0;
|
|
|
|
|
case 's':
|
|
|
|
|
addr = optarg;
|
|
|
|
|
break;
|
2020-03-14 20:52:16 +08:00
|
|
|
|
case 'H':
|
|
|
|
|
__host = optarg;
|
|
|
|
|
break;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
case 'c':
|
|
|
|
|
cocurrent = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
__loop_count = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'z':
|
|
|
|
|
__unzip = true;
|
|
|
|
|
break;
|
2020-03-14 20:52:16 +08:00
|
|
|
|
case 'D':
|
|
|
|
|
__debug = true;
|
|
|
|
|
break;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
default:
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-14 20:52:16 +08:00
|
|
|
|
if (__host.empty()) {
|
|
|
|
|
__host = addr;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
init(addr, cocurrent);
|
|
|
|
|
run(cocurrent);
|
|
|
|
|
end();
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
printf("enter any key to exit...\r\n");
|
|
|
|
|
getchar();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|