=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / performance-tests / CSD_Strategy / TestApps / ServerApp.cpp
blob489fb1d558a6cb7e8d44ada8edaca630c69277c9
1 #include "ServerApp.h"
2 #include "TestInf/AppHelper.h"
3 #include "TestInf/OrbRunner.h"
4 #include "TestInf/AppShutdown.h"
5 #include "TestInf/TestAppExceptionC.h"
6 #include "TestServant/Foo_ClientEngine.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/Time_Value.h"
9 #include "ace/High_Res_Timer.h"
10 // To force static load the service.
11 #include "tao/PI/PI.h"
12 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
15 ServerApp::ServerApp()
16 : TestAppBase("CSD_PT_ServerApp"),
17 ior_filename_prefix_(ACE_TEXT("foo")),
18 num_servants_(1),
19 num_csd_threads_(1),
20 num_orb_threads_(1),
21 num_remote_clients_(1),
22 num_collocated_clients_(0),
23 num_loops_(1),
24 use_csd_(1),
25 scenario_id_("UnknownScenarioId"),
26 trial_id_(0)
31 ServerApp::~ServerApp()
36 int
37 ServerApp::run_i(int argc, ACE_TCHAR* argv[])
39 int result = this->init(argc, argv);
40 if (result != 0)
42 return result;
45 this->poa_setup();
46 this->csd_setup();
47 this->servant_setup();
48 this->collocated_setup();
49 this->poa_activate();
51 ACE_High_Res_Timer timer;
52 timer.start();
54 this->run_collocated_clients();
55 this->run_orb_event_loop();
58 timer.stop();
60 ACE_Time_Value tv;
61 timer.elapsed_time(tv);
63 // Calling wait on ACE_Thread_Manager singleton to avoid the problem
64 // that the main thread might exit before all CSD Threads exit.
66 // Wait for all CSD task threads exit.
67 ACE_Thread_Manager::instance ()->wait ();
69 unsigned num_operations = this->stats_.total();
71 double ops_per_msec = (1.0 * num_operations) / tv.msec();
73 ACE_DEBUG((LM_DEBUG, "%s,%d,%d,%d,%.2f\n",
74 this->scenario_id_.c_str(),
75 this->trial_id_,
76 num_operations,
77 tv.msec(),
78 ops_per_msec));
80 this->cleanup();
81 return this->check_results () ? 0 : -1;
85 int
86 ServerApp::init(int argc, ACE_TCHAR* argv[])
88 this->orb_ = CORBA::ORB_init(argc, argv);
90 // Parse the command-line args for this application.
91 // * Raises -1 if problems are encountered.
92 // * Returns 1 if the usage statement was explicitly requested.
93 // * Returns 0 otherwise.
94 int result = this->parse_args(argc, argv);
96 if (result != 0)
98 return result;
101 TheAppShutdown->init(this->orb_.in(),
102 this->num_remote_clients_ +
103 this->num_collocated_clients_);
105 return 0;
109 void
110 ServerApp::poa_setup()
112 this->poa_ = this->create_poa(this->orb_.in(),
113 "ChildPoa");
117 void
118 ServerApp::csd_setup()
120 this->tp_strategy_ = new TAO::CSD::TP_Strategy(this->num_csd_threads_);
122 if (this->use_csd_ > 0)
124 if (!this->tp_strategy_->apply_to(this->poa_.in()))
126 ACE_ERROR((LM_ERROR,
127 "Failed to apply CSD strategy to poa.\n"));
128 throw TestAppException();
134 void
135 ServerApp::servant_setup()
137 this->servants_.create_and_activate(this->num_servants_,
138 this->orb_.in (),
139 this->poa_.in (),
140 this->ior_filename_prefix_.c_str());
144 void
145 ServerApp::collocated_setup()
147 int client_id = this->num_remote_clients_;
149 for (unsigned i = 0; i < this->num_collocated_clients_; i++)
151 ++client_id;
153 // Dole out the servant object references in a round-robin fashion.
154 unsigned servant_index = i % this->num_servants_;
156 ServantListType::T_stub_var obj = this->servants_.objref(servant_index);
158 ClientEngine_Handle engine = new Foo_ClientEngine(obj.in(), client_id);
160 this->collocated_client_task_.add_engine(engine.in());
163 this->collocated_client_task_.num_loops(this->num_loops_);
167 void
168 ServerApp::poa_activate()
170 PortableServer::POAManager_var poa_manager
171 = this->poa_->the_POAManager();
173 poa_manager->activate();
177 void
178 ServerApp::run_collocated_clients()
180 if (this->num_collocated_clients_ > 0)
182 if (this->collocated_client_task_.open() == -1)
184 throw TestAppException ();
190 void
191 ServerApp::run_orb_event_loop()
193 OrbRunner orb_runner(this->orb_.in(), this->num_orb_threads_);
194 orb_runner.run();
195 TheAppShutdown->wait ();
199 void
200 ServerApp::cleanup()
206 ServerApp::parse_args(int argc, ACE_TCHAR* argv[])
208 this->exe_name_ = argv[0];
210 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("p:s:n:t:r:c:l:u:x:z:"));
212 int c;
214 while ((c = get_opts()) != -1)
216 int result = 0;
217 switch (c)
219 case 'p':
220 this->ior_filename_prefix_ = get_opts.opt_arg();
221 break;
223 case 's':
224 result = this->set_arg(this->num_servants_,
225 get_opts.opt_arg(),
227 "num_servants",
229 break;
231 case 'n':
232 result = this->set_arg(this->num_csd_threads_,
233 get_opts.opt_arg(),
235 "num_servants",
237 break;
239 case 't':
240 result = this->set_arg(this->num_orb_threads_,
241 get_opts.opt_arg(),
243 "num_orb_threads",
245 break;
247 case 'r':
248 result = this->set_arg(this->num_remote_clients_,
249 get_opts.opt_arg(),
251 "num_remote_clients");
252 break;
254 case 'c':
255 result = this->set_arg(this->num_collocated_clients_,
256 get_opts.opt_arg(),
258 "num_collocated_clients");
259 break;
261 case 'l':
262 result = this->set_arg(this->num_loops_,
263 get_opts.opt_arg(),
265 "num_loops",
267 break;
269 case 'u':
270 result = this->set_arg(this->use_csd_,
271 get_opts.opt_arg(),
273 "use_csd_flag");
274 break;
276 case 'x':
277 this->scenario_id_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg());
278 break;
280 case 'z':
281 result = this->set_arg(this->trial_id_,
282 get_opts.opt_arg(),
284 "trial_id_number",
286 break;
288 case '?':
289 this->usage_statement();
290 return 1;
292 default:
293 this->usage_statement();
294 return -1;
297 if (result != 0)
299 return result;
303 return this->arg_dependency_checks();
306 void
307 ServerApp::usage_statement()
309 ACE_ERROR((LM_ERROR,
310 "Usage: %s [options]\n\n"
311 "OPTIONS:\n\n"
312 "\t[-p <ior_filename_prefix>]\n"
313 "\t[-s <num_servants>]\n"
314 "\t[-n <num_csd_threads>]\n"
315 "\t[-t <num_orb_threads>]\n"
316 "\t[-r <num_remote_clients>]\n"
317 "\t[-c <num_collocated_clients>]\n"
318 "\t[-l <num_loops>]\n"
319 "\t[-u <use_csd_flag>]\n"
320 "\t[-x <scenario_id_string>]\n"
321 "\t[-z <trial_id_number>]\n"
322 "\t[-?]\n\n",
323 this->exe_name_.c_str()));
328 ServerApp::arg_dependency_checks()
330 return (this->num_remote_clients_
331 + this->num_collocated_clients_) > 0 ? 0 : -1;
336 ServerApp::set_arg(unsigned& value,
337 const ACE_TCHAR* arg,
338 char opt,
339 const char* name,
340 int min)
342 int tmp = ACE_OS::atoi(arg);
344 if (tmp < min)
346 ACE_ERROR((LM_ERROR,
347 "Error: -%c <%s> must be integer type with a value of, "
348 "at least, %d.\n", opt, name, min));
349 this->usage_statement();
350 return -1;
353 value = tmp;
354 return 0;
358 PortableServer::POA_ptr
359 ServerApp::create_poa(CORBA::ORB_ptr orb,
360 const char* poa_name)
362 // Get the Root POA.
363 PortableServer::POA_var root_poa
364 = RefHelper<PortableServer::POA>::resolve_initial_ref(orb,
365 "RootPOA");
367 // Get the POAManager from the Root POA.
368 PortableServer::POAManager_var poa_manager
369 = root_poa->the_POAManager();
371 // Create the child POA Policies.
372 CORBA::PolicyList policies(0);
373 policies.length(0);
375 // Create the child POA
376 PortableServer::POA_var poa = AppHelper::create_poa(poa_name,
377 root_poa.in(),
378 poa_manager.in(),
379 policies);
381 // Give away the child POA_ptr from the POA_var variable.
382 return poa._retn();
386 bool
387 ServerApp::check_results()
389 this->stats_.init(this->num_remote_clients_ +
390 this->num_collocated_clients_,
391 this->num_loops_);
393 Foo_ClientEngine::expected_results(this->stats_);
395 for (unsigned i = 0; i < this->num_servants_; i++)
397 this->servants_.servant(i)->gather_stats(this->stats_);
400 this->stats_.actual_vs_expected();
402 return true;