acl/lib_acl_cpp/samples/thread_client/thread_client.cpp

86 lines
1.7 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#include "stdafx.h"
#include "util.h"
#include "thread_client.h"
thread_client::thread_client(const char* server_addr, bool keep_alive,
int count, int length)
: server_addr_(server_addr)
, keep_alive_(keep_alive)
, count_(count)
, length_(length)
{
}
thread_client::~thread_client()
{
}
void* thread_client::run()
{
acl::socket_stream conn;
// <20><><EFBFBD><EFBFBD>Զ<EFBFBD>̷<EFBFBD><CCB7><EFBFBD><EFBFBD><EFBFBD>
if (conn.open(server_addr_, 10, 10) == false)
{
printf("connect %s error %s\r\n", server_addr_.c_str(),
acl::last_serror());
return NULL;
}
char* data = (char*) acl_mymalloc(length_);
memset(data, 'X', length_);
data[length_ - 1] = '\n';
acl::string buf;
struct timeval begin;
gettimeofday(&begin, NULL);
int i = 0;
for (; i < count_; i++)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>дһ<D0B4><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (conn.write(data, length_) == -1)
{
printf("write to %s error %s\r\n",
server_addr_.c_str(), acl::last_serror());
break;
}
// <20>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (conn.gets(buf) == false)
{
printf("gets from %s error %s\r\n",
server_addr_.c_str(), acl::last_serror());
break;
}
if (i % 1000 == 0)
{
buf.format("total: %d, curr: %d", count_, i);
ACL_METER_TIME(buf.c_str());
}
if (keep_alive_)
continue;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD>ȹر<C8B9><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><D9B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
conn.close();
if (conn.open(server_addr_.c_str(), 10, 10) == false)
{
printf("connect %s error %s\r\n", server_addr_.c_str(),
acl::last_serror());
break;
}
}
struct timeval end;
gettimeofday(&end, NULL);
double spent = util::stamp_sub(&end, &begin);
printf("total: %d, curr: %d, spent: %.2f, speed: %.2f\r\n",
count_, i, spent, (i * 1000) / (spent > 1 ? spent : 1));
acl_myfree(data);
return NULL;
}