1 // ============================================================================
4 // This class implements a CORBA client for a redundant CosNaming
5 // Service using stubs generated by the TAO ORB IDL compiler.
8 // Rich Seibel <seibel_r@ociweb.com>
9 // ============================================================================
11 #include "test_objectS.h"
12 #include "orbsvcs/CosNamingC.h"
13 #include "orbsvcs/Naming/Naming_Server.h"
14 #include "tao/debug.h"
15 #include "ace/Get_Opt.h"
16 #include "ace/OS_NS_stdio.h"
17 #include "ace/High_Res_Timer.h"
19 #if defined (_MSC_VER)
20 # pragma warning (disable : 4250)
23 class My_Test_Object
:
24 public virtual POA_Test_Object
27 My_Test_Object (CORBA::Short id
= 0);
30 ~My_Test_Object (void);
33 // = Interface implementation accessor methods.
35 void id (CORBA::Short id
);
38 CORBA::Short
id (void);
45 My_Test_Object::My_Test_Object (CORBA::Short id
)
50 My_Test_Object::~My_Test_Object (void)
55 My_Test_Object::id (void)
61 My_Test_Object::id (CORBA::Short id
)
67 // This function runs the test.
70 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
75 ACE_TCHAR
*ns1ref
= 0;
76 ACE_TCHAR
*ns2ref
= 0;
78 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT ("b:d:o:p:q:"));
82 while ((c
= get_opts ()) != -1)
86 i
= ACE_OS::atoi(get_opts
.opt_arg ());
90 ACE_TEXT ("Invalid breath, must be 2 or more\n")));
96 i
= ACE_OS::atoi(get_opts
.opt_arg ());
100 ACE_TEXT ("Invalid depth, must be 2 or more\n")));
106 i
= ACE_OS::atoi(get_opts
.opt_arg ());
110 ACE_TEXT ("Invalid breath, must be 2 or more\n")));
116 ns1ref
= get_opts
.opt_arg ();
119 ns2ref
= get_opts
.opt_arg ();
122 ACE_ERROR_RETURN ((LM_ERROR
,
123 ACE_TEXT ("Argument %c \n usage: %s")
124 ACE_TEXT (" [-b <breath of context tree>]")
125 ACE_TEXT (" [-d <depth of context tree>]")
126 ACE_TEXT (" [-o <breath of object tree>]")
127 ACE_TEXT (" -p <ior of first name server>")
128 ACE_TEXT (" -q <ior of second name server>")
133 CosNaming::NamingContext_var root_context_1
;
134 CosNaming::NamingContext_var root_context_2
;
139 CORBA::ORB_var orb
= CORBA::ORB_init(argc
, argv
);
141 // ior's are specified for the name servers through a commandline
144 // Resolve the first name server
146 CORBA::Object_var ns1obj
= orb
->string_to_object (
147 ACE_TEXT_ALWAYS_CHAR (ns1ref
));
149 if (CORBA::is_nil (ns1obj
.in ()))
150 ACE_ERROR_RETURN ((LM_ERROR
,
151 ACE_TEXT ("invalid ior <%s>\n"),
154 root_context_1
= CosNaming::NamingContext::_narrow (ns1obj
.in ());
156 // Resolve the second name server
158 CORBA::Object_var ns2obj
= orb
->string_to_object (
159 ACE_TEXT_ALWAYS_CHAR (ns2ref
));
161 if (CORBA::is_nil (ns2obj
.in ()))
162 ACE_ERROR_RETURN ((LM_ERROR
,
163 ACE_TEXT ("invalid ior <%s>\n"),
166 root_context_2
= CosNaming::NamingContext::_narrow (ns2obj
.in ());
169 catch (const CORBA::Exception
& ex
)
171 ex
._tao_print_exception (ACE_TEXT ("Unable to resolve name servers"));
175 // Create a bunch of objects in one context
176 // Note: strings to the naming service must be char, not wchar
179 // Bind one context level under root.
180 CosNaming::Name level1
;
182 level1
[0].id
= CORBA::string_dup ("level1_context");
183 CosNaming::NamingContext_var level1_context
;
184 level1_context
= root_context_1
->bind_new_context (level1
);
186 for (i
=0; i
<o_breath
; i
++)
188 // Instantiate a dummy object and bind it under the new context.
189 My_Test_Object
*impl1
= new My_Test_Object (i
+1);
190 Test_Object_var obj1
= impl1
->_this ();
191 impl1
->_remove_ref ();
195 ACE_OS::sprintf(wide_name
, "obj_%d", i
);
196 level1
[1].id
= CORBA::string_dup (wide_name
);
197 root_context_1
->bind (level1
, obj1
.in ());
201 catch (const CORBA::Exception
& ex
)
203 ex
._tao_print_exception (ACE_TEXT ("Unable to create a lot of objects"));
207 // Create a deep context tree
210 CosNaming::NamingContext_var next_context
= root_context_1
;
211 for (i
=0; i
<c_depth
; i
++)
213 // Bind level1 context under root.
214 CosNaming::Name deep
;
217 ACE_OS::sprintf(deep_name
, "deep_%d", i
);
218 deep
[0].id
= CORBA::string_dup (deep_name
);
219 CosNaming::NamingContext_var deep_context
;
220 deep_context
= next_context
->bind_new_context (deep
);
221 next_context
= deep_context
;
224 catch (const CORBA::Exception
& ex
)
226 ex
._tao_print_exception (ACE_TEXT ("Unable to create deep context"));
230 // Create a wide context tree
233 for (i
=0; i
<c_breath
; i
++)
235 // Bind all level of context under root.
236 CosNaming::Name wide
;
239 ACE_OS::sprintf(wide_name
, "wide_%d", i
);
240 wide
[0].id
= CORBA::string_dup (wide_name
);
241 CosNaming::NamingContext_var wide_context
;
242 wide_context
= root_context_1
->bind_new_context (wide
);
245 catch (const CORBA::Exception
& ex
)
247 ex
._tao_print_exception (ACE_TEXT ("Unable to create wide context"));
251 // Delete three selected things, one from each tree
254 // Remove the second to last object from the Naming Context
255 CosNaming::Name wide1
;
257 wide1
[0].id
= CORBA::string_dup ("level1_context");
259 ACE_OS::sprintf(wide_name
, "obj_%d", o_breath
-2);
260 wide1
[1].id
= CORBA::string_dup (wide_name
);
261 root_context_1
->unbind (wide1
);
263 // Remove the second to last context from the wide root Naming Context
264 CosNaming::Name wide2
;
266 ACE_OS::sprintf(wide_name
, "wide_%d", c_breath
-2);
267 wide2
[0].id
= CORBA::string_dup (wide_name
);
268 CORBA::Object_var result_obj_ref
= root_context_1
->resolve (wide2
);
269 CosNaming::NamingContext_var result_object
=
270 CosNaming::NamingContext::_narrow (result_obj_ref
.in ());
271 if (CORBA::is_nil (result_object
.in ()))
272 ACE_ERROR_RETURN ((LM_ERROR
,
273 ACE_TEXT ("Problems with resolving wide context ")
274 ACE_TEXT ("- nil object ref.\n")),
276 result_object
->destroy();
277 root_context_1
->unbind (wide2
);
279 // Remove the last context from the deep Naming Context
280 CosNaming::Name deep
;
281 deep
.length (c_depth
);
283 for (i
=0; i
<c_depth
; i
++)
285 ACE_OS::sprintf(deep_name
, "deep_%d", i
);
286 deep
[i
].id
= CORBA::string_dup (deep_name
);
288 result_obj_ref
= root_context_1
->resolve (deep
);
290 CosNaming::NamingContext::_narrow (result_obj_ref
.in ());
291 if (CORBA::is_nil (result_object
.in ()))
292 ACE_ERROR_RETURN ((LM_ERROR
,
293 ACE_TEXT ("Problems with resolving deep context ")
294 ACE_TEXT ("- nil object ref.\n")),
296 result_object
->destroy();
297 root_context_1
->unbind (deep
);
300 catch (const CORBA::Exception
& ex
)
302 ex
._tao_print_exception (ACE_TEXT ("Unable to delete objects"));
306 // Now use the other name server to access 3 objects next to the
307 // deleted objects and the 3 deleted objects
310 // Access the last object from the Naming Context
311 CosNaming::Name wide
;
313 wide
[0].id
= CORBA::string_dup ("level1_context");
315 ACE_OS::sprintf(wide_name
, "obj_%d", o_breath
-1);
316 wide
[1].id
= CORBA::string_dup (wide_name
);
317 CORBA::Object_var result_obj_ref
= root_context_2
->resolve (wide
);
318 Test_Object_var result_object
= Test_Object::_narrow (result_obj_ref
.in ());
319 if (CORBA::is_nil (result_object
.in ()))
320 ACE_ERROR_RETURN ((LM_ERROR
,
321 ACE_TEXT ("Problems with resolving object from ")
322 ACE_TEXT ("redundant server - nil object ref.\n")),
325 catch (const CORBA::Exception
& ex
)
327 ex
._tao_print_exception (
329 "Unable to resolve object from redundant server"));
335 // Access the deleted second to last object from the Naming Context
336 CosNaming::Name wide
;
338 wide
[0].id
= CORBA::string_dup ("level1_context");
340 ACE_OS::sprintf(wide_name
, "obj_%d", o_breath
-2);
341 wide
[1].id
= CORBA::string_dup (wide_name
);
342 CORBA::Object_var result_obj_ref
= root_context_2
->resolve (wide
);
343 ACE_ERROR_RETURN ((LM_ERROR
,
344 ACE_TEXT ("Problems with resolving object from ")
345 ACE_TEXT ("redundant server - deleted object found.\n")),
348 catch (const CORBA::Exception
&)
350 //expect exception since the context was deleted
355 // Access the last context from the wide Naming Context
356 CosNaming::Name wide
;
359 ACE_OS::sprintf(wide_name
, "wide_%d", c_breath
-1);
360 wide
[0].id
= CORBA::string_dup (wide_name
);
361 CORBA::Object_var result_obj_ref
= root_context_2
->resolve (wide
);
362 CosNaming::NamingContext_var result_object
=
363 CosNaming::NamingContext::_narrow (result_obj_ref
.in ());
364 if (CORBA::is_nil (result_object
.in ()))
365 ACE_ERROR_RETURN ((LM_ERROR
,
366 ACE_TEXT ("Problems with resolving wide context from ")
367 ACE_TEXT ("redundant server - nil object ref.\n")),
370 catch (const CORBA::Exception
& ex
)
372 ex
._tao_print_exception (
374 "Unable to resolve wide context from redundant server"));
380 // Access the deleted second to last object from the Naming Context
381 CosNaming::Name wide
;
384 ACE_OS::sprintf(wide_name
, "wide_%d", c_breath
-2);
385 wide
[0].id
= CORBA::string_dup (wide_name
);
386 CORBA::Object_var result_obj_ref
= root_context_2
->resolve (wide
);
387 ACE_ERROR_RETURN ((LM_ERROR
,
388 ACE_TEXT ("Problems with resolving wide context from ")
389 ACE_TEXT ("redundant server - deleted object found.\n")),
392 catch (const CORBA::Exception
&)
394 //expect exception since the context was deleted
399 // Access the deleted last context from the deep Naming Context
400 CosNaming::Name deep
;
401 deep
.length (c_depth
);
403 for (i
=0; i
<c_depth
; i
++)
405 ACE_OS::sprintf(deep_name
, "deep_%d", i
);
406 deep
[i
].id
= CORBA::string_dup (deep_name
);
408 CORBA::Object_var result_obj_ref
= root_context_1
->resolve (deep
);
409 ACE_ERROR_RETURN ((LM_ERROR
,
410 ACE_TEXT ("Problems with resolving deep context from ")
411 ACE_TEXT ("redundant server - deleted object found.\n")),
414 catch (const CORBA::Exception
&)
416 //expect exception since the context was deleted
421 // Access the second to last object from the Naming Context
422 CosNaming::Name deep
;
423 deep
.length (c_depth
-1);
425 for (i
=0; i
<c_depth
-1; i
++)
427 ACE_OS::sprintf(deep_name
, "deep_%d", i
);
428 deep
[i
].id
= CORBA::string_dup (deep_name
);
430 CORBA::Object_var result_obj_ref
= root_context_1
->resolve (deep
);
431 CosNaming::NamingContext_var result_object
=
432 CosNaming::NamingContext::_narrow (result_obj_ref
.in ());
433 if (CORBA::is_nil (result_object
.in ()))
434 ACE_ERROR_RETURN ((LM_ERROR
,
435 ACE_TEXT ("Problems with resolving deep context from ")
436 ACE_TEXT ("redundant server - nil object ref.\n")),
441 catch (const CORBA::Exception
& ex
)
443 ex
._tao_print_exception (
445 "Unable to resolve deep context from redundant server"));
449 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Redundancy test OK\n")));
451 // Test performance of binding a bunch of objects in one context
454 // Bind one context level under root.
455 CosNaming::Name level1
;
457 level1
[0].id
= CORBA::string_dup ("perf_context");
458 CosNaming::NamingContext_var perf_context
;
459 perf_context
= root_context_1
->bind_new_context (level1
);
461 // Instantiate a dummy object and bind it under the new context.
462 My_Test_Object
*impl1
= new My_Test_Object (i
+1);
463 Test_Object_var obj1
= impl1
->_this ();
464 impl1
->_remove_ref ();
467 ACE_High_Res_Timer::global_scale_factor_type gsf
=
468 ACE_High_Res_Timer::global_scale_factor ();
470 ACE_hrtime_t start
= ACE_OS::gethrtime ();
472 // Test how long it takes to bind
473 for (i
=0; i
<test_runs
; i
++)
477 ACE_OS::sprintf(wide_name
, "obj_%d", i
);
478 level1
[0].id
= CORBA::string_dup (wide_name
);
479 perf_context
->bind (level1
, obj1
.in ());
482 ACE_hrtime_t elapsed_time
= ACE_OS::gethrtime () - start
;
483 // convert to microseconds
484 ACE_UINT32 usecs
= ACE_UINT32(elapsed_time
/ gsf
);
485 double secs
= usecs
/ 1000000.0;
487 ACE_DEBUG ((LM_DEBUG
,
488 "Bound %i objects in %.2f secs\n",
491 // Test how long it takes to resolve
492 start
= ACE_OS::gethrtime ();
493 for (i
=0; i
<test_runs
; i
++)
497 ACE_OS::sprintf(wide_name
, "obj_%d", i
);
498 level1
[0].id
= CORBA::string_dup (wide_name
);
499 CORBA::Object_var result_obj_ref
= perf_context
->resolve (level1
);
502 elapsed_time
= ACE_OS::gethrtime () - start
;
503 // convert to microseconds
504 usecs
= ACE_UINT32(elapsed_time
/ gsf
);
505 secs
= ((ACE_INT32
) usecs
) / 1000000.0;
507 ACE_DEBUG ((LM_DEBUG
,
508 "Resolved %i objects in %.2f secs\n",
511 // Test how long it takes to unbind
512 start
= ACE_OS::gethrtime ();
513 for (i
=0; i
<test_runs
; i
++)
517 ACE_OS::sprintf(wide_name
, "obj_%d", i
);
518 level1
[0].id
= CORBA::string_dup (wide_name
);
519 perf_context
->unbind (level1
);
522 elapsed_time
= ACE_OS::gethrtime () - start
;
523 // convert to microseconds
524 usecs
= ACE_UINT32(elapsed_time
/ gsf
);
525 secs
= ((ACE_INT32
) usecs
) / 1000000.0;
527 ACE_DEBUG ((LM_DEBUG
,
528 "Unbound %i objects in %.2f secs\n",
531 catch (const CORBA::Exception
& ex
)
533 ex
._tao_print_exception (ACE_TEXT ("ERROR: Exception during performance test.\n"));
537 // All tests have passed up to this point