Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / OctetSeq / OctetSeq.cpp
blob1f0d75f1bada505b5d2679035f51396210c53263
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file OctetSeq.cpp
7 * TAO optimizes octet sequences, this test verifies that the
8 * optimizations do not break any code and that they effectively
9 * improve performance.
11 * @author Carlos O'Ryan
13 //=============================================================================
15 #include "ace/Get_Opt.h"
16 #include "ace/High_Res_Timer.h"
18 #include "tao/ORB.h"
19 #include "tao/CDR.h"
20 #include "testC.h"
21 #include "ace/OS_NS_string.h"
23 static int
24 test_write_octet (TAO_OutputCDR &cdr,
25 char* /* buf */,
26 size_t bufsize)
28 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
29 ACE_Message_Block mb (/* buf, */ bufsize);
30 mb.wr_ptr (bufsize);
31 Test::OctetSeq os (bufsize, &mb);
32 #else
33 Test::OctetSeq os (bufsize);
34 os.length (bufsize);
35 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
38 if ((cdr << os) == 0)
39 return -1;
40 return 0;
43 static int
44 test_read_octet (TAO_InputCDR &cdr,
45 char* /* buf */,
46 size_t /* bufsize */)
48 Test::OctetSeq os;
50 if ((cdr >> os) == 0)
51 return -1;
52 return 0;
55 static int
56 test_write_char (TAO_OutputCDR &cdr,
57 char* buf,
58 size_t bufsize)
60 Test::CharSeq cs (bufsize, bufsize, buf);
62 if ((cdr << cs) == 0)
63 return -1;
64 return 0;
67 static int
68 test_read_char (TAO_InputCDR &cdr,
69 char* /* buf */,
70 size_t /* bufsize */)
72 Test::CharSeq cs;
74 if ((cdr >> cs) == 0)
75 return -1;
76 return 0;
79 typedef
80 int (*Writer)(TAO_OutputCDR& cdr,
81 char* buf, size_t bufsize);
82 typedef
83 int (*Reader)(TAO_InputCDR& cdr,
84 char* buf, size_t bufsize);
86 int
87 run (char* buf, size_t bufsize,
88 size_t n, size_t lo, size_t s,
89 int quiet,
90 const ACE_TCHAR* name,
91 Writer writer, Reader reader)
93 size_t count = 0;
94 double sum_read = 0;
95 double sum_write = 0;
97 for (size_t x = lo; x <= bufsize; x += s)
99 ACE_High_Res_Timer writing;
100 ACE_High_Res_Timer reading;
102 for (size_t i = 0; i < n; ++i)
104 writing.start_incr ();
105 TAO_OutputCDR output;
107 if (writer (output, buf, x) != 0)
108 return -1;
109 writing.stop_incr ();
111 TAO_InputCDR input (output);
112 reading.start_incr ();
113 if (reader (input, buf, x) != 0)
114 return -1;
115 reading.stop_incr ();
118 double m = n;
120 ACE_Time_Value wtv;
121 writing.elapsed_time_incr (wtv);
122 ACE_hrtime_t wusecs = wtv.sec ();
123 wusecs *= static_cast<ACE_UINT32> (ACE_ONE_SECOND_IN_USECS);
124 wusecs += wtv.usec ();
126 ACE_Time_Value rtv;
127 reading.elapsed_time_incr (rtv);
128 ACE_hrtime_t rusecs = rtv.sec ();
129 rusecs *= static_cast<ACE_UINT32> (ACE_ONE_SECOND_IN_USECS);
130 rusecs += rtv.usec ();
132 double write_average = ACE_HRTIME_CONVERSION(wusecs) / m;
133 double read_average = ACE_HRTIME_CONVERSION(rusecs) / m;
135 count++;
136 sum_read += read_average;
137 sum_write += write_average;
138 if (quiet == 0)
140 ACE_DEBUG ((LM_DEBUG,
141 "%s: %d %.3f %.3f\n",
142 name, x, write_average, read_average));
146 if (count != 0)
148 ACE_DEBUG ((LM_DEBUG,
149 "%s total: %.3f %.3f\n",
150 name, sum_write / count, sum_read / count));
152 return 0;
156 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
160 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
162 int n = 64;
163 int lo = 128;
164 int hi = 65536;
165 int s = 32;
167 int quiet = 0;
169 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("qn:l:h:s:"));
170 int opt;
172 while ((opt = get_opt ()) != EOF)
174 switch (opt)
176 case 'n':
177 n = ACE_OS::atoi (get_opt.opt_arg ());
178 break;
179 case 'l':
180 lo = ACE_OS::atoi (get_opt.opt_arg ());
181 break;
182 case 'h':
183 hi = ACE_OS::atoi (get_opt.opt_arg ());
184 break;
185 case 's':
186 s = ACE_OS::atoi (get_opt.opt_arg ());
187 break;
188 case 'q':
189 quiet = 1;
190 break;
192 case '?':
193 default:
194 ACE_DEBUG ((LM_DEBUG,
195 "Usage: %s "
196 "-l low "
197 "-h high "
198 "-s step "
199 "-n n "
200 "\n"
201 "Writes and then reads octet sequences to a CDR stream "
202 "starting from <low> up to <high> incrementing "
203 "by <step>, at each step run <n> iterations to "
204 "average."
205 "\n",
206 argv[0]));
207 return -1;
211 ACE_DEBUG ((LM_DEBUG, "Running:\n"
212 " low: %d\n"
213 " hi : %d\n"
214 " s : %d\n"
215 " n : %d\n",
216 lo, hi, s, n));
218 // Create a "big" buffer and fill it up.
219 char* buf = new char[hi];
220 CORBA::Long l = 0xdeadbeef;
221 for (int i = 0; i < hi / (int) sizeof (l); ++i)
223 ACE_OS::memcpy (buf + sizeof (l) * i, &l, sizeof (l));
226 if (run (buf, hi,
227 n, lo, s, quiet,
228 ACE_TEXT ("OCTET"), test_write_octet, test_read_octet) != 0)
229 return 1;
231 if (run (buf, hi,
232 n, lo, s, quiet,
233 ACE_TEXT ("CHAR"), test_write_char, test_read_char) != 0)
234 return 1;
235 delete[] buf;
237 catch (const CORBA::Exception& ex)
239 ex._tao_print_exception ("OctetSeq");
242 return 0;