blob: db00347843ee9ec8eb4ca6f6459e3984e10301a7 [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>
Mark Sleea3302652006-10-25 19:03:32 +00005#include <transport/TTransportUtils.h>
Marc Slemko6be374b2006-08-04 03:16:25 +00006#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;
Mark Sleea3302652006-10-25 19:03:32 +000036 bool framed = false;
37 bool frameInput = true;
Mark Sleee8540632006-05-30 09:24:40 +000038
Mark Sleea3302652006-10-25 19:03:32 +000039 for (int i = 0; i < argc; ++i) {
40 if (strcmp(argv[i], "-h") == 0) {
41 char* pch = strtok(argv[++i], ":");
42 if (pch != NULL) {
43 host = string(pch);
44 }
45 pch = strtok(NULL, ":");
46 if (pch != NULL) {
47 port = atoi(pch);
48 }
49 } else if (strcmp(argv[i], "-n") == 0) {
50 numTests = atoi(argv[++i]);
51 } else if (strcmp(argv[i], "-f") == 0) {
52 framed = true;
53 } else if (strcmp(argv[i], "-fo") == 0) {
54 framed = true;
55 frameInput = false;
56 }
Mark Sleee8540632006-05-30 09:24:40 +000057 }
Mark Sleea3302652006-10-25 19:03:32 +000058
59
60 shared_ptr<TTransport> transport;
Mark Sleee8540632006-05-30 09:24:40 +000061
Marc Slemko6be374b2006-08-04 03:16:25 +000062 shared_ptr<TSocket> socket(new TSocket(host, port));
Mark Sleea3302652006-10-25 19:03:32 +000063
64 if (framed) {
65 shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
66 framedSocket->setRead(frameInput);
67 transport = framedSocket;
68 if (frameInput) {
69 printf("Using bi-directional framed transport mode\n");
70 } else {
71 printf("Using framed output only mode\n");
72 }
73 } else {
74 shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
75 transport = bufferedSocket;
76 }
77
78 shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport, transport));
79 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +000080
81 uint64_t time_min = 0;
82 uint64_t time_max = 0;
83 uint64_t time_tot = 0;
84
Mark Sleee8540632006-05-30 09:24:40 +000085 int test = 0;
86 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +000087
Mark Slee95771002006-06-07 06:53:25 +000088 try {
Mark Sleea3302652006-10-25 19:03:32 +000089 transport->open();
Mark Slee95771002006-06-07 06:53:25 +000090 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000091 printf("Connect failed: %s\n", ttx.what());
Mark Sleee8540632006-05-30 09:24:40 +000092 continue;
93 }
Mark Sleed788b2e2006-09-07 01:26:35 +000094
95 /**
96 * CONNECT TEST
97 */
98 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +000099
100 uint64_t start = now();
Mark Sleee8540632006-05-30 09:24:40 +0000101
102 /**
103 * VOID TEST
104 */
105 printf("testVoid()");
106 testClient.testVoid();
107 printf(" = void\n");
108
109 /**
110 * STRING TEST
111 */
112 printf("testString(\"Test\")");
113 string s = testClient.testString("Test");
114 printf(" = \"%s\"\n", s.c_str());
115
116 /**
117 * BYTE TEST
118 */
119 printf("testByte(1)");
120 uint8_t u8 = testClient.testByte(1);
121 printf(" = %d\n", (int)u8);
Mark Slee6e536442006-06-30 18:28:50 +0000122
Mark Sleee8540632006-05-30 09:24:40 +0000123 /**
124 * I32 TEST
125 */
126 printf("testI32(-1)");
127 int32_t i32 = testClient.testI32(-1);
128 printf(" = %d\n", i32);
129
130 /**
Mark Sleee8540632006-05-30 09:24:40 +0000131 * I64 TEST
132 */
133 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000134 int64_t i64 = testClient.testI64(-34359738368LL);
Mark Sleed3d733a2006-09-01 22:19:06 +0000135 printf(" = %ld\n", i64);
Mark Sleec98d0502006-09-06 02:42:25 +0000136
137 /**
138 * DOUBLE TEST
139 */
140 printf("testDouble(-5.2098523)");
141 double dub = testClient.testDouble(-5.2098523);
142 printf(" = %lf\n", dub);
Mark Sleee8540632006-05-30 09:24:40 +0000143
144 /**
145 * STRUCT TEST
146 */
Mark Slee6e536442006-06-30 18:28:50 +0000147 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000148 Xtruct out;
149 out.string_thing = "Zero";
150 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000151 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000152 out.i64_thing = -5;
153 Xtruct in = testClient.testStruct(out);
Mark Sleed3d733a2006-09-01 22:19:06 +0000154 printf(" = {\"%s\", %d, %d, %ld}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000155 in.string_thing.c_str(),
156 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000157 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000158 in.i64_thing);
159
160 /**
161 * NESTED STRUCT TEST
162 */
Mark Slee6e536442006-06-30 18:28:50 +0000163 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000164 Xtruct2 out2;
165 out2.byte_thing = 1;
166 out2.struct_thing = out;
167 out2.i32_thing = 5;
168 Xtruct2 in2 = testClient.testNest(out2);
169 in = in2.struct_thing;
Mark Sleed3d733a2006-09-01 22:19:06 +0000170 printf(" = {%d, {\"%s\", %d, %d, %ld}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000171 in2.byte_thing,
172 in.string_thing.c_str(),
173 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000174 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000175 in.i64_thing,
176 in2.i32_thing);
177
178 /**
179 * MAP TEST
180 */
181 map<int32_t,int32_t> mapout;
182 for (int32_t i = 0; i < 5; ++i) {
183 mapout.insert(make_pair(i, i-10));
184 }
185 printf("testMap({");
186 map<int32_t, int32_t>::const_iterator m_iter;
187 bool first = true;
188 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
189 if (first) {
190 first = false;
191 } else {
192 printf(", ");
193 }
194 printf("%d => %d", m_iter->first, m_iter->second);
195 }
196 printf("})");
197 map<int32_t,int32_t> mapin = testClient.testMap(mapout);
198 printf(" = {");
199 first = true;
200 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
201 if (first) {
202 first = false;
203 } else {
204 printf(", ");
205 }
206 printf("%d => %d", m_iter->first, m_iter->second);
207 }
208 printf("}\n");
209
210 /**
211 * SET TEST
212 */
213 set<int32_t> setout;
214 for (int32_t i = -2; i < 3; ++i) {
215 setout.insert(i);
216 }
217 printf("testSet({");
218 set<int32_t>::const_iterator s_iter;
219 first = true;
220 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
221 if (first) {
222 first = false;
223 } else {
224 printf(", ");
225 }
226 printf("%d", *s_iter);
227 }
228 printf("})");
229 set<int32_t> setin = testClient.testSet(setout);
230 printf(" = {");
231 first = true;
232 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
233 if (first) {
234 first = false;
235 } else {
236 printf(", ");
237 }
238 printf("%d", *s_iter);
239 }
240 printf("}\n");
241
242 /**
243 * LIST TEST
244 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000245 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000246 for (int32_t i = -2; i < 3; ++i) {
247 listout.push_back(i);
248 }
249 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000250 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000251 first = true;
252 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
253 if (first) {
254 first = false;
255 } else {
256 printf(", ");
257 }
258 printf("%d", *l_iter);
259 }
260 printf("})");
Mark Sleeb9acf982006-10-10 01:57:32 +0000261 vector<int32_t> listin = testClient.testList(listout);
Mark Sleee8540632006-05-30 09:24:40 +0000262 printf(" = {");
263 first = true;
264 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
265 if (first) {
266 first = false;
267 } else {
268 printf(", ");
269 }
270 printf("%d", *l_iter);
271 }
272 printf("}\n");
273
274 /**
275 * ENUM TEST
276 */
277 printf("testEnum(ONE)");
278 Numberz ret = testClient.testEnum(ONE);
279 printf(" = %d\n", ret);
280
281 printf("testEnum(TWO)");
282 ret = testClient.testEnum(TWO);
283 printf(" = %d\n", ret);
284
285 printf("testEnum(THREE)");
286 ret = testClient.testEnum(THREE);
287 printf(" = %d\n", ret);
288
289 printf("testEnum(FIVE)");
290 ret = testClient.testEnum(FIVE);
291 printf(" = %d\n", ret);
292
293 printf("testEnum(EIGHT)");
294 ret = testClient.testEnum(EIGHT);
295 printf(" = %d\n", ret);
296
297 /**
298 * TYPEDEF TEST
299 */
300 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000301 UserId uid = testClient.testTypedef(309858235082523LL);
Mark Sleed3d733a2006-09-01 22:19:06 +0000302 printf(" = %ld\n", uid);
Mark Sleee8540632006-05-30 09:24:40 +0000303
304 /**
305 * NESTED MAP TEST
306 */
307 printf("testMapMap(1)");
308 map<int32_t, map<int32_t, int32_t> > mm = testClient.testMapMap(1);
309 printf(" = {");
310 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
311 for (mi = mm.begin(); mi != mm.end(); ++mi) {
312 printf("%d => {", mi->first);
313 map<int32_t, int32_t>::const_iterator mi2;
314 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
315 printf("%d => %d, ", mi2->first, mi2->second);
316 }
317 printf("}, ");
318 }
319 printf("}\n");
320
321 /**
322 * INSANITY TEST
323 */
324 Insanity insane;
325 insane.userMap.insert(make_pair(FIVE, 5000));
326 Xtruct truck;
327 truck.string_thing = "Truck";
328 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000329 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000330 truck.i64_thing = 8;
331 insane.xtructs.push_back(truck);
332 printf("testInsanity()");
333 map<UserId, map<Numberz,Insanity> > whoa = testClient.testInsanity(insane);
334 printf(" = {");
335 map<UserId, map<Numberz,Insanity> >::const_iterator i_iter;
336 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000337 printf("%ld => {", i_iter->first);
Mark Sleee8540632006-05-30 09:24:40 +0000338 map<Numberz,Insanity>::const_iterator i2_iter;
339 for (i2_iter = i_iter->second.begin();
340 i2_iter != i_iter->second.end();
341 ++i2_iter) {
342 printf("%d => {", i2_iter->first);
343 map<Numberz, UserId> userMap = i2_iter->second.userMap;
344 map<Numberz, UserId>::const_iterator um;
345 printf("{");
346 for (um = userMap.begin(); um != userMap.end(); ++um) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000347 printf("%d => %ld, ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000348 }
349 printf("}, ");
350
Mark Sleeb9acf982006-10-10 01:57:32 +0000351 vector<Xtruct> xtructs = i2_iter->second.xtructs;
352 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000353 printf("{");
354 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000355 printf("{\"%s\", %d, %d, %ld}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000356 x->string_thing.c_str(),
357 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000358 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000359 x->i64_thing);
360 }
361 printf("}");
362
363 printf("}, ");
364 }
365 printf("}, ");
366 }
367 printf("}\n");
368
Marc Slemko71d4e472006-08-15 22:34:04 +0000369 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000370
Marc Slemkobf4fd192006-08-15 21:29:39 +0000371 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000372 printf("testClient.testException(\"Xception\") =>");
373 testClient.testException("Xception");
374 printf(" void\nFAILURE\n");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000375
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000376 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000377 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000378 }
Marc Slemko71d4e472006-08-15 22:34:04 +0000379
Marc Slemkobf4fd192006-08-15 21:29:39 +0000380 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000381 printf("testClient.testException(\"success\") =>");
382 testClient.testException("success");
383 printf(" void\n");
384 } catch(...) {
385 printf(" exception\nFAILURE\n");
386 }
387
388 /* test multi exception */
389
390 try {
391 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
392 Xtruct result = testClient.testMultiException("Xception", "test 1");
393 printf(" result\nFAILURE\n");
Mark Sleed3d733a2006-09-01 22:19:06 +0000394 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000395 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
396 }
397
398 try {
399 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000400 Xtruct result = testClient.testMultiException("Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000401 printf(" result\nFAILURE\n");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000402
Mark Sleed3d733a2006-09-01 22:19:06 +0000403 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000404 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000405 }
406
Marc Slemko71d4e472006-08-15 22:34:04 +0000407 try {
408 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
409 Xtruct result = testClient.testMultiException("success", "test 3");
410 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
411 } catch(...) {
412 printf(" exception\nFAILURE\n");
413 }
Marc Slemkobf4fd192006-08-15 21:29:39 +0000414
Marc Slemkobf4fd192006-08-15 21:29:39 +0000415 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000416 uint64_t tot = stop-start;
417
Mark Sleed3d733a2006-09-01 22:19:06 +0000418 printf("Total time: %lu us\n", stop-start);
Marc Slemkobf4fd192006-08-15 21:29:39 +0000419
Mark Sleed788b2e2006-09-07 01:26:35 +0000420 time_tot += tot;
421 if (time_min == 0 || tot < time_min) {
422 time_min = tot;
423 }
424 if (tot > time_max) {
425 time_max = tot;
426 }
427
Mark Sleea3302652006-10-25 19:03:32 +0000428 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000429 }
430
Marc Slemko6be374b2006-08-04 03:16:25 +0000431 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000432 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000433
434 uint64_t time_avg = time_tot / numTests;
435
436 printf("Min time: %lu us\n", time_min);
437 printf("Max time: %lu us\n", time_max);
438 printf("Avg time: %lu us\n", time_avg);
439
Mark Sleee8540632006-05-30 09:24:40 +0000440 return 0;
441}