Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / RTCORBA / Bug_3643_Regression / client.cpp
blobf7cea023958200ce7ae3aceb2beba444219e94b8
1 #include "ace/Get_Opt.h"
2 #include "testC.h"
3 #include "tao/RTCORBA/RTCORBA.h"
4 #include "tao/ORB_Core.h"
5 #include "ace/Task.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/OS_NS_time.h"
9 const ACE_TCHAR *ior = ACE_TEXT ("file://ior_1");
10 int iterations = 1000;
11 int shutdown_server = 0;
12 bool decreased = false;
14 const char *
15 sched_policy_name (int sched_policy)
17 const char *name = 0;
19 switch (sched_policy)
21 case ACE_SCHED_OTHER:
22 name = "SCHED_OTHER";
23 break;
24 case ACE_SCHED_RR:
25 name = "SCHED_RR";
26 break;
27 case ACE_SCHED_FIFO:
28 name = "SCHED_FIFO";
29 break;
32 return name;
35 int
36 parse_args (int argc, ACE_TCHAR *argv[])
38 ACE_Get_Opt get_opts (argc, argv, "xk:i:");
39 int c;
41 while ((c = get_opts ()) != -1)
42 switch (c)
44 case 'x':
45 shutdown_server = 1;
46 break;
48 case 'k':
49 ior = get_opts.opt_arg ();
50 break;
52 case 'i':
53 iterations = ACE_OS::atoi (get_opts.opt_arg ());
54 break;
56 case '?':
57 default:
58 ACE_ERROR_RETURN ((LM_ERROR,
59 "usage: %s "
60 "-k <ior> "
61 "-i <iterations> "
62 "-x [shutdown server] "
63 "\n",
64 argv [0]),
65 -1);
68 // Indicates successful parsing of the command line
69 return 0;
72 class Task : public ACE_Task_Base
74 public:
75 Task (ACE_Thread_Manager &thread_manager,
76 CORBA::ORB_ptr orb);
78 int svc ();
80 CORBA::ORB_var orb_;
83 Task::Task (ACE_Thread_Manager &thread_manager,
84 CORBA::ORB_ptr orb)
85 : ACE_Task_Base (&thread_manager),
86 orb_ (CORBA::ORB::_duplicate (orb))
90 int
91 Task::svc ()
93 try
95 CORBA::Object_var object =
96 this->orb_->string_to_object (ior);
98 test_var test =
99 test::_narrow (object.in ());
101 pid_t pid =
102 ACE_OS::getpid ();
104 CORBA::Long tc = 0;
106 struct timespec millis;
107 millis.tv_sec = 0L;
108 millis.tv_nsec = 50 * 1000000L; // 1e6 nanos per milli
110 ACE_hrtime_t start = ACE_OS::gethrtime();
112 for (int i = 0; i != iterations; ++i)
114 // ~20 req/sec means to sleep 50 millis between each
115 ACE_OS::nanosleep(&millis); // ignore signals
117 CORBA::Long mtc = 0;
118 CORBA::Long r =
119 test->method (pid,
121 mtc);
123 ACE_ASSERT (r == i);
124 // Assert disappears on with optimizations on.
125 ACE_UNUSED_ARG (r);
127 if (mtc > tc)
129 // Number of threads increased, so store this.
130 ACE_DEBUG ((LM_DEBUG, "Thread count increased to %d\n", mtc));
131 tc = mtc;
133 else if (mtc < tc)
135 // Number of threads decreased!
136 ACE_DEBUG ((LM_DEBUG, "Thread count decreased to %d\n", mtc));
137 decreased = true;
138 tc = mtc;
141 ACE_hrtime_t duration = ACE_OS::gethrtime() - start;
142 double tp = ((double) iterations) / (duration / 1000000);
143 tp *= 1000.0;
144 ACE_DEBUG ((LM_DEBUG, "Throughput: %d\n", tp));
146 ACE_OS::sleep (10);
148 CORBA::Long end = 0;
149 CORBA::Long re =
150 test->method (pid,
152 end);
154 ACE_ASSERT (re == 0);
155 // Assert disappears on with optimizations on.
156 ACE_UNUSED_ARG (re);
158 if (end != 0)
160 ACE_ERROR ((LM_ERROR, "Dynamic thread count should be 0, not %d\n", end));
163 if (shutdown_server)
165 test->shutdown ();
168 catch (const CORBA::Exception& ex)
170 ex._tao_print_exception ("Exception caught:");
171 return -1;
174 return 0;
178 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
182 CORBA::ORB_var orb =
183 CORBA::ORB_init (argc, argv);
185 int result =
186 parse_args (argc, argv);
187 if (result != 0)
188 return result;
190 // Thread Manager for managing task.
191 ACE_Thread_Manager thread_manager;
193 // Create task.
194 Task task (thread_manager,
195 orb.in ());
197 // Task activation flags.
198 long flags =
199 THR_NEW_LWP |
200 THR_JOINABLE |
201 orb->orb_core ()->orb_params ()->thread_creation_flags ();
203 // Activate task.
204 result = task.activate (flags);
206 if (result == -1)
208 if (errno == EPERM)
210 ACE_ERROR_RETURN ((LM_ERROR,
211 "Cannot create thread with scheduling policy %s\n"
212 "because the user does not have the appropriate privileges, terminating program....\n"
213 "Check svc.conf options and/or run as root\n",
214 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
217 else
218 // Unexpected error.
219 ACE_ASSERT (0);
222 // Wait for task to exit.
223 result =
224 thread_manager.wait ();
225 ACE_ASSERT (result != -1);
227 if (decreased == false)
229 ACE_ERROR_RETURN ((LM_ERROR, "ERROR: Number of threads didn't decrease\n"), -1);
232 catch (const CORBA::Exception& ex)
234 ex._tao_print_exception ("Exception caught");
235 return -1;
238 return 0;