Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / performance-tests / POA / Object_Creation_And_Registration / registration.cpp
blob135955a31381137b1835b00bf7083ee35ace9545
2 //=============================================================================
3 /**
4 * @file registration.cpp
6 * This test is used to measure the time it takes to register and
7 * activate an object in the POA.
9 * @author Irfan Pyarali
11 //=============================================================================
14 #include "testS.h"
15 #include "tao/Server_Strategy_Factory.h"
16 #include "tao/ORB_Core.h"
17 #include "ace/Profile_Timer.h"
18 #include "ace/Get_Opt.h"
19 #include "ace/Truncate.h"
22 // The following macros help take a very precise look into the
23 // different object creations and registration calls through
24 // Quantify. You'll have to define USING_QUANTIFY and define any of
25 // the other specific defines that you are interested in to be 1.
27 // Compilation: Add the directory where pure.h is (usually C:\Program
28 // Files\Rational\Quantify) to the list of include directories. In
29 // addition, add pure_api.c to the source file compilation list.
32 //#define USING_QUANTIFY
33 #define QUANTIFY_DEACTIVATE_OBJECT 0
34 #define QUANTIFY_ACTIVATE_OBJECT_WITH_ID 0
35 #define QUANTIFY_CREATE_REFERENCE_WITH_ID 0
36 #define QUANTIFY_SERVANT_TO_ID 0
37 #define QUANTIFY_UNDERBAR_THIS 0
39 #if defined (USING_QUANTIFY)
40 #if defined (ACE_WIN32)
41 #include "pure.h"
43 #else
45 #include "quantify.h"
46 inline int QuantifyClearData ()
48 return quantify_clear_data ();
51 inline int QuantifyStartRecordingData ()
53 return quantify_start_recording_data ();
56 inline int QuantifyStopRecordingData ()
58 return quantify_stop_recording_data ();
61 #endif /* ACE_WIN32 */
62 #endif /* USING_QUANTIFY */
64 /**
65 * @class test_i
67 * @brief Oversimplified servant class
69 class test_i : public POA_test
73 // Program statics
74 static int measure_reverse_map_effectiveness = 0;
75 static u_long iterations = 1000;
77 static int
78 parse_args (int argc, ACE_TCHAR **argv)
80 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("i:r"));
81 int c;
83 while ((c = get_opts ()) != -1)
84 switch (c)
86 case 'i':
87 iterations = static_cast<u_long> (ACE_OS::atoi (get_opts.opt_arg ()));
88 break;
90 case 'r':
91 measure_reverse_map_effectiveness = 1;
92 break;
94 case '?':
95 default:
96 ACE_ERROR_RETURN ((LM_ERROR,
97 "usage: %s "
98 "[-r [measure reverse map effectiveness]] "
99 "[-i iterations] "
100 "\n",
101 argv [0]),
102 -1);
105 // Indicates successful parsing of command line.
106 return 0;
110 // Code for printing stats
112 void
113 print_stats (ACE_Profile_Timer::ACE_Elapsed_Time &elapsed_time)
115 if (iterations > 0)
117 elapsed_time.real_time *= ACE_ONE_SECOND_IN_MSECS;
118 elapsed_time.user_time *= ACE_ONE_SECOND_IN_MSECS;
119 elapsed_time.system_time *= ACE_ONE_SECOND_IN_MSECS;
121 elapsed_time.real_time /= iterations;
122 elapsed_time.user_time /= iterations;
123 elapsed_time.system_time /= iterations;
125 double tmp = 1000 / elapsed_time.real_time;
127 ACE_DEBUG ((LM_DEBUG,
128 "\titerations\t = %d,\n"
129 "\treal_time\t = %0.06f ms,\n"
130 "\tuser_time\t = %0.06f ms,\n"
131 "\tsystem_time\t = %0.06f ms,\n"
132 "\t%0.00f calls/second\n",
133 iterations,
134 elapsed_time.real_time < 0.0 ? 0.0 : elapsed_time.real_time,
135 elapsed_time.user_time < 0.0 ? 0.0 : elapsed_time.user_time,
136 elapsed_time.system_time < 0.0 ? 0.0 : elapsed_time.system_time,
137 tmp < 0.0 ? 0.0 : tmp));
139 else
140 ACE_ERROR ((LM_ERROR,
141 "\tNo time stats printed. Zero iterations or error occurred.\n"));
146 // Measures how well the reverse hash map works.
148 void
149 reverse_map_effectiveness (test_i *servants)
151 // Size of the active object map
152 const TAO_Server_Strategy_Factory::Active_Object_Map_Creation_Parameters &parameters =
153 TAO_ORB_Core_instance ()->server_factory ()->active_object_map_creation_parameters ();
155 u_long active_object_map_size = parameters.active_object_map_size_;
157 // Hash counters
158 u_long *hash_counter = new u_long[active_object_map_size];
160 // Index counter
161 u_long i = 0;
163 // Initialize the hash counters
164 for (i = 0; i < active_object_map_size; i++)
166 hash_counter[i] = 0;
169 // Calculate the effectiveness of the hash.
170 for (i = 0; i < iterations; i++)
172 u_long hash_index = ACE_Utils::truncate_cast<u_long> ((intptr_t)&servants[i]) % active_object_map_size;
173 hash_counter[hash_index]++;
176 for (i = 0; i < active_object_map_size; i++)
178 if (((i) % 10) == 0)
180 ACE_DEBUG ((LM_DEBUG, "\n"));
182 ACE_DEBUG ((LM_DEBUG, "%d = %d, ", i, hash_counter[i]));
184 ACE_DEBUG ((LM_DEBUG, "\n\n"));
186 delete[] hash_counter;
189 class stats
191 public:
192 stats (int quantify,
193 const char *test_name)
194 : quantify_ (quantify),
195 test_name_ (test_name)
197 this->timer_.start ();
199 if (this->quantify_)
201 #if defined (USING_QUANTIFY)
202 // Reset Quantify data recording; whatever happened before this
203 // point is not relevant anymore.
204 QuantifyClearData ();
205 QuantifyStartRecordingData ();
206 #endif /* USING_QUANTIFY */
210 ~stats ()
212 if (this->quantify_)
214 #if defined (USING_QUANTIFY)
215 // Stop recording data here; whatever happens after this is
216 // not relevant.
217 QuantifyStopRecordingData ();
218 #endif /* USING_QUANTIFY */
221 // stop the timer.
222 this->timer_.stop ();
224 ACE_Profile_Timer::ACE_Elapsed_Time elapsed_time;
225 this->timer_.elapsed_time (elapsed_time);
227 // compute average time.
228 ACE_DEBUG ((LM_DEBUG, "\n%C stats...\n",
229 this->test_name_));
230 print_stats (elapsed_time);
233 private:
234 // Profile timer
235 ACE_Profile_Timer timer_;
236 int quantify_;
237 const char *test_name_;
240 void
241 child_poa_testing (PortableServer::POA_ptr root_poa)
243 // Policies for the child POA.
244 CORBA::PolicyList policies (1);
245 policies.length (1);
247 // Id Assignment Policy
248 policies[0] =
249 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
251 // Create the child POA under the RootPOA.
252 PortableServer::POA_var child_poa =
253 root_poa->create_POA ("child POA",
254 PortableServer::POAManager::_nil (),
255 policies);
257 // Create an array of servants
258 test_i *servants =
259 new test_i[iterations];
261 // Create an array of objects
262 CORBA::Object_var *objects =
263 new CORBA::Object_var[iterations];
265 // Create an array of object ids
266 PortableServer::ObjectId_var *object_ids =
267 new PortableServer::ObjectId_var[iterations];
269 // Buffer for the id string.
270 char id_buffer[128];
272 // Index counter
273 u_long i = 0;
276 // Record and quantify stats.
277 stats s (QUANTIFY_CREATE_REFERENCE_WITH_ID,
278 "create_reference_with_id");
279 ACE_UNUSED_ARG (s);
281 for (i = 0; i < iterations; i++)
283 ACE_OS::sprintf (id_buffer,
284 "%ld",
287 object_ids[i] =
288 PortableServer::string_to_ObjectId (id_buffer);
290 objects[i] =
291 child_poa->create_reference_with_id (object_ids[i].in (),
292 "IDL:test:1.0");
297 // Record and quantify stats.
298 stats s (QUANTIFY_ACTIVATE_OBJECT_WITH_ID,
299 "activate_object_with_id");
300 ACE_UNUSED_ARG (s);
302 for (i = 0; i < iterations; i++)
304 child_poa->activate_object_with_id (object_ids[i].in (),
305 &servants[i]);
310 // Record and quantify stats.
311 stats s (QUANTIFY_DEACTIVATE_OBJECT,
312 "deactivate_object");
313 ACE_UNUSED_ARG (s);
315 for (i = 0; i < iterations; i++)
317 child_poa->deactivate_object (object_ids[i].in ());
321 // Cleanup
322 delete[] object_ids;
323 delete[] objects;
324 delete[] servants;
328 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
332 // Initialize the ORB first.
333 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
335 int result = parse_args (argc, argv);
336 if (result != 0)
337 return result;
339 // Obtain the RootPOA.
340 CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA");
342 // Get the POA_var object from Object_var.
343 PortableServer::POA_var root_poa =
344 PortableServer::POA::_narrow (obj.in ());
346 // Create an array of servants
347 test_i *servants =
348 new test_i[iterations];
350 // Create an array of objects
351 CORBA::Object_var *objects =
352 new CORBA::Object_var[iterations];
354 // Create an array of object ids
355 PortableServer::ObjectId_var *object_ids =
356 new PortableServer::ObjectId_var[iterations];
358 if (measure_reverse_map_effectiveness)
360 reverse_map_effectiveness (servants);
363 // Index counter
364 u_long i = 0;
367 stats s (QUANTIFY_UNDERBAR_THIS,
368 "_this");
369 ACE_UNUSED_ARG (s);
371 for (i = 0; i < iterations; i++)
373 PortableServer::ObjectId_var id =
374 root_poa->activate_object (&servants[i]);
375 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
376 objects[i] = test::_narrow (object.in ());
381 stats s (QUANTIFY_SERVANT_TO_ID,
382 "servant_to_id");
383 ACE_UNUSED_ARG (s);
385 for (i = 0; i < iterations; i++)
387 object_ids[i] = root_poa->servant_to_id (&servants[i]);
391 // Create the child POA.
392 child_poa_testing (root_poa.in ());
394 // Destroy RootPOA.
395 root_poa->destroy (true, true);
397 // Cleanup
398 delete[] object_ids;
399 delete[] objects;
400 delete[] servants;
402 catch (const CORBA::Exception& ex)
404 ex._tao_print_exception ("Exception caught");
405 return -1;
408 return 0;