1 // This tests the features of the <ACE_SSL_SOCK_Connector> and
2 // <ACE_SSL_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 "ace/OS_NS_string.h"
8 #include "ace/OS_NS_unistd.h"
9 #include "ace/INET_Addr.h"
10 #include "ace/Thread_Manager.h"
11 #include "ace/Singleton.h"
12 #include "ace/Get_Opt.h"
13 #include "ace/High_Res_Timer.h"
14 #include "ace/Truncate.h"
16 #include "ace/SSL/SSL_SOCK_Connector.h"
18 #include "SSL-client.h"
21 : host_ (ACE_DEFAULT_SERVER_HOST
),
22 port_ (ACE_DEFAULT_SERVER_PORT
),
23 sleep_time_ (0, 0), // By default, don't sleep between calls.
27 io_source_ (ACE_INVALID_HANDLE
), // Defaults to using the generator.
29 oneway_ (1) // Make oneway calls the default.
30 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
32 #endif /* ACE_MT_SAFE */
34 ACE_OS::strcpy (quit_string_
, "q");
39 ACE_MT (delete this->barrier_
);
40 delete [] this->message_buf_
;
44 typedef ACE_Singleton
<Options
, ACE_SYNCH_RECURSIVE_MUTEX
> OPTIONS
;
49 // Check for default case.
50 if (this->message_len_
== 0)
51 this->message_len_
= ACE_OS::strlen ("TAO");
53 this->message_len_
+= sizeof (ACE_UINT32
);
55 ACE_NEW_RETURN (this->message_buf_
,
56 char[this->message_len_
],
59 // Copy the length into the beginning of the message.
61 ntohl (ACE_Utils::truncate_cast
<u_long
> (this->message_len_
));
63 ACE_OS::memcpy ((void *) this->message_buf_
,
67 ACE_OS::memset ((void *) (this->message_buf_
+ sizeof (ACE_UINT32
)),
69 this->message_len_
- sizeof (ACE_UINT32
));
71 // Allocate the barrier with the correct count.
72 ACE_MT (ACE_NEW_RETURN (this->barrier_
,
74 ACE_Utils::truncate_cast
<unsigned int> (
81 Options::message_len () const
83 return this->message_len_
;
87 Options::message_buf () const
89 return this->message_buf_
;
93 Options::read (void *buf
, size_t len
, size_t &iteration
)
97 if (this->io_source_
== ACE_STDIN
)
98 return ACE_OS::read (ACE_STDIN
, buf
, len
);
99 else if (iteration
>= this->iterations_
)
104 this->message_buf (),
107 return ACE_Utils::truncate_cast
<ssize_t
> (len
);
112 Options::parse_args (int argc
, ACE_TCHAR
*argv
[])
114 //FUZZ: disable check_for_lack_ACE_OS
115 ACE_Get_Opt
getopt (argc
, argv
, ACE_TEXT ("2h:i:m:p:q:st:T:"), 1);
117 for (int c
; (c
= getopt ()) != -1; )
118 //FUZZ: enable check_for_lack_ACE_OS
121 case '2': // Disable the oneway client.
125 this->host_
= getopt
.opt_arg ();
128 this->iterations_
= ACE_OS::atoi (getopt
.opt_arg ());
131 this->message_len_
= ACE_OS::atoi (getopt
.opt_arg ());
134 this->port_
= ACE_OS::atoi (getopt
.opt_arg ());
137 ACE_OS::strncpy (this->quit_string_
,
138 ACE_TEXT_ALWAYS_CHAR (getopt
.opt_arg ()),
142 this->io_source_
= ACE_STDIN
;
145 this->threads_
= (size_t) ACE_OS::atoi (getopt
.opt_arg ());
148 this->sleep_time_
.set (0, ACE_OS::atoi (getopt
.opt_arg ()));
151 ACE_ERROR_RETURN ((LM_ERROR
,
152 ACE_TEXT ("(%P|%t) usage: %n [-2] [-h <host>] ")
153 ACE_TEXT ("[-i iterations] [-m message-size] ")
154 ACE_TEXT ("[-p <port>] [-q <quit string>] ")
155 ACE_TEXT ("[-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_SSL_SOCK_Stream
&cli_stream
)
196 ACE_INET_Addr
remote_addr (port
, this->host_
);
198 ACE_SSL_SOCK_Connector con
;
200 if (con
.connect (cli_stream
,
202 ACE_ERROR_RETURN ((LM_ERROR
,
203 ACE_TEXT ("(%P|%t) %p\n"),
204 ACE_TEXT ("connection failed")),
207 ACE_DEBUG ((LM_DEBUG
,
208 ACE_TEXT ("(%P|%t) connected to %C at port %d\n"),
209 remote_addr
.get_host_name (),
210 remote_addr
.get_port_number ()));
212 // Allocate the transmit buffer.
215 char[this->message_len ()],
218 ACE_DEBUG ((LM_DEBUG
,
219 ACE_TEXT ("(%P|%t) waiting...\n")));
221 // Wait for all other threads to finish initialization.
222 ACE_MT (this->barrier_
->wait ());
225 // Static function entry point to the oneway client service.
228 Options::oneway_client_test (void *)
230 Options
*options
= OPTIONS::instance ();
231 ACE_SSL_SOCK_Stream cli_stream
;
233 // Add 1 to the port to trigger the oneway test!
234 char *request
= options
->shared_client_test (options
->port () + 1,
239 // This variable is allocated off the stack to obviate the need for
241 size_t iteration
= 0;
243 // Keep track of return value.
246 ACE_Utils::truncate_cast
<ACE_INT32
> (options
->message_len ());
248 ACE_DEBUG ((LM_DEBUG
,
249 ACE_TEXT ("(%P|%t) starting oneway transmission\n")));
251 // Perform oneway transmission of data to server (correctly handles
252 // "incomplete writes").
254 for (ssize_t r_bytes
;
255 (r_bytes
= options
->read (request
, len
, iteration
)) > 0;
256 // Transmit at the proper rate.
257 ACE_OS::sleep (options
->sleep_time ()))
258 if (ACE_OS::memcmp (request
,
259 options
->quit_string (),
260 ACE_OS::strlen (options
->quit_string ())) == 0)
262 else if (cli_stream
.send_n (request
, r_bytes
) == -1)
264 ACE_ERROR ((LM_ERROR
,
265 ACE_TEXT ("(%P|%t) %p\n"),
266 ACE_TEXT ("send_n")));
267 result
= size_t (-1);
271 // Close the connection.
275 return (void *) result
;
278 // Static function entry point to the twoway client service.
281 Options::twoway_client_test (void *)
283 Options
*options
= OPTIONS::instance ();
285 ACE_SSL_SOCK_Stream cli_stream
;
287 char *request
= options
->shared_client_test (options
->port (),
292 // This variable is allocated off the stack to obviate the need for
294 size_t iteration
= 0;
296 // Keep track of return value.
300 ACE_High_Res_Timer timer
;
303 ACE_Utils::truncate_cast
<ACE_INT32
> (options
->message_len ());
305 ACE_DEBUG ((LM_DEBUG
,
306 ACE_TEXT ("(%P|%t) starting twoway transmission\n")));
308 // Perform twoway transmission of data to server (correctly handles
309 // "incomplete writes").
311 for (ssize_t r_bytes
;
312 (r_bytes
= options
->read (request
, len
, iteration
)) > 0;
313 // Transmit at the proper rate.
314 ACE_OS::sleep (options
->sleep_time ()))
315 if (ACE_OS::memcmp (request
,
316 options
->quit_string (),
317 ACE_OS::strlen (options
->quit_string ())) == 0)
320 // Transmit <request> to the server.
323 // Note that we use the incremental feature of the
324 // <ACE_High_Res_Timer> so that we don't get "charged" for the
325 // <ACE_OS::sleep> used to control the rate at which requests
329 if (cli_stream
.send_n (request
, r_bytes
) == -1)
331 ACE_ERROR ((LM_ERROR
,
332 ACE_TEXT ("(%P|%t) %p\n"),
333 ACE_TEXT ("send_n")));
334 result
= size_t (-1);
337 // Receive the reply from the server. Normally, it just sends
338 // back 24 bytes, which is typical for an IIOP reply.
339 else if (cli_stream
.recv (request
, r_bytes
) <= 0)
341 ACE_ERROR ((LM_ERROR
,
342 ACE_TEXT ("(%P|%t) %p\n"),
344 result
= size_t (-1);
353 timer
.elapsed_time_incr (tv
);
354 double real_time
= static_cast<double> (tv
.sec () * ACE_ONE_SECOND_IN_USECS
+ tv
.usec ());
355 double messages_per_sec
=
356 iteration
* double (ACE_ONE_SECOND_IN_USECS
) / real_time
;
358 ACE_DEBUG ((LM_DEBUG
,
359 ACE_TEXT ("(%t) messages = %d\n")
360 ACE_TEXT ("(%t) usec-per-message = %f\n")
361 ACE_TEXT ("(%t) messages-per-second = %0.00f\n"),
363 real_time
/ double (iteration
),
364 messages_per_sec
< 0 ? 0 : messages_per_sec
));
366 // Close the connection.
370 return (void *) result
;
376 if (this->oneway_
== 0)
377 return ACE_THR_FUNC (&Options::twoway_client_test
);
379 return ACE_THR_FUNC (&Options::oneway_client_test
);
385 // Raise the socket handle limit to the maximum.
386 ACE::set_handle_limit ();
388 #if defined (ACE_HAS_THREADS)
389 if (ACE_Thread_Manager::instance ()->spawn_n (
390 OPTIONS::instance ()->threads (),
391 OPTIONS::instance ()->thr_func ()) == -1)
392 ACE_ERROR_RETURN ((LM_ERROR
,
393 ACE_TEXT ("(%P|%t) %p\n"),
394 ACE_TEXT ("spawn_n")),
397 ACE_Thread_Manager::instance ()->wait ();
399 (void) *(OPTIONS::instance ()->thr_func
) ();
400 #endif /* ACE_HAS_THREADS */
405 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
407 ACE_SSL_Context
*context
= ACE_SSL_Context::instance ();
409 context
->certificate ("./dummy.pem", SSL_FILETYPE_PEM
);
410 context
->private_key ("./key.pem", SSL_FILETYPE_PEM
);
412 // Initialize the logger.
413 ACE_LOG_MSG
->open (argv
[0]);
415 if (OPTIONS::instance ()->parse_args (argc
, argv
) == -1)