1 // This tests the features of the <ACE_SOCK_Connector> and
2 // <ACE_SOCK_Stream> classes. In addition, it can be used to test the
3 // oneway and twoway latency and throughput at the socket-level. This
4 // is useful as a baseline to compare against ORB-level performance
5 // for the same types of data.
7 #include "CPP-inclient.h"
9 #include "ace/SOCK_Connector.h"
10 #include "ace/INET_Addr.h"
11 #include "ace/Thread_Manager.h"
12 #include "ace/Singleton.h"
13 #include "ace/Get_Opt.h"
14 #include "ace/Truncate.h"
15 #include "ace/High_Res_Timer.h"
16 #include "ace/Basic_Types.h"
17 #include "ace/OS_NS_string.h"
18 #include "ace/OS_NS_unistd.h"
19 #include "ace/OS_main.h"
22 : host_ (ACE_DEFAULT_SERVER_HOST
),
23 port_ (ACE_DEFAULT_SERVER_PORT
),
24 sleep_time_ (0, 0), // By default, don't sleep between calls.
26 quit_string_ (ACE_TEXT("q")),
29 io_source_ (ACE_INVALID_HANDLE
), // Defaults to using the generator.
31 oneway_ (1) // Make oneway calls the default.
32 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
34 #endif /* ACE_MT_SAFE */
40 ACE_MT (delete this->barrier_
);
41 delete [] this->message_buf_
;
45 typedef ACE_Singleton
<Options
, ACE_SYNCH_RECURSIVE_MUTEX
> OPTIONS
;
50 ACE_DEBUG((LM_DEBUG
,"Options::init, len = %d\n",this->message_len_
));
52 // Check for default case.
53 if (this->message_len_
== 0)
54 this->message_len_
= ACE_OS::strlen ("TAO");
55 ACE_DEBUG((LM_DEBUG
,"Options::init, len = %d\n",this->message_len_
));
57 this->message_len_
+= sizeof (ACE_UINT32
);
58 ACE_DEBUG((LM_DEBUG
,"Options::init, len = %d\n",this->message_len_
));
60 ACE_NEW_RETURN (this->message_buf_
,
61 char[this->message_len_
],
64 // Copy the length into the beginning of the message.
66 ACE_NTOHL (ACE_Utils::truncate_cast
<ACE_UINT32
> (this->message_len_
));
68 ACE_OS::memcpy ((void *) this->message_buf_
,
72 ACE_OS::memset ((void *) (this->message_buf_
+ sizeof (ACE_UINT32
)),
74 this->message_len_
- sizeof (ACE_UINT32
));
76 // Allocate the barrier with the correct count.
77 ACE_MT (ACE_NEW_RETURN (this->barrier_
,
79 ACE_Utils::truncate_cast
<unsigned int> (
86 Options::message_len () const
88 return this->message_len_
;
92 Options::message_buf () const
94 return this->message_buf_
;
98 Options::read (void *buf
, size_t len
, size_t &iteration
)
100 ACE_UNUSED_ARG (len
);
102 if (this->io_source_
== ACE_STDIN
)
103 return ACE_OS::read (ACE_STDIN
, buf
, len
);
104 else if (iteration
>= this->iterations_
)
109 this->message_buf (),
112 return ACE_Utils::truncate_cast
<ssize_t
> (len
);
117 Options::parse_args (int argc
, ACE_TCHAR
*argv
[])
119 //FUZZ: disable check_for_lack_ACE_OS
120 ACE_Get_Opt
getopt (argc
, argv
, ACE_TEXT("2h:i:m:p:q:st:T:"), 1);
122 for (int c
; (c
= getopt ()) != -1; )
123 //FUZZ: enable check_for_lack_ACE_OS
126 case '2': // Disable the oneway client.
130 this->host_
= getopt
.opt_arg ();
133 this->iterations_
= ACE_OS::atoi (getopt
.opt_arg ());
136 this->message_len_
= ACE_OS::atoi (getopt
.opt_arg ());
139 this->port_
= ACE_OS::atoi (getopt
.opt_arg ());
142 this->quit_string_
= getopt
.opt_arg ();
145 this->io_source_
= ACE_STDIN
;
148 this->threads_
= (size_t) ACE_OS::atoi (getopt
.opt_arg ());
151 this->sleep_time_
.set (0, ACE_OS::atoi (getopt
.opt_arg ()));
154 ACE_ERROR_RETURN ((LM_ERROR
,
155 "(%P|%t) usage: %n [-2] [-h <host>] [-i iterations] [-m message-size] [-p <port>] [-q <quit string>] [-s] [-t <threads>] [-T <sleep_time>]\n"),
159 return this->init ();
163 Options::port () const
169 Options::host () const
175 Options::quit_string () const
177 return this->quit_string_
;
181 Options::threads () const
183 return this->threads_
;
186 const ACE_Time_Value
&
187 Options::sleep_time () const
189 return this->sleep_time_
;
193 Options::shared_client_test (u_short port
,
194 ACE_SOCK_Stream
&cli_stream
)
196 ACE_INET_Addr
remote_addr (port
, this->host_
);
198 ACE_SOCK_Connector con
;
200 if (con
.connect (cli_stream
,
202 ACE_ERROR_RETURN ((LM_ERROR
,
204 "connection failed"),
207 ACE_DEBUG ((LM_DEBUG
,
208 "(%P|%t) connected to %s at port %d\n",
209 remote_addr
.get_host_name (),
210 remote_addr
.get_port_number ()));
213 ACE_HTONL (ACE_Utils::truncate_cast
<ACE_INT32
> (this->message_len ()));
215 // Allocate the transmit buffer.
217 ACE_DEBUG((LM_DEBUG
,"(%P|%t) allocating buffer, len = %d msglen = %d\n",
221 char[this->message_len()],
224 ACE_DEBUG ((LM_DEBUG
,
225 "(%P|%t) waiting...\n"));
227 // Wait for all other threads to finish initialization.
228 ACE_MT (this->barrier_
->wait ());
231 // Static function entry point to the oneway client service.
234 Options::oneway_client_test (void *)
236 Options
*options
= OPTIONS::instance ();
237 ACE_SOCK_Stream cli_stream
;
239 ACE_DEBUG((LM_DEBUG
,"options = %d, len = %d\n",options
,options
->message_len()));
241 // Add 1 to the port to trigger the oneway test!
242 char *request
= options
->shared_client_test (options
->port () + 1,
247 // This variable is allocated off the stack to obviate the need for
249 size_t iteration
= 0;
251 // Keep track of return value.
254 ACE_Utils::truncate_cast
<ACE_INT32
> (options
->message_len ());
256 ACE_DEBUG ((LM_DEBUG
,
257 "(%P|%t) starting oneway transmission\n"));
259 // Perform oneway transmission of data to server (correctly handles
260 // "incomplete writes").
262 for (ssize_t r_bytes
;
263 (r_bytes
= options
->read (request
, len
, iteration
)) > 0;
264 // Transmit at the proper rate.
265 ACE_OS::sleep (options
->sleep_time ()))
266 if (ACE_OS::memcmp (request
,
267 options
->quit_string (),
268 ACE_OS::strlen (options
->quit_string ())) == 0)
270 else if (cli_stream
.send_n (request
, r_bytes
) == -1)
272 ACE_ERROR ((LM_ERROR
,
279 // Close the connection.
283 return reinterpret_cast<void *> (result
);
286 // Static function entry point to the twoway client service.
289 Options::twoway_client_test (void *)
291 Options
*options
= OPTIONS::instance ();
293 ACE_SOCK_Stream cli_stream
;
295 char *request
= options
->shared_client_test (options
->port (),
300 // This variable is allocated off the stack to obviate the need for
302 size_t iteration
= 0;
304 // Keep track of return value.
308 ACE_High_Res_Timer timer
;
311 ACE_Utils::truncate_cast
<ACE_INT32
> (options
->message_len ());
313 ACE_DEBUG ((LM_DEBUG
,
314 "(%P|%t) starting twoway transmission\n"));
316 // Perform twoway transmission of data to server (correctly handles
317 // "incomplete writes").
319 for (ssize_t r_bytes
;
320 (r_bytes
= options
->read (request
, len
, iteration
)) > 0;
321 // Transmit at the proper rate.
322 ACE_OS::sleep (options
->sleep_time ()))
323 if (ACE_OS::memcmp (request
,
324 options
->quit_string (),
325 ACE_OS::strlen (options
->quit_string ())) == 0)
328 // Transmit <request> to the server.
331 // Note that we use the incremental feature of the
332 // <ACE_High_Res_Timer> so that we don't get "charged" for the
333 // <ACE_OS::sleep> used to control the rate at which requests
337 if (cli_stream
.send_n (request
, r_bytes
) == -1)
339 ACE_ERROR ((LM_ERROR
,
345 // Receive the reply from the server. Normally, it just sends
346 // back 24 bytes, which is typical for an IIOP reply.
347 else if (cli_stream
.recv (request
, r_bytes
) <= 0)
349 ACE_ERROR ((LM_ERROR
,
361 timer
.elapsed_time_incr (tv
);
362 double real_time
= (long) tv
.sec () * ACE_ONE_SECOND_IN_USECS
+ tv
.usec ();
363 double messages_per_sec
= iteration
* double (ACE_ONE_SECOND_IN_USECS
) / real_time
;
365 ACE_DEBUG ((LM_DEBUG
,
366 ACE_TEXT ("(%t) messages = %d\n(%t) usec-per-message = %f\n(%t) messages-per-second = %0.00f\n"),
368 real_time
/ double (iteration
),
369 messages_per_sec
< 0 ? 0 : messages_per_sec
));
371 // Close the connection.
375 return reinterpret_cast<void *> (result
);
381 ACE_DEBUG((LM_DEBUG
,"(%P|%t) in thread func, mesg len = %d\n",this->message_len()));
382 if (this->oneway_
== 0)
383 return ACE_THR_FUNC (&Options::twoway_client_test
);
385 return ACE_THR_FUNC (&Options::oneway_client_test
);
391 // Raise the socket handle limit to the maximum.
392 ACE::set_handle_limit ();
394 #if defined (ACE_HAS_THREADS)
395 ACE_DEBUG((LM_DEBUG
,"(%P|%t) spawning client test thread options = %d\n",
396 OPTIONS::instance()));
398 if (ACE_Thread_Manager::instance ()->spawn_n (OPTIONS::instance ()->threads (),
399 OPTIONS::instance ()->thr_func ()) == -1)
400 ACE_ERROR_RETURN ((LM_ERROR
,
405 ACE_Thread_Manager::instance ()->wait ();
407 (void) *(OPTIONS::instance ()->thr_func
) ();
408 #endif /* ACE_HAS_THREADS */
413 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
415 // Initialize the logger.
416 ACE_LOG_MSG
->open (argv
[0]);
418 if (OPTIONS::instance ()->parse_args (argc
, argv
) == -1)
427 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton
, Options
, ACE_SYNCH_RECURSIVE_MUTEX
);