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
|
// $Id: CPP-inclient.cpp 91671 2010-09-08 18:39:23Z johnnyw $
// This tests the features of the <ACE_SOCK_Connector> and
// <ACE_SOCK_Stream> classes. In addition, it can be used to test the
// oneway and twoway latency and throughput at the socket-level. This
// is useful as a baseline to compare against ORB-level performance
// for the same types of data.
#include "CPP-inclient.h"
#include "ace/SOCK_Connector.h"
#include "ace/INET_Addr.h"
#include "ace/Thread_Manager.h"
#include "ace/Singleton.h"
#include "ace/Get_Opt.h"
#include "ace/Truncate.h"
#include "ace/High_Res_Timer.h"
#include "ace/Basic_Types.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_main.h"
Options::Options (void)
: host_ (ACE_DEFAULT_SERVER_HOST),
port_ (ACE_DEFAULT_SERVER_PORT),
sleep_time_ (0, 0), // By default, don't sleep between calls.
threads_ (10),
quit_string_ (ACE_TEXT("q")),
message_len_ (0),
message_buf_ (0),
io_source_ (ACE_INVALID_HANDLE), // Defaults to using the generator.
iterations_ (10000),
oneway_ (1) // Make oneway calls the default.
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
, barrier_ (0)
#endif /* ACE_MT_SAFE */
{
}
Options::~Options (void)
{
ACE_MT (delete this->barrier_);
delete [] this->message_buf_;
}
// Options Singleton.
typedef ACE_Singleton<Options, ACE_SYNCH_RECURSIVE_MUTEX> OPTIONS;
int
Options::init (void)
{
ACE_DEBUG((LM_DEBUG,"Options::init, len = %d\n",this->message_len_));
// Check for default case.
if (this->message_len_ == 0)
this->message_len_ = ACE_OS::strlen ("TAO");
ACE_DEBUG((LM_DEBUG,"Options::init, len = %d\n",this->message_len_));
this->message_len_ += sizeof (ACE_UINT32);
ACE_DEBUG((LM_DEBUG,"Options::init, len = %d\n",this->message_len_));
ACE_NEW_RETURN (this->message_buf_,
char[this->message_len_],
-1);
// Copy the length into the beginning of the message.
ACE_UINT32 length =
ACE_NTOHL (ACE_Utils::truncate_cast<ACE_UINT32> (this->message_len_));
ACE_OS::memcpy ((void *) this->message_buf_,
(void *) &length,
sizeof length);
ACE_OS::memset ((void *) (this->message_buf_ + sizeof (ACE_UINT32)),
'a',
this->message_len_ - sizeof (ACE_UINT32));
// Allocate the barrier with the correct count.
ACE_MT (ACE_NEW_RETURN (this->barrier_,
ACE_Barrier (
ACE_Utils::truncate_cast<unsigned int> (
this->threads_)),
-1));
return 0;
}
size_t
Options::message_len (void) const
{
return this->message_len_;
}
const void *
Options::message_buf (void) const
{
return this->message_buf_;
}
ssize_t
Options::read (void *buf, size_t len, size_t &iteration)
{
ACE_UNUSED_ARG (len);
if (this->io_source_ == ACE_STDIN)
return ACE_OS::read (ACE_STDIN, buf, len);
else if (iteration >= this->iterations_)
return 0;
else
{
ACE_OS::memcpy (buf,
this->message_buf (),
len);
iteration++;
return ACE_Utils::truncate_cast<ssize_t> (len);
}
}
int
Options::parse_args (int argc, ACE_TCHAR *argv[])
{
//FUZZ: disable check_for_lack_ACE_OS
ACE_Get_Opt getopt (argc, argv, ACE_TEXT("2h:i:m:p:q:st:T:"), 1);
for (int c; (c = getopt ()) != -1; )
//FUZZ: enable check_for_lack_ACE_OS
switch (c)
{
case '2': // Disable the oneway client.
this->oneway_ = 0;
break;
case 'h':
this->host_ = getopt.opt_arg ();
break;
case 'i':
this->iterations_ = ACE_OS::atoi (getopt.opt_arg ());
break;
case 'm':
this->message_len_ = ACE_OS::atoi (getopt.opt_arg ());
break;
case 'p':
this->port_ = ACE_OS::atoi (getopt.opt_arg ());
break;
case 'q':
this->quit_string_ = getopt.opt_arg ();
break;
case 's':
this->io_source_ = ACE_STDIN;
break;
case 't':
this->threads_ = (size_t) ACE_OS::atoi (getopt.opt_arg ());
break;
case 'T':
this->sleep_time_.set (0, ACE_OS::atoi (getopt.opt_arg ()));
break;
default:
ACE_ERROR_RETURN ((LM_ERROR,
"(%P|%t) usage: %n [-2] [-h <host>] [-i iterations] [-m message-size] [-p <port>] [-q <quit string>] [-s] [-t <threads>] [-T <sleep_time>]\n"),
-1);
}
return this->init ();
}
u_short
Options::port (void) const
{
return this->port_;
}
const ACE_TCHAR *
Options::host (void) const
{
return this->host_;
}
const ACE_TCHAR *
Options::quit_string (void) const
{
return this->quit_string_;
}
size_t
Options::threads (void) const
{
return this->threads_;
}
const ACE_Time_Value &
Options::sleep_time (void) const
{
return this->sleep_time_;
}
char *
Options::shared_client_test (u_short port,
ACE_SOCK_Stream &cli_stream)
{
ACE_INET_Addr remote_addr (port, this->host_);
ACE_SOCK_Connector con;
if (con.connect (cli_stream,
remote_addr) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"(%P|%t) %p\n",
"connection failed"),
0);
else
ACE_DEBUG ((LM_DEBUG,
"(%P|%t) connected to %s at port %d\n",
remote_addr.get_host_name (),
remote_addr.get_port_number ()));
ACE_INT32 len =
ACE_HTONL (ACE_Utils::truncate_cast<ACE_INT32> (this->message_len ()));
// Allocate the transmit buffer.
char *buf;
ACE_DEBUG((LM_DEBUG,"(%P|%t) allocating buffer, len = %d msglen = %d\n",
len, message_len_));
ACE_NEW_RETURN (buf,
char[this->message_len()],
0);
ACE_DEBUG ((LM_DEBUG,
"(%P|%t) waiting...\n"));
// Wait for all other threads to finish initialization.
ACE_MT (this->barrier_->wait ());
return buf;
}
// Static function entry point to the oneway client service.
void *
Options::oneway_client_test (void *)
{
Options *options = OPTIONS::instance ();
ACE_SOCK_Stream cli_stream;
ACE_DEBUG((LM_DEBUG,"options = %d, len = %d\n",options,options->message_len()));
// Add 1 to the port to trigger the oneway test!
char *request = options->shared_client_test (options->port () + 1,
cli_stream);
if (request == 0)
return 0;
// This variable is allocated off the stack to obviate the need for
// locking.
size_t iteration = 0;
// Keep track of return value.
intptr_t result = 0;
ACE_INT32 len =
ACE_Utils::truncate_cast<ACE_INT32> (options->message_len ());
ACE_DEBUG ((LM_DEBUG,
"(%P|%t) starting oneway transmission\n"));
// Perform oneway transmission of data to server (correctly handles
// "incomplete writes").
for (ssize_t r_bytes;
(r_bytes = options->read (request, len, iteration)) > 0;
// Transmit at the proper rate.
ACE_OS::sleep (options->sleep_time ()))
if (ACE_OS::memcmp (request,
options->quit_string (),
ACE_OS::strlen (options->quit_string ())) == 0)
break;
else if (cli_stream.send_n (request, r_bytes) == -1)
{
ACE_ERROR ((LM_ERROR,
"(%P|%t) %p\n",
"send_n"));
result = -1;
break;
}
// Close the connection.
cli_stream.close ();
delete [] request;
return reinterpret_cast<void *> (result);
}
// Static function entry point to the twoway client service.
void *
Options::twoway_client_test (void *)
{
Options *options = OPTIONS::instance ();
ACE_SOCK_Stream cli_stream;
char *request = options->shared_client_test (options->port (),
cli_stream);
if (request == 0)
return 0;
// This variable is allocated off the stack to obviate the need for
// locking.
size_t iteration = 0;
// Keep track of return value.
intptr_t result = 0;
// Timer business.
ACE_High_Res_Timer timer;
ACE_INT32 len =
ACE_Utils::truncate_cast<ACE_INT32> (options->message_len ());
ACE_DEBUG ((LM_DEBUG,
"(%P|%t) starting twoway transmission\n"));
// Perform twoway transmission of data to server (correctly handles
// "incomplete writes").
for (ssize_t r_bytes;
(r_bytes = options->read (request, len, iteration)) > 0;
// Transmit at the proper rate.
ACE_OS::sleep (options->sleep_time ()))
if (ACE_OS::memcmp (request,
options->quit_string (),
ACE_OS::strlen (options->quit_string ())) == 0)
break;
// Transmit <request> to the server.
else
{
// Note that we use the incremental feature of the
// <ACE_High_Res_Timer> so that we don't get "charged" for the
// <ACE_OS::sleep> used to control the rate at which requests
// are sent.
timer.start_incr ();
if (cli_stream.send_n (request, r_bytes) == -1)
{
ACE_ERROR ((LM_ERROR,
"(%P|%t) %p\n",
"send_n"));
result = -1;
break;
}
// Receive the reply from the server. Normally, it just sends
// back 24 bytes, which is typical for an IIOP reply.
else if (cli_stream.recv (request, r_bytes) <= 0)
{
ACE_ERROR ((LM_ERROR,
"(%P|%t) %p\n",
"recv"));
result = -1;
break;
}
timer.stop_incr ();
}
ACE_Time_Value tv;
timer.elapsed_time_incr (tv);
double real_time = (long) tv.sec () * ACE_ONE_SECOND_IN_USECS + tv.usec ();
double messages_per_sec = iteration * double (ACE_ONE_SECOND_IN_USECS) / real_time;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) messages = %d\n(%t) usec-per-message = %f\n(%t) messages-per-second = %0.00f\n"),
iteration,
real_time / double (iteration),
messages_per_sec < 0 ? 0 : messages_per_sec));
// Close the connection.
cli_stream.close ();
delete [] request;
return reinterpret_cast<void *> (result);
}
ACE_THR_FUNC
Options::thr_func (void)
{
ACE_DEBUG((LM_DEBUG,"(%P|%t) in thread func, mesg len = %d\n",this->message_len()));
if (this->oneway_ == 0)
return ACE_THR_FUNC (&Options::twoway_client_test);
else
return ACE_THR_FUNC (&Options::oneway_client_test);
}
static int
run_client (void)
{
// Raise the socket handle limit to the maximum.
ACE::set_handle_limit ();
#if defined (ACE_HAS_THREADS)
ACE_DEBUG((LM_DEBUG,"(%P|%t) spawning client test thread options = %d\n",
OPTIONS::instance()));
if (ACE_Thread_Manager::instance ()->spawn_n (OPTIONS::instance ()->threads (),
OPTIONS::instance ()->thr_func ()) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"(%P|%t) %p\n",
"spawn_n"),
1);
else
ACE_Thread_Manager::instance ()->wait ();
#else
*(OPTIONS::instance ()->thr_func) ();
#endif /* ACE_HAS_THREADS */
return 0;
}
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
// Initialize the logger.
ACE_LOG_MSG->open (argv[0]);
if (OPTIONS::instance ()->parse_args (argc, argv) == -1)
return -1;
// Run the client
run_client ();
return 0;
}
#if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
template ACE_Singleton<Options, ACE_SYNCH_RECURSIVE_MUTEX> *
ACE_Singleton<Options, ACE_SYNCH_RECURSIVE_MUTEX>::singleton_;
#endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
|