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;
|
|
|
|
|
|
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();
|
2018-02-12 18:52:51 +08:00
|
|
|
|
header.set_host("www.sina.com.cn")
|
|
|
|
|
.set_url("/")
|
2014-11-19 00:25:21 +08:00
|
|
|
|
.set_keep_alive(true)
|
|
|
|
|
.set_method(HTTP_METHOD_GET)
|
|
|
|
|
.accept_gzip(__unzip);
|
|
|
|
|
|
2017-06-24 12:16:18 +08:00
|
|
|
|
if (0)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
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><>Ӧͷ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (conn->request(NULL, 0) == false)
|
|
|
|
|
{
|
|
|
|
|
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>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
ret = conn->read_body(buf, sizeof(buf));
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
break;
|
|
|
|
|
else if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
printf("%lu--%d: error, length: %d\r\n",
|
|
|
|
|
(unsigned long) acl_pthread_self(), n, length);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
length += ret;
|
|
|
|
|
if (0)
|
|
|
|
|
printf("%lu--%d: read length: %d, %d\r\n",
|
|
|
|
|
(unsigned long) acl_pthread_self(), n, length, ret);
|
|
|
|
|
}
|
2017-06-24 12:16:18 +08:00
|
|
|
|
if (0)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
printf("%lu--%d: read body over, length: %d\r\n",
|
|
|
|
|
(unsigned long) acl_pthread_self(), n, length);
|
|
|
|
|
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*)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
if (conn == NULL)
|
|
|
|
|
{
|
|
|
|
|
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>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
else
|
|
|
|
|
conn->reset();
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD>µ<EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (http_get(conn, i) == false)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
__conn_pool->put(conn, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
for (int i = 0; i < cocurrent; i++)
|
|
|
|
|
acl_pthread_pool_add(__thr_pool, thread_main, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s -h [help]\r\n"
|
|
|
|
|
" -s http_server_addr [www.sina.com.cn:80]\r\n"
|
|
|
|
|
" -z [unzip response body, default: false]\r\n"
|
|
|
|
|
" -c cocurrent [default: 10]\r\n"
|
|
|
|
|
" -n loop_count[default: 10]\r\n", procname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
int ch, cocurrent = 10;
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hs:n:c:z")) > 0)
|
|
|
|
|
{
|
|
|
|
|
switch (ch)
|
|
|
|
|
{
|
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 0;
|
|
|
|
|
case 's':
|
|
|
|
|
addr = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'c':
|
|
|
|
|
cocurrent = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
__loop_count = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'z':
|
|
|
|
|
__unzip = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(addr, cocurrent);
|
|
|
|
|
run(cocurrent);
|
|
|
|
|
end();
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
printf("enter any key to exit...\r\n");
|
|
|
|
|
getchar();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|