blob: 83e8f795d9f40f10b6f1d075460275634d566548 [file] [log] [blame]
Mark Sleee8540632006-05-30 09:24:40 +00001#include <stdio.h>
2#include <unistd.h>
Mark Slee95771002006-06-07 06:53:25 +00003#include <sys/time.h>
Marc Slemko6be374b2006-08-04 03:16:25 +00004#include <protocol/TBinaryProtocol.h>
5#include <transport/TBufferedTransport.h>
6#include <transport/TSocket.h>
Mark Sleee8540632006-05-30 09:24:40 +00007
Marc Slemko6be374b2006-08-04 03:16:25 +00008#include <boost/shared_ptr.hpp>
9#include "ThriftTest.h"
10
11using namespace boost;
12using namespace std;
13using namespace facebook::thrift;
14using namespace facebook::thrift::protocol;
15using namespace facebook::thrift::transport;
Marc Slemkobf4fd192006-08-15 21:29:39 +000016using namespace thrift::test;
Marc Slemko6be374b2006-08-04 03:16:25 +000017
18//extern uint32_t g_socket_syscalls;
Mark Slee95771002006-06-07 06:53:25 +000019
20// Current time, microseconds since the epoch
21uint64_t now()
22{
23 long long ret;
24 struct timeval tv;
25
26 gettimeofday(&tv, NULL);
27 ret = tv.tv_sec;
28 ret = ret*1000*1000 + tv.tv_usec;
29 return ret;
30}
31
Mark Sleee8540632006-05-30 09:24:40 +000032int main(int argc, char** argv) {
33 string host = "localhost";
34 int port = 9090;
35 int numTests = 1;
36
37 if (argc > 1) {
38 host = argv[1];
39 }
40 if (argc > 2) {
41 port = atoi(argv[2]);
42 }
43 if (argc > 3) {
44 numTests = atoi(argv[3]);
45 }
46
Marc Slemko6be374b2006-08-04 03:16:25 +000047 shared_ptr<TSocket> socket(new TSocket(host, port));
Mark Sleed788b2e2006-09-07 01:26:35 +000048 shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
Marc Slemko6be374b2006-08-04 03:16:25 +000049 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol());
50 ThriftTestClient testClient(bufferedSocket, binaryProtocol);
Mark Sleed788b2e2006-09-07 01:26:35 +000051
52 uint64_t time_min = 0;
53 uint64_t time_max = 0;
54 uint64_t time_tot = 0;
55
Mark Sleee8540632006-05-30 09:24:40 +000056 int test = 0;
57 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +000058
Mark Slee95771002006-06-07 06:53:25 +000059 try {
Marc Slemko6be374b2006-08-04 03:16:25 +000060 bufferedSocket->open();
Mark Slee95771002006-06-07 06:53:25 +000061 } catch (TTransportException& ttx) {
62 printf("Connect failed: %s\n", ttx.getMessage().c_str());
Mark Sleee8540632006-05-30 09:24:40 +000063 continue;
64 }
Mark Sleed788b2e2006-09-07 01:26:35 +000065
66 /**
67 * CONNECT TEST
68 */
69 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +000070
71 uint64_t start = now();
Mark Sleee8540632006-05-30 09:24:40 +000072
73 /**
74 * VOID TEST
75 */
76 printf("testVoid()");
77 testClient.testVoid();
78 printf(" = void\n");
79
80 /**
81 * STRING TEST
82 */
83 printf("testString(\"Test\")");
84 string s = testClient.testString("Test");
85 printf(" = \"%s\"\n", s.c_str());
86
87 /**
88 * BYTE TEST
89 */
90 printf("testByte(1)");
91 uint8_t u8 = testClient.testByte(1);
92 printf(" = %d\n", (int)u8);
Mark Slee6e536442006-06-30 18:28:50 +000093
Mark Sleee8540632006-05-30 09:24:40 +000094 /**
95 * I32 TEST
96 */
97 printf("testI32(-1)");
98 int32_t i32 = testClient.testI32(-1);
99 printf(" = %d\n", i32);
100
101 /**
Mark Sleee8540632006-05-30 09:24:40 +0000102 * I64 TEST
103 */
104 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000105 int64_t i64 = testClient.testI64(-34359738368LL);
Mark Sleed3d733a2006-09-01 22:19:06 +0000106 printf(" = %ld\n", i64);
Mark Sleec98d0502006-09-06 02:42:25 +0000107
108 /**
109 * DOUBLE TEST
110 */
111 printf("testDouble(-5.2098523)");
112 double dub = testClient.testDouble(-5.2098523);
113 printf(" = %lf\n", dub);
Mark Sleee8540632006-05-30 09:24:40 +0000114
115 /**
116 * STRUCT TEST
117 */
Mark Slee6e536442006-06-30 18:28:50 +0000118 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000119 Xtruct out;
120 out.string_thing = "Zero";
121 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000122 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000123 out.i64_thing = -5;
124 Xtruct in = testClient.testStruct(out);
Mark Sleed3d733a2006-09-01 22:19:06 +0000125 printf(" = {\"%s\", %d, %d, %ld}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000126 in.string_thing.c_str(),
127 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000128 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000129 in.i64_thing);
130
131 /**
132 * NESTED STRUCT TEST
133 */
Mark Slee6e536442006-06-30 18:28:50 +0000134 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000135 Xtruct2 out2;
136 out2.byte_thing = 1;
137 out2.struct_thing = out;
138 out2.i32_thing = 5;
139 Xtruct2 in2 = testClient.testNest(out2);
140 in = in2.struct_thing;
Mark Sleed3d733a2006-09-01 22:19:06 +0000141 printf(" = {%d, {\"%s\", %d, %d, %ld}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000142 in2.byte_thing,
143 in.string_thing.c_str(),
144 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000145 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000146 in.i64_thing,
147 in2.i32_thing);
148
149 /**
150 * MAP TEST
151 */
152 map<int32_t,int32_t> mapout;
153 for (int32_t i = 0; i < 5; ++i) {
154 mapout.insert(make_pair(i, i-10));
155 }
156 printf("testMap({");
157 map<int32_t, int32_t>::const_iterator m_iter;
158 bool first = true;
159 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
160 if (first) {
161 first = false;
162 } else {
163 printf(", ");
164 }
165 printf("%d => %d", m_iter->first, m_iter->second);
166 }
167 printf("})");
168 map<int32_t,int32_t> mapin = testClient.testMap(mapout);
169 printf(" = {");
170 first = true;
171 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
172 if (first) {
173 first = false;
174 } else {
175 printf(", ");
176 }
177 printf("%d => %d", m_iter->first, m_iter->second);
178 }
179 printf("}\n");
180
181 /**
182 * SET TEST
183 */
184 set<int32_t> setout;
185 for (int32_t i = -2; i < 3; ++i) {
186 setout.insert(i);
187 }
188 printf("testSet({");
189 set<int32_t>::const_iterator s_iter;
190 first = true;
191 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
192 if (first) {
193 first = false;
194 } else {
195 printf(", ");
196 }
197 printf("%d", *s_iter);
198 }
199 printf("})");
200 set<int32_t> setin = testClient.testSet(setout);
201 printf(" = {");
202 first = true;
203 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
204 if (first) {
205 first = false;
206 } else {
207 printf(", ");
208 }
209 printf("%d", *s_iter);
210 }
211 printf("}\n");
212
213 /**
214 * LIST TEST
215 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000216 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000217 for (int32_t i = -2; i < 3; ++i) {
218 listout.push_back(i);
219 }
220 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000221 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000222 first = true;
223 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
224 if (first) {
225 first = false;
226 } else {
227 printf(", ");
228 }
229 printf("%d", *l_iter);
230 }
231 printf("})");
Mark Sleeb9acf982006-10-10 01:57:32 +0000232 vector<int32_t> listin = testClient.testList(listout);
Mark Sleee8540632006-05-30 09:24:40 +0000233 printf(" = {");
234 first = true;
235 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
236 if (first) {
237 first = false;
238 } else {
239 printf(", ");
240 }
241 printf("%d", *l_iter);
242 }
243 printf("}\n");
244
245 /**
246 * ENUM TEST
247 */
248 printf("testEnum(ONE)");
249 Numberz ret = testClient.testEnum(ONE);
250 printf(" = %d\n", ret);
251
252 printf("testEnum(TWO)");
253 ret = testClient.testEnum(TWO);
254 printf(" = %d\n", ret);
255
256 printf("testEnum(THREE)");
257 ret = testClient.testEnum(THREE);
258 printf(" = %d\n", ret);
259
260 printf("testEnum(FIVE)");
261 ret = testClient.testEnum(FIVE);
262 printf(" = %d\n", ret);
263
264 printf("testEnum(EIGHT)");
265 ret = testClient.testEnum(EIGHT);
266 printf(" = %d\n", ret);
267
268 /**
269 * TYPEDEF TEST
270 */
271 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000272 UserId uid = testClient.testTypedef(309858235082523LL);
Mark Sleed3d733a2006-09-01 22:19:06 +0000273 printf(" = %ld\n", uid);
Mark Sleee8540632006-05-30 09:24:40 +0000274
275 /**
276 * NESTED MAP TEST
277 */
278 printf("testMapMap(1)");
279 map<int32_t, map<int32_t, int32_t> > mm = testClient.testMapMap(1);
280 printf(" = {");
281 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
282 for (mi = mm.begin(); mi != mm.end(); ++mi) {
283 printf("%d => {", mi->first);
284 map<int32_t, int32_t>::const_iterator mi2;
285 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
286 printf("%d => %d, ", mi2->first, mi2->second);
287 }
288 printf("}, ");
289 }
290 printf("}\n");
291
292 /**
293 * INSANITY TEST
294 */
295 Insanity insane;
296 insane.userMap.insert(make_pair(FIVE, 5000));
297 Xtruct truck;
298 truck.string_thing = "Truck";
299 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000300 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000301 truck.i64_thing = 8;
302 insane.xtructs.push_back(truck);
303 printf("testInsanity()");
304 map<UserId, map<Numberz,Insanity> > whoa = testClient.testInsanity(insane);
305 printf(" = {");
306 map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
307 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000308 printf("%ld => {", i_iter->first);
Mark Sleee8540632006-05-30 09:24:40 +0000309 map<Numberz,Insanity>::const_iterator i2_iter;
310 for (i2_iter = i_iter->second.begin();
311 i2_iter != i_iter->second.end();
312 ++i2_iter) {
313 printf("%d => {", i2_iter->first);
314 map<Numberz, UserId> userMap = i2_iter->second.userMap;
315 map<Numberz, UserId>::const_iterator um;
316 printf("{");
317 for (um = userMap.begin(); um != userMap.end(); ++um) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000318 printf("%d => %ld, ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000319 }
320 printf("}, ");
321
Mark Sleeb9acf982006-10-10 01:57:32 +0000322 vector<Xtruct> xtructs = i2_iter->second.xtructs;
323 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000324 printf("{");
325 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000326 printf("{\"%s\", %d, %d, %ld}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000327 x->string_thing.c_str(),
328 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000329 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000330 x->i64_thing);
331 }
332 printf("}");
333
334 printf("}, ");
335 }
336 printf("}, ");
337 }
338 printf("}\n");
339
Marc Slemko71d4e472006-08-15 22:34:04 +0000340 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000341
Marc Slemkobf4fd192006-08-15 21:29:39 +0000342 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000343 printf("testClient.testException(\"Xception\") =>");
344 testClient.testException("Xception");
345 printf(" void\nFAILURE\n");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000346
347 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000348 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000349 }
Marc Slemko71d4e472006-08-15 22:34:04 +0000350
Marc Slemkobf4fd192006-08-15 21:29:39 +0000351 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000352 printf("testClient.testException(\"success\") =>");
353 testClient.testException("success");
354 printf(" void\n");
355 } catch(...) {
356 printf(" exception\nFAILURE\n");
357 }
358
359 /* test multi exception */
360
361 try {
362 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
363 Xtruct result = testClient.testMultiException("Xception", "test 1");
364 printf(" result\nFAILURE\n");
Mark Sleed3d733a2006-09-01 22:19:06 +0000365 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000366 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
367 }
368
369 try {
370 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000371 Xtruct result = testClient.testMultiException("Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000372 printf(" result\nFAILURE\n");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000373
Mark Sleed3d733a2006-09-01 22:19:06 +0000374 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000375 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000376 }
377
Marc Slemko71d4e472006-08-15 22:34:04 +0000378 try {
379 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
380 Xtruct result = testClient.testMultiException("success", "test 3");
381 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
382 } catch(...) {
383 printf(" exception\nFAILURE\n");
384 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000385
Marc Slemkobf4fd192006-08-15 21:29:39 +0000386 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000387 uint64_t tot = stop-start;
388
Mark Sleed3d733a2006-09-01 22:19:06 +0000389 printf("Total time: %lu us\n", stop-start);
Marc Slemkobf4fd192006-08-15 21:29:39 +0000390
Mark Sleed788b2e2006-09-07 01:26:35 +0000391 time_tot += tot;
392 if (time_min == 0 || tot < time_min) {
393 time_min = tot;
394 }
395 if (tot > time_max) {
396 time_max = tot;
397 }
398
Marc Slemko6be374b2006-08-04 03:16:25 +0000399 bufferedSocket->close();
Mark Sleee8540632006-05-30 09:24:40 +0000400 }
401
Marc Slemko6be374b2006-08-04 03:16:25 +0000402 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000403 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000404
405 uint64_t time_avg = time_tot / numTests;
406
407 printf("Min time: %lu us\n", time_min);
408 printf("Max time: %lu us\n", time_max);
409 printf("Avg time: %lu us\n", time_avg);
410
Mark Sleee8540632006-05-30 09:24:40 +0000411 return 0;
412}