Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / RTCORBA / Bug_3643_Regression / client.cpp
blob7e0342f40b4e8e12943097592fae28e20852c3f3
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:
76 Task (ACE_Thread_Manager &thread_manager,
77 CORBA::ORB_ptr orb);
79 int svc (void);
81 CORBA::ORB_var orb_;
85 Task::Task (ACE_Thread_Manager &thread_manager,
86 CORBA::ORB_ptr orb)
87 : ACE_Task_Base (&thread_manager),
88 orb_ (CORBA::ORB::_duplicate (orb))
92 int
93 Task::svc (void)
95 try
97 CORBA::Object_var object =
98 this->orb_->string_to_object (ior);
100 test_var test =
101 test::_narrow (object.in ());
103 pid_t pid =
104 ACE_OS::getpid ();
106 CORBA::Long tc = 0;
108 struct timespec millis;
109 millis.tv_sec = 0L;
110 millis.tv_nsec = 50 * 1000000L; // 1e6 nanos per milli
112 ACE_hrtime_t start = ACE_OS::gethrtime();
114 for (int i = 0; i != iterations; ++i)
116 // ~20 req/sec means to sleep 50 millis between each
117 ACE_OS::nanosleep(&millis); // ignore signals
119 CORBA::Long mtc = 0;
120 CORBA::Long r =
121 test->method (pid,
123 mtc);
125 ACE_ASSERT (r == i);
126 // Assert disappears on with optimizations on.
127 ACE_UNUSED_ARG (r);
129 if (mtc > tc)
131 // Number of threads increased, so store this.
132 ACE_DEBUG ((LM_DEBUG, "Thread count increased to %d\n", mtc));
133 tc = mtc;
135 else if (mtc < tc)
137 // Number of threads decreased!
138 ACE_DEBUG ((LM_DEBUG, "Thread count decreased to %d\n", mtc));
139 decreased = true;
140 tc = mtc;
143 ACE_hrtime_t duration = ACE_OS::gethrtime() - start;
144 double tp = ((double) iterations) / (duration / 1000000);
145 tp *= 1000.0;
146 ACE_DEBUG ((LM_DEBUG, "Throughput: %d\n", tp));
148 ACE_OS::sleep (10);
150 CORBA::Long end = 0;
151 CORBA::Long re =
152 test->method (pid,
154 end);
156 ACE_ASSERT (re == 0);
157 // Assert disappears on with optimizations on.
158 ACE_UNUSED_ARG (re);
160 if (end != 0)
162 ACE_ERROR ((LM_ERROR, "Dynamic thread count should be 0, not %d\n", end));
165 if (shutdown_server)
167 test->shutdown ();
170 catch (const CORBA::Exception& ex)
172 ex._tao_print_exception ("Exception caught:");
173 return -1;
176 return 0;
180 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
184 CORBA::ORB_var orb =
185 CORBA::ORB_init (argc, argv);
187 int result =
188 parse_args (argc, argv);
189 if (result != 0)
190 return result;
192 // Thread Manager for managing task.
193 ACE_Thread_Manager thread_manager;
195 // Create task.
196 Task task (thread_manager,
197 orb.in ());
199 // Task activation flags.
200 long flags =
201 THR_NEW_LWP |
202 THR_JOINABLE |
203 orb->orb_core ()->orb_params ()->thread_creation_flags ();
205 // Activate task.
206 result = task.activate (flags);
208 if (result == -1)
210 if (errno == EPERM)
212 ACE_ERROR_RETURN ((LM_ERROR,
213 "Cannot create thread with scheduling policy %s\n"
214 "because the user does not have the appropriate privileges, terminating program....\n"
215 "Check svc.conf options and/or run as root\n",
216 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
219 else
220 // Unexpected error.
221 ACE_ASSERT (0);
224 // Wait for task to exit.
225 result =
226 thread_manager.wait ();
227 ACE_ASSERT (result != -1);
229 if (decreased == false)
231 ACE_ERROR_RETURN ((LM_ERROR, "ERROR: Number of threads didn't decrease\n"), -1);
234 catch (const CORBA::Exception& ex)
236 ex._tao_print_exception ("Exception caught");
237 return -1;
240 return 0;