Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Smart_Proxies / Benchmark / client.cpp
blobadf1da5926bfa86162e52239471f987b9c8fa877
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * This is the client program that tests TAO's Smart Proxy extension.
8 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 */
10 //=============================================================================
13 #include "testC.h"
14 #include "Smart_Proxy_Impl.h"
15 #include "tao/debug.h"
16 #include "ace/High_Res_Timer.h"
17 #include "ace/Sched_Params.h"
18 #include "ace/Stats.h"
19 #include "ace/Throughput_Stats.h"
20 #include "ace/Get_Opt.h"
21 #include "ace/OS_NS_string.h"
22 #include "ace/OS_NS_errno.h"
24 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
25 int niterations = 5;
26 int register_smart_proxy = 1;
28 class Marker
30 public:
31 void accumulate_into (ACE_Throughput_Stats &throughput) const
33 // Accumulate the throughput statistics into <throughput>
34 throughput.accumulate (this->throughput_);
36 void dump_stats (const ACE_TCHAR* msg,
37 ACE_High_Res_Timer::global_scale_factor_type gsf)
39 // Print stats
40 this->throughput_.dump_results (msg, gsf);
42 void sample (ACE_hrtime_t throughput_diff,
43 ACE_hrtime_t latency_diff)
45 // get the sample.
46 this->throughput_.sample (throughput_diff,
47 latency_diff);
49 private:
50 /// Keep throughput statistics on a per-thread basis
51 ACE_Throughput_Stats throughput_;
55 int
56 parse_args (int argc, ACE_TCHAR *argv[])
58 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("i:n:r:"));
59 int c;
61 while ((c = get_opts ()) != -1)
62 switch (c)
64 case 'i':
65 ior = get_opts.opt_arg ();
66 break;
67 case 'n':
68 niterations = ACE_OS::atoi (get_opts.opt_arg ());
69 break;
70 case 'r':
71 register_smart_proxy = ACE_OS::atoi (get_opts.opt_arg ());
72 break;
73 case '?':
74 default:
75 ACE_ERROR_RETURN ((LM_ERROR,
76 "usage: %s "
77 "-i -n"
78 "\n",
79 argv [0]),
80 -1);
82 return 0;
86 int
87 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
89 int priority =
90 (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO)
91 + ACE_Sched_Params::priority_max (ACE_SCHED_FIFO)) / 2;
92 // Enable FIFO scheduling
94 if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO,
95 priority,
96 ACE_SCOPE_PROCESS)) != 0)
98 if (ACE_OS::last_error () == EPERM)
100 ACE_DEBUG ((LM_DEBUG,
101 "client (%P|%t): user is not superuser, "
102 "test runs in time-shared class\n"));
104 else
105 ACE_ERROR ((LM_ERROR,
106 "client (%P|%t): sched_params failed\n"));
111 CORBA::ORB_var orb =
112 CORBA::ORB_init (argc,
113 argv);
115 if (parse_args (argc, argv) != 0)
116 return 1;
118 CORBA::Object_var object =
119 orb->string_to_object (ior);
120 if (register_smart_proxy == 1)
122 // To use the smart proxy it is necessary to allocate the
123 // user-defined smart factory on the heap as the smart proxy
124 // generated classes take care of destroying the object. This
125 // way it a win situation for the application developer who
126 // doesnt have to make sure to destroy it and also for the smart
127 // proxy designer who now can manage the lifetime of the object
128 // much surely.
130 Smart_Test_Factory *test_factory = 0;
131 ACE_NEW_RETURN (test_factory,
132 Smart_Test_Factory,
133 -1);
135 ACE_UNUSED_ARG (test_factory);
138 Test_var server =
139 Test::_narrow (object.in ());
141 if (CORBA::is_nil (server.in ()))
142 ACE_ERROR_RETURN ((LM_ERROR,
143 "Object reference <%s> is nil.\n",
144 ior),
146 Marker marker;
147 ACE_Throughput_Stats throughput;
148 int i=0;
149 ACE_DEBUG ((LM_DEBUG, "High res. timer calibration...."));
150 ACE_High_Res_Timer::global_scale_factor_type gsf =
151 ACE_High_Res_Timer::global_scale_factor ();
152 ACE_DEBUG ((LM_DEBUG, "done\n"));
154 marker.accumulate_into (throughput);
155 CORBA::Short price =0;
156 CORBA::Long cost =0;
157 ACE_hrtime_t throughput_base = ACE_OS::gethrtime ();
158 for (i = 0; i < niterations ; ++i)
160 // Record current time.
161 ACE_hrtime_t latency_base = ACE_OS::gethrtime ();
163 price = server->box_prices ();
165 if (price < 300)
166 cost = server->tickets (5);
168 // Grab timestamp again.
169 ACE_hrtime_t now = ACE_OS::gethrtime ();
171 // Record statistics.
172 marker.sample (now - throughput_base,
173 now - latency_base);
175 if (TAO_debug_level > 0 && i % 100 == 0)
176 ACE_DEBUG ((LM_DEBUG, "(%P|%t) iteration <%d> - price <%d> - cost <%d>\n",
177 i, price, cost));
180 marker.dump_stats (ACE_TEXT("buying tickets "), gsf);
182 server->shutdown ();
185 Test_var server1 =
186 Test::_narrow (object.in ());
188 if (CORBA::is_nil (server1.in ()))
189 ACE_ERROR_RETURN ((LM_ERROR,
190 "Object reference <%C> is nil.\n",
191 ior),
193 Marker marker1;
194 ACE_Throughput_Stats throughput1;
196 ACE_DEBUG ((LM_DEBUG, "High res. timer calibration...."));
197 ACE_High_Res_Timer::global_scale_factor_type gsf1 =
198 ACE_High_Res_Timer::global_scale_factor ();
199 ACE_DEBUG ((LM_DEBUG, "done\n"));
201 marker1.accumulate_into (throughput1);
202 CORBA::Short price1 =0;
203 CORBA::Long cost1 =0;
204 ACE_hrtime_t throughput_base1 = ACE_OS::gethrtime ();
205 for (i = 0; i < niterations ; ++i)
207 // Record current time.
208 ACE_hrtime_t latency_base = ACE_OS::gethrtime ();
210 price1 = server1->box_prices ();
212 if (price1 < 300)
213 cost = server1->tickets (5);
215 // Grab timestamp again.
216 ACE_hrtime_t now = ACE_OS::gethrtime ();
218 // Record statistics.
219 marker.sample (now - throughput_base1,
220 now - latency_base);
222 if (TAO_debug_level > 0 && i % 100 == 0)
223 ACE_DEBUG ((LM_DEBUG, "(%P|%t) iteration = %d\n", i));
226 marker1.dump_stats (ACE_TEXT("buying tickets using a default proxy "), gsf1);
228 server1->shutdown ();
230 orb->destroy ();
232 catch (const CORBA::Exception& ex)
234 ex._tao_print_exception ("Client-side exception:");
235 return 1;
238 return 0;