Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / AMH / Sink_Server / Client_Task.cpp
bloba04b0456469cc17b2c08464d59d4107e00a844df
1 #include "Client_Task.h"
2 #include "tao/Strategies/advanced_resource.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/Sched_Params.h"
6 #include "ace/Stats.h"
7 #include "ace/Throughput_Stats.h"
9 Client_Task::Client_Task (int &argc, ACE_TCHAR **argv)
10 : argc_ (argc)
11 , argv_ (argv)
12 , ior_ (ACE_TEXT("file://test.ior"))
13 , iterations_ (1000)
17 int
18 Client_Task::parse_args (void)
20 ACE_Get_Opt get_opts (this->argc_, this->argv_, "k:n:O:");
21 int c;
23 while ((c = get_opts ()) != -1)
25 switch (c)
27 case 'k':
29 this->ior_ = get_opts.opt_arg ();
30 break;
32 case 'n':
34 int iterations = ACE_OS::atoi (get_opts.opt_arg ());
35 if (iterations >= 0 )
36 this->iterations_ = iterations;
37 break;
39 case '?':
40 ACE_ERROR_RETURN ((LM_ERROR,
41 "usage: %s "
42 "-k <file that contains IOR> "
43 "-n <iterations> "
44 "\n",
45 this->argv_ [0]),
46 -1);
47 default:
48 break;
51 // Indicates successful parsing of the command line
52 return 1;
55 void
56 Client_Task::try_RT_scheduling (void)
58 int priority =
59 (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO)
60 + ACE_Sched_Params::priority_max (ACE_SCHED_FIFO)) / 2;
61 priority = ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
62 priority);
64 // Enable FIFO scheduling, e.g., RT scheduling class on Solaris.
65 if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO,
66 priority,
67 ACE_SCOPE_PROCESS)) != 0)
69 if (errno == EPERM)
71 ACE_DEBUG ((LM_DEBUG,
72 "client (%P|%t): user is not superuser, "
73 "test runs in time-shared class\n"));
75 else
76 ACE_ERROR ((LM_ERROR,
77 "client (%P|%t): sched_params failed\n"));
81 int
82 Client_Task::narrow_servant (CORBA::ORB_ptr orb)
84 try
86 CORBA::Object_var object =
87 orb->string_to_object (this->ior_);
89 this->roundtrip_ =
90 Test::Roundtrip::_narrow (object.in ());
92 if (CORBA::is_nil (this->roundtrip_.in ()))
94 ACE_ERROR_RETURN ((LM_ERROR,
95 "Nil Test::Roundtrip reference <%s>\n",
96 this->ior_),
97 0);
100 catch (const CORBA::Exception& ex)
102 ex._tao_print_exception ("Exception caught trying to narrow servant\n");
103 return 0;
105 return 1;
109 Client_Task::run_test (void)
111 ACE_hrtime_t test_start = 0;
112 ACE_hrtime_t test_end = 0;
116 test_start = ACE_OS::gethrtime ();
118 this->roundtrip_->start_test ();
120 this->svc ();
122 this->roundtrip_->end_test ();
124 test_end = ACE_OS::gethrtime ();
126 catch (const CORBA::Exception&)
128 return 0;
132 // High resolution timer calibration
133 ACE_High_Res_Timer::global_scale_factor_type gsf =
134 ACE_High_Res_Timer::global_scale_factor ();
136 ACE_Basic_Stats totals;
138 this->accumulate_and_dump (totals, ACE_TEXT("Task"), gsf);
140 totals.dump_results (ACE_TEXT("Total"), gsf);
142 ACE_Throughput_Stats::dump_throughput (ACE_TEXT("Total"), gsf,
143 test_end - test_start,
144 totals.samples_count ());
146 return 1;
150 Client_Task::svc (void)
154 for (int i = 0; i != this->iterations_; ++i)
156 CORBA::ULongLong start = ACE_OS::gethrtime ();
158 (void) this->roundtrip_->test_method (start);
160 ACE_hrtime_t now = ACE_OS::gethrtime ();
161 this->latency_.sample (now - start);
164 catch (const CORBA::Exception&)
166 return 0;
168 return 1;
171 void
172 Client_Task::accumulate_and_dump (ACE_Basic_Stats &totals,
173 const ACE_TCHAR *msg,
174 ACE_High_Res_Timer::global_scale_factor_type gsf)
176 totals.accumulate (this->latency_);
177 this->latency_.dump_results (msg, gsf);