blob: 379761d85d2d8895039f46da52f8bef58d94a6d6 [file] [log] [blame]
Mark Slee95771002006-06-07 06:53:25 +00001package com.facebook.thrift.test;
2
Mark Slee78f58e22006-09-02 04:17:07 +00003// Generated code
4import thrift.test.*;
5
Mark Sleee129a2d2007-02-21 05:17:48 +00006import com.facebook.thrift.TApplicationException;
Mark Slee844ac122007-11-27 08:38:52 +00007import com.facebook.thrift.TSerializer;
Mark Sleeb9acf982006-10-10 01:57:32 +00008import com.facebook.thrift.transport.TTransport;
Mark Slee95771002006-06-07 06:53:25 +00009import com.facebook.thrift.transport.TSocket;
Mark Sleeb9acf982006-10-10 01:57:32 +000010import com.facebook.thrift.transport.THttpClient;
Mark Sleea3302652006-10-25 19:03:32 +000011import com.facebook.thrift.transport.TFramedTransport;
Mark Slee95771002006-06-07 06:53:25 +000012import com.facebook.thrift.transport.TTransportException;
13import com.facebook.thrift.protocol.TBinaryProtocol;
David Reiss4f15f592008-02-23 22:07:43 +000014import com.facebook.thrift.protocol.TSimpleJSONProtocol;
Mark Slee95771002006-06-07 06:53:25 +000015
Mark Slee22360b22008-02-09 00:18:32 +000016import java.util.Map;
Mark Slee95771002006-06-07 06:53:25 +000017import java.util.HashMap;
Mark Slee22360b22008-02-09 00:18:32 +000018import java.util.Set;
Mark Slee95771002006-06-07 06:53:25 +000019import java.util.HashSet;
Mark Slee22360b22008-02-09 00:18:32 +000020import java.util.List;
Mark Slee95771002006-06-07 06:53:25 +000021import java.util.ArrayList;
22
23/**
24 * Test Java client for thrift. Essentially just a copy of the C++ version,
25 * this makes a variety of requests to enable testing for both performance and
26 * correctness of the output.
27 *
28 * @author Mark Slee <mcslee@facebook.com>
29 */
30public class TestClient {
31 public static void main(String [] args) {
32 try {
33 String host = "localhost";
34 int port = 9090;
Mark Sleeb9acf982006-10-10 01:57:32 +000035 String url = null;
Mark Slee95771002006-06-07 06:53:25 +000036 int numTests = 1;
Mark Sleea3302652006-10-25 19:03:32 +000037 boolean framed = false;
38 boolean framedInput = true;
39 boolean framedOutput = true;
Mark Sleeb9acf982006-10-10 01:57:32 +000040
41 try {
42 for (int i = 0; i < args.length; ++i) {
43 if (args[i].equals("-h")) {
Mark Sleea3302652006-10-25 19:03:32 +000044 String[] hostport = (args[++i]).split(":");
Mark Sleeb9acf982006-10-10 01:57:32 +000045 host = hostport[0];
Mark Slee844ac122007-11-27 08:38:52 +000046 port = Integer.valueOf(hostport[1]);
Mark Sleea3302652006-10-25 19:03:32 +000047 } else if (args[i].equals("-f") || args[i].equals("-framed")) {
48 framed = true;
49 } else if (args[i].equals("-fo")) {
50 framed = true;
51 framedInput = false;
52 } else if (args[i].equals("-u")) {
Mark Sleeb9acf982006-10-10 01:57:32 +000053 url = args[++i];
Mark Sleea3302652006-10-25 19:03:32 +000054 } else if (args[i].equals("-n")) {
Mark Sleeb9acf982006-10-10 01:57:32 +000055 numTests = Integer.valueOf(args[++i]);
56 }
57 }
58 } catch (Exception x) {
59 x.printStackTrace();
Mark Slee95771002006-06-07 06:53:25 +000060 }
Mark Slee844ac122007-11-27 08:38:52 +000061
Mark Sleeb9acf982006-10-10 01:57:32 +000062 TTransport transport;
Mark Slee844ac122007-11-27 08:38:52 +000063
Mark Sleeb9acf982006-10-10 01:57:32 +000064 if (url != null) {
65 transport = new THttpClient(url);
66 } else {
Mark Sleea3302652006-10-25 19:03:32 +000067 TSocket socket = new TSocket(host, port);
68 socket.setTimeout(1000);
69 transport = socket;
70 if (framed) {
71 transport = new TFramedTransport(transport,
72 framedInput,
73 framedOutput);
74 }
Mark Sleeb9acf982006-10-10 01:57:32 +000075 }
76
Mark Slee95771002006-06-07 06:53:25 +000077 TBinaryProtocol binaryProtocol =
Mark Slee7dafc942007-08-09 03:40:15 +000078 new TBinaryProtocol(transport);
Mark Slee78f58e22006-09-02 04:17:07 +000079 ThriftTest.Client testClient =
Mark Slee1dd819c2006-10-26 04:56:18 +000080 new ThriftTest.Client(binaryProtocol);
Mark Slee844ac122007-11-27 08:38:52 +000081 Insanity insane = new Insanity();
Mark Slee95771002006-06-07 06:53:25 +000082
Mark Sleed788b2e2006-09-07 01:26:35 +000083 long timeMin = 0;
84 long timeMax = 0;
85 long timeTot = 0;
86
Mark Slee95771002006-06-07 06:53:25 +000087 for (int test = 0; test < numTests; ++test) {
88
89 /**
90 * CONNECT TEST
91 */
Mark Sleed788b2e2006-09-07 01:26:35 +000092 System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
Mark Slee95771002006-06-07 06:53:25 +000093 try {
Mark Sleeb9acf982006-10-10 01:57:32 +000094 transport.open();
Mark Slee95771002006-06-07 06:53:25 +000095 } catch (TTransportException ttx) {
96 System.out.println("Connect failed: " + ttx.getMessage());
97 continue;
98 }
99
Mark Sleed788b2e2006-09-07 01:26:35 +0000100 long start = System.nanoTime();
Mark Slee844ac122007-11-27 08:38:52 +0000101
Mark Slee95771002006-06-07 06:53:25 +0000102 /**
103 * VOID TEST
104 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000105 try {
106 System.out.print("testVoid()");
107 testClient.testVoid();
108 System.out.print(" = void\n");
109 } catch (TApplicationException tax) {
110 tax.printStackTrace();
111 }
Mark Slee95771002006-06-07 06:53:25 +0000112
113 /**
114 * STRING TEST
115 */
116 System.out.print("testString(\"Test\")");
Mark Slee78f58e22006-09-02 04:17:07 +0000117 String s = testClient.testString("Test");
118 System.out.print(" = \"" + s + "\"\n");
119
Mark Slee95771002006-06-07 06:53:25 +0000120 /**
121 * BYTE TEST
122 */
123 System.out.print("testByte(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000124 byte i8 = testClient.testByte((byte)1);
125 System.out.print(" = " + i8 + "\n");
Mark Slee844ac122007-11-27 08:38:52 +0000126
Mark Slee95771002006-06-07 06:53:25 +0000127 /**
128 * I32 TEST
129 */
130 System.out.print("testI32(-1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000131 int i32 = testClient.testI32(-1);
132 System.out.print(" = " + i32 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000133
134 /**
Mark Slee95771002006-06-07 06:53:25 +0000135 * I64 TEST
136 */
137 System.out.print("testI64(-34359738368)");
Mark Slee78f58e22006-09-02 04:17:07 +0000138 long i64 = testClient.testI64(-34359738368L);
139 System.out.print(" = " + i64 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000140
141 /**
Mark Sleec98d0502006-09-06 02:42:25 +0000142 * DOUBLE TEST
143 */
144 System.out.print("testDouble(5.325098235)");
145 double dub = testClient.testDouble(5.325098235);
146 System.out.print(" = " + dub + "\n");
147
148 /**
Mark Slee95771002006-06-07 06:53:25 +0000149 * STRUCT TEST
150 */
Mark Slee6e536442006-06-30 18:28:50 +0000151 System.out.print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee95771002006-06-07 06:53:25 +0000152 Xtruct out = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000153 out.string_thing = "Zero";
154 out.byte_thing = (byte) 1;
155 out.i32_thing = -3;
156 out.i64_thing = -5;
Mark Slee95771002006-06-07 06:53:25 +0000157 Xtruct in = testClient.testStruct(out);
Mark Sleed788b2e2006-09-07 01:26:35 +0000158 System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000159
160 /**
161 * NESTED STRUCT TEST
162 */
Mark Slee6e536442006-06-30 18:28:50 +0000163 System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Slee95771002006-06-07 06:53:25 +0000164 Xtruct2 out2 = new Xtruct2();
Mark Slee78f58e22006-09-02 04:17:07 +0000165 out2.byte_thing = (short)1;
Mark Slee95771002006-06-07 06:53:25 +0000166 out2.struct_thing = out;
Mark Slee78f58e22006-09-02 04:17:07 +0000167 out2.i32_thing = 5;
Mark Slee95771002006-06-07 06:53:25 +0000168 Xtruct2 in2 = testClient.testNest(out2);
169 in = in2.struct_thing;
Mark Sleed788b2e2006-09-07 01:26:35 +0000170 System.out.print(" = {" + in2.byte_thing + ", {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}, " + in2.i32_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000171
172 /**
173 * MAP TEST
174 */
Mark Slee22360b22008-02-09 00:18:32 +0000175 Map<Integer,Integer> mapout = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000176 for (int i = 0; i < 5; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000177 mapout.put(i, i-10);
Mark Slee95771002006-06-07 06:53:25 +0000178 }
179 System.out.print("testMap({");
180 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000181 for (int key : mapout.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000182 if (first) {
183 first = false;
184 } else {
185 System.out.print(", ");
186 }
Mark Slee78f58e22006-09-02 04:17:07 +0000187 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000188 }
189 System.out.print("})");
Mark Slee22360b22008-02-09 00:18:32 +0000190 Map<Integer,Integer> mapin = testClient.testMap(mapout);
Mark Slee95771002006-06-07 06:53:25 +0000191 System.out.print(" = {");
192 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000193 for (int key : mapin.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000194 if (first) {
195 first = false;
196 } else {
197 System.out.print(", ");
198 }
Mark Slee78f58e22006-09-02 04:17:07 +0000199 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000200 }
201 System.out.print("}\n");
202
203 /**
204 * SET TEST
205 */
Mark Slee22360b22008-02-09 00:18:32 +0000206 Set<Integer> setout = new HashSet<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000207 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000208 setout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000209 }
210 System.out.print("testSet({");
211 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000212 for (int elem : setout) {
Mark Slee95771002006-06-07 06:53:25 +0000213 if (first) {
214 first = false;
215 } else {
216 System.out.print(", ");
217 }
Mark Slee78f58e22006-09-02 04:17:07 +0000218 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000219 }
220 System.out.print("})");
Mark Slee22360b22008-02-09 00:18:32 +0000221 Set<Integer> setin = testClient.testSet(setout);
Mark Slee95771002006-06-07 06:53:25 +0000222 System.out.print(" = {");
223 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000224 for (int elem : setin) {
Mark Slee95771002006-06-07 06:53:25 +0000225 if (first) {
226 first = false;
227 } else {
228 System.out.print(", ");
229 }
Mark Slee78f58e22006-09-02 04:17:07 +0000230 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000231 }
232 System.out.print("}\n");
233
234 /**
235 * LIST TEST
236 */
Mark Slee22360b22008-02-09 00:18:32 +0000237 List<Integer> listout = new ArrayList<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000238 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000239 listout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000240 }
241 System.out.print("testList({");
242 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000243 for (int elem : listout) {
Mark Slee95771002006-06-07 06:53:25 +0000244 if (first) {
245 first = false;
246 } else {
247 System.out.print(", ");
248 }
Mark Slee78f58e22006-09-02 04:17:07 +0000249 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000250 }
251 System.out.print("})");
Mark Slee22360b22008-02-09 00:18:32 +0000252 List<Integer> listin = testClient.testList(listout);
Mark Slee95771002006-06-07 06:53:25 +0000253 System.out.print(" = {");
254 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000255 for (int elem : listin) {
Mark Slee95771002006-06-07 06:53:25 +0000256 if (first) {
257 first = false;
258 } else {
259 System.out.print(", ");
260 }
Mark Slee78f58e22006-09-02 04:17:07 +0000261 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000262 }
263 System.out.print("}\n");
264
265 /**
266 * ENUM TEST
267 */
268 System.out.print("testEnum(ONE)");
Mark Slee78f58e22006-09-02 04:17:07 +0000269 int ret = testClient.testEnum(Numberz.ONE);
270 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000271
272 System.out.print("testEnum(TWO)");
273 ret = testClient.testEnum(Numberz.TWO);
Mark Slee78f58e22006-09-02 04:17:07 +0000274 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000275
276 System.out.print("testEnum(THREE)");
277 ret = testClient.testEnum(Numberz.THREE);
Mark Slee78f58e22006-09-02 04:17:07 +0000278 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000279
280 System.out.print("testEnum(FIVE)");
281 ret = testClient.testEnum(Numberz.FIVE);
Mark Slee78f58e22006-09-02 04:17:07 +0000282 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000283
284 System.out.print("testEnum(EIGHT)");
285 ret = testClient.testEnum(Numberz.EIGHT);
Mark Slee78f58e22006-09-02 04:17:07 +0000286 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000287
288 /**
289 * TYPEDEF TEST
290 */
291 System.out.print("testTypedef(309858235082523)");
Mark Slee78f58e22006-09-02 04:17:07 +0000292 long uid = testClient.testTypedef(309858235082523L);
293 System.out.print(" = " + uid + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000294
295 /**
296 * NESTED MAP TEST
297 */
298 System.out.print("testMapMap(1)");
Mark Slee22360b22008-02-09 00:18:32 +0000299 Map<Integer,Map<Integer,Integer>> mm =
Mark Slee78f58e22006-09-02 04:17:07 +0000300 testClient.testMapMap(1);
Mark Slee95771002006-06-07 06:53:25 +0000301 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000302 for (int key : mm.keySet()) {
303 System.out.print(key + " => {");
Mark Slee22360b22008-02-09 00:18:32 +0000304 Map<Integer,Integer> m2 = mm.get(key);
Mark Slee78f58e22006-09-02 04:17:07 +0000305 for (int k2 : m2.keySet()) {
306 System.out.print(k2 + " => " + m2.get(k2) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000307 }
308 System.out.print("}, ");
309 }
310 System.out.print("}\n");
311
312 /**
313 * INSANITY TEST
314 */
Mark Slee844ac122007-11-27 08:38:52 +0000315 insane = new Insanity();
Mark Sleee129a2d2007-02-21 05:17:48 +0000316 insane.userMap = new HashMap<Integer, Long>();
Mark Slee78f58e22006-09-02 04:17:07 +0000317 insane.userMap.put(Numberz.FIVE, (long)5000);
Mark Slee95771002006-06-07 06:53:25 +0000318 Xtruct truck = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000319 truck.string_thing = "Truck";
320 truck.byte_thing = (byte)8;
321 truck.i32_thing = 8;
322 truck.i64_thing = 8;
Mark Sleee129a2d2007-02-21 05:17:48 +0000323 insane.xtructs = new ArrayList<Xtruct>();
Mark Slee95771002006-06-07 06:53:25 +0000324 insane.xtructs.add(truck);
325 System.out.print("testInsanity()");
Mark Slee22360b22008-02-09 00:18:32 +0000326 Map<Long,Map<Integer,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000327 testClient.testInsanity(insane);
328 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000329 for (long key : whoa.keySet()) {
Mark Slee22360b22008-02-09 00:18:32 +0000330 Map<Integer,Insanity> val = whoa.get(key);
Mark Slee78f58e22006-09-02 04:17:07 +0000331 System.out.print(key + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000332
Mark Slee78f58e22006-09-02 04:17:07 +0000333 for (int k2 : val.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000334 Insanity v2 = val.get(k2);
Mark Slee78f58e22006-09-02 04:17:07 +0000335 System.out.print(k2 + " => {");
Mark Slee22360b22008-02-09 00:18:32 +0000336 Map<Integer, Long> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000337 System.out.print("{");
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000338 if (userMap != null) {
339 for (int k3 : userMap.keySet()) {
340 System.out.print(k3 + " => " + userMap.get(k3) + ", ");
341 }
Mark Slee95771002006-06-07 06:53:25 +0000342 }
343 System.out.print("}, ");
344
Mark Slee22360b22008-02-09 00:18:32 +0000345 List<Xtruct> xtructs = v2.xtructs;
Mark Slee95771002006-06-07 06:53:25 +0000346 System.out.print("{");
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000347 if (xtructs != null) {
348 for (Xtruct x : xtructs) {
349 System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
350 }
Mark Slee95771002006-06-07 06:53:25 +0000351 }
352 System.out.print("}");
353
354 System.out.print("}, ");
355 }
356 System.out.print("}, ");
357 }
358 System.out.print("}\n");
359
David Reiss2ab6fe82008-02-18 02:11:44 +0000360 // Test async
361 System.out.print("testAsync(3)...");
362 long startAsync = System.nanoTime();
363 testClient.testAsync(3);
364 long asyncElapsedMillis = (System.nanoTime() - startAsync) / 1000000;
365 if (asyncElapsedMillis > 200) {
366 throw new Exception("Async test failed: took " +
367 Long.toString(asyncElapsedMillis) +
368 "ms");
369 } else {
370 System.out.println("Success - took " +
371 Long.toString(asyncElapsedMillis) +
372 "ms");
373 }
374
375
Mark Sleed788b2e2006-09-07 01:26:35 +0000376 long stop = System.nanoTime();
377 long tot = stop-start;
378
379 System.out.println("Total time: " + tot/1000 + "us");
380
381 if (timeMin == 0 || tot < timeMin) {
382 timeMin = tot;
383 }
384 if (tot > timeMax) {
385 timeMax = tot;
386 }
387 timeTot += tot;
Mark Slee95771002006-06-07 06:53:25 +0000388
Mark Sleeb9acf982006-10-10 01:57:32 +0000389 transport.close();
Mark Slee95771002006-06-07 06:53:25 +0000390 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000391
392 long timeAvg = timeTot / numTests;
Mark Slee844ac122007-11-27 08:38:52 +0000393
Mark Sleed788b2e2006-09-07 01:26:35 +0000394 System.out.println("Min time: " + timeMin/1000 + "us");
395 System.out.println("Max time: " + timeMax/1000 + "us");
396 System.out.println("Avg time: " + timeAvg/1000 + "us");
Mark Slee844ac122007-11-27 08:38:52 +0000397
David Reiss4f15f592008-02-23 22:07:43 +0000398 String json = (new TSerializer(new TSimpleJSONProtocol.Factory())).toString(insane);
Mark Slee844ac122007-11-27 08:38:52 +0000399
400 System.out.println("\nFor good meausre here is some JSON:\n" + json);
401
Mark Slee95771002006-06-07 06:53:25 +0000402 } catch (Exception x) {
403 x.printStackTrace();
Mark Slee844ac122007-11-27 08:38:52 +0000404 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000405
Mark Slee95771002006-06-07 06:53:25 +0000406 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000407
Mark Slee95771002006-06-07 06:53:25 +0000408}