acl/lib_acl_cpp/samples/thread_client/main.cpp

91 lines
1.9 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#include "stdafx.h"
#include "util.h"
#include "thread_client.h"
static void usage(const char* procname)
{
printf("usage: %s -h [help]\r\n"
" -s server_addr [default: 127.0.0.1:8888]\r\n"
" -k [keep alive, default: false]\r\n"
" -L data_length [default: 1024]\r\n"
" -c cocurrent [default: 1]\r\n"
" -n count [default: 10]\r\n", procname);
}
int main(int argc, char* argv[])
{
int ch, cocurrent = 1, count = 10, length = 1024;
bool keep_alive = false;
acl::string server_addr("127.0.0.1:8888");
// <20><>ʼ<EFBFBD><CABC> acl <20><>
acl::acl_cpp_init();
while ((ch = getopt(argc, argv, "hs:c:n:k")) > 0)
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 'c':
cocurrent = atoi(optarg);
break;
case 'n':
count = atoi(optarg);
break;
case 'k':
keep_alive = true;
break;
case 's':
server_addr = optarg;
break;
default:
break;
}
}
struct timeval begin;
gettimeofday(&begin, NULL);
std::list<thread_client*> threads;
for (int i = 0; i < cocurrent; i++)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>߳
thread_client* thread = new thread_client(server_addr, keep_alive,
count, length);
// <20><><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>Ϊ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ģʽ<C4A3><CABD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><D4B5> thread::wait
// <20>ȴ<EFBFBD><C8B4>߳̽<DFB3><CCBD><EFBFBD>
thread->set_detachable(false);
// <20><><EFBFBD>̷߳<DFB3><CCB7>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD>
threads.push_back(thread);
// <20><><EFBFBD><EFBFBD><EFBFBD>߳
thread->start();
}
std::list<thread_client*>::iterator it = threads.begin();
for (; it != threads.end(); ++it)
{
// <20>ȴ<EFBFBD><C8B4>߳̽<DFB3><CCBD><EFBFBD>
if ((*it)->wait(NULL) == false)
printf("wait one thread(%lu) error\r\n",
(*it)->thread_id());
// ɾ<><C9BE><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̶߳<DFB3><CCB6><EFBFBD>
delete *it;
}
struct timeval end;
gettimeofday(&end, NULL);
double spent = util::stamp_sub(&end, &begin);
printf("total: %d, spent: %.2f, speed: %.f\r\n", count,
spent, (count * 1000) / (spent > 1 ? spent : 1));
return 0;
}