blob: 778bb160b601441d20a722ce2440d31cef14056e [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 Sleeb9acf982006-10-10 01:57:32 +00007import com.facebook.thrift.transport.TTransport;
Mark Slee95771002006-06-07 06:53:25 +00008import com.facebook.thrift.transport.TSocket;
Mark Sleeb9acf982006-10-10 01:57:32 +00009import com.facebook.thrift.transport.THttpClient;
Mark Sleea3302652006-10-25 19:03:32 +000010import com.facebook.thrift.transport.TFramedTransport;
Mark Slee95771002006-06-07 06:53:25 +000011import com.facebook.thrift.transport.TTransportException;
12import com.facebook.thrift.protocol.TBinaryProtocol;
Mark Slee95771002006-06-07 06:53:25 +000013
Mark Sleea7747c22007-01-29 17:35:54 +000014import java.util.AbstractMap;
Mark Slee95771002006-06-07 06:53:25 +000015import java.util.HashMap;
16import java.util.HashSet;
17import java.util.ArrayList;
18
19/**
20 * Test Java client for thrift. Essentially just a copy of the C++ version,
21 * this makes a variety of requests to enable testing for both performance and
22 * correctness of the output.
23 *
24 * @author Mark Slee <mcslee@facebook.com>
25 */
26public class TestClient {
27 public static void main(String [] args) {
28 try {
29 String host = "localhost";
30 int port = 9090;
Mark Sleeb9acf982006-10-10 01:57:32 +000031 String url = null;
Mark Slee95771002006-06-07 06:53:25 +000032 int numTests = 1;
Mark Sleea3302652006-10-25 19:03:32 +000033 boolean framed = false;
34 boolean framedInput = true;
35 boolean framedOutput = true;
Mark Sleeb9acf982006-10-10 01:57:32 +000036
37 try {
38 for (int i = 0; i < args.length; ++i) {
39 if (args[i].equals("-h")) {
Mark Sleea3302652006-10-25 19:03:32 +000040 String[] hostport = (args[++i]).split(":");
Mark Sleeb9acf982006-10-10 01:57:32 +000041 host = hostport[0];
42 port = Integer.valueOf(hostport[1]);
Mark Sleea3302652006-10-25 19:03:32 +000043 } else if (args[i].equals("-f") || args[i].equals("-framed")) {
44 framed = true;
45 } else if (args[i].equals("-fo")) {
46 framed = true;
47 framedInput = false;
48 } else if (args[i].equals("-u")) {
Mark Sleeb9acf982006-10-10 01:57:32 +000049 url = args[++i];
Mark Sleea3302652006-10-25 19:03:32 +000050 } else if (args[i].equals("-n")) {
Mark Sleeb9acf982006-10-10 01:57:32 +000051 numTests = Integer.valueOf(args[++i]);
52 }
53 }
54 } catch (Exception x) {
55 x.printStackTrace();
Mark Slee95771002006-06-07 06:53:25 +000056 }
57
Mark Sleeb9acf982006-10-10 01:57:32 +000058 TTransport transport;
59
60 if (url != null) {
61 transport = new THttpClient(url);
62 } else {
Mark Sleea3302652006-10-25 19:03:32 +000063 TSocket socket = new TSocket(host, port);
64 socket.setTimeout(1000);
65 transport = socket;
66 if (framed) {
67 transport = new TFramedTransport(transport,
68 framedInput,
69 framedOutput);
70 }
Mark Sleeb9acf982006-10-10 01:57:32 +000071 }
72
Mark Slee95771002006-06-07 06:53:25 +000073 TBinaryProtocol binaryProtocol =
Mark Slee7dafc942007-08-09 03:40:15 +000074 new TBinaryProtocol(transport);
Mark Slee78f58e22006-09-02 04:17:07 +000075 ThriftTest.Client testClient =
Mark Slee1dd819c2006-10-26 04:56:18 +000076 new ThriftTest.Client(binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +000077
Mark Sleed788b2e2006-09-07 01:26:35 +000078 long timeMin = 0;
79 long timeMax = 0;
80 long timeTot = 0;
81
Mark Slee95771002006-06-07 06:53:25 +000082 for (int test = 0; test < numTests; ++test) {
83
84 /**
85 * CONNECT TEST
86 */
Mark Sleed788b2e2006-09-07 01:26:35 +000087 System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
Mark Slee95771002006-06-07 06:53:25 +000088 try {
Mark Sleeb9acf982006-10-10 01:57:32 +000089 transport.open();
Mark Slee95771002006-06-07 06:53:25 +000090 } catch (TTransportException ttx) {
91 System.out.println("Connect failed: " + ttx.getMessage());
92 continue;
93 }
94
Mark Sleed788b2e2006-09-07 01:26:35 +000095 long start = System.nanoTime();
Mark Slee95771002006-06-07 06:53:25 +000096
97 /**
98 * VOID TEST
99 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000100 try {
101 System.out.print("testVoid()");
102 testClient.testVoid();
103 System.out.print(" = void\n");
104 } catch (TApplicationException tax) {
105 tax.printStackTrace();
106 }
Mark Slee95771002006-06-07 06:53:25 +0000107
108 /**
109 * STRING TEST
110 */
111 System.out.print("testString(\"Test\")");
Mark Slee78f58e22006-09-02 04:17:07 +0000112 String s = testClient.testString("Test");
113 System.out.print(" = \"" + s + "\"\n");
114
Mark Slee95771002006-06-07 06:53:25 +0000115 /**
116 * BYTE TEST
117 */
118 System.out.print("testByte(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000119 byte i8 = testClient.testByte((byte)1);
120 System.out.print(" = " + i8 + "\n");
Mark Slee6e536442006-06-30 18:28:50 +0000121
Mark Slee95771002006-06-07 06:53:25 +0000122 /**
123 * I32 TEST
124 */
125 System.out.print("testI32(-1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000126 int i32 = testClient.testI32(-1);
127 System.out.print(" = " + i32 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000128
129 /**
Mark Slee95771002006-06-07 06:53:25 +0000130 * I64 TEST
131 */
132 System.out.print("testI64(-34359738368)");
Mark Slee78f58e22006-09-02 04:17:07 +0000133 long i64 = testClient.testI64(-34359738368L);
134 System.out.print(" = " + i64 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000135
136 /**
Mark Sleec98d0502006-09-06 02:42:25 +0000137 * DOUBLE TEST
138 */
139 System.out.print("testDouble(5.325098235)");
140 double dub = testClient.testDouble(5.325098235);
141 System.out.print(" = " + dub + "\n");
142
143 /**
Mark Slee95771002006-06-07 06:53:25 +0000144 * STRUCT TEST
145 */
Mark Slee6e536442006-06-30 18:28:50 +0000146 System.out.print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee95771002006-06-07 06:53:25 +0000147 Xtruct out = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000148 out.string_thing = "Zero";
149 out.byte_thing = (byte) 1;
150 out.i32_thing = -3;
151 out.i64_thing = -5;
Mark Slee95771002006-06-07 06:53:25 +0000152 Xtruct in = testClient.testStruct(out);
Mark Sleed788b2e2006-09-07 01:26:35 +0000153 System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000154
155 /**
156 * NESTED STRUCT TEST
157 */
Mark Slee6e536442006-06-30 18:28:50 +0000158 System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Slee95771002006-06-07 06:53:25 +0000159 Xtruct2 out2 = new Xtruct2();
Mark Slee78f58e22006-09-02 04:17:07 +0000160 out2.byte_thing = (short)1;
Mark Slee95771002006-06-07 06:53:25 +0000161 out2.struct_thing = out;
Mark Slee78f58e22006-09-02 04:17:07 +0000162 out2.i32_thing = 5;
Mark Slee95771002006-06-07 06:53:25 +0000163 Xtruct2 in2 = testClient.testNest(out2);
164 in = in2.struct_thing;
Mark Sleed788b2e2006-09-07 01:26:35 +0000165 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 +0000166
167 /**
168 * MAP TEST
169 */
Mark Slee78f58e22006-09-02 04:17:07 +0000170 HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000171 for (int i = 0; i < 5; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000172 mapout.put(i, i-10);
Mark Slee95771002006-06-07 06:53:25 +0000173 }
174 System.out.print("testMap({");
175 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000176 for (int key : mapout.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000177 if (first) {
178 first = false;
179 } else {
180 System.out.print(", ");
181 }
Mark Slee78f58e22006-09-02 04:17:07 +0000182 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000183 }
184 System.out.print("})");
Mark Sleea7747c22007-01-29 17:35:54 +0000185 AbstractMap<Integer,Integer> mapin = testClient.testMap(mapout);
Mark Slee95771002006-06-07 06:53:25 +0000186 System.out.print(" = {");
187 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000188 for (int key : mapin.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000189 if (first) {
190 first = false;
191 } else {
192 System.out.print(", ");
193 }
Mark Slee78f58e22006-09-02 04:17:07 +0000194 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000195 }
196 System.out.print("}\n");
197
198 /**
199 * SET TEST
200 */
Mark Slee78f58e22006-09-02 04:17:07 +0000201 HashSet<Integer> setout = new HashSet<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000202 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000203 setout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000204 }
205 System.out.print("testSet({");
206 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000207 for (int elem : setout) {
Mark Slee95771002006-06-07 06:53:25 +0000208 if (first) {
209 first = false;
210 } else {
211 System.out.print(", ");
212 }
Mark Slee78f58e22006-09-02 04:17:07 +0000213 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000214 }
215 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000216 HashSet<Integer> setin = testClient.testSet(setout);
Mark Slee95771002006-06-07 06:53:25 +0000217 System.out.print(" = {");
218 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000219 for (int elem : setin) {
Mark Slee95771002006-06-07 06:53:25 +0000220 if (first) {
221 first = false;
222 } else {
223 System.out.print(", ");
224 }
Mark Slee78f58e22006-09-02 04:17:07 +0000225 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000226 }
227 System.out.print("}\n");
228
229 /**
230 * LIST TEST
231 */
Mark Slee78f58e22006-09-02 04:17:07 +0000232 ArrayList<Integer> listout = new ArrayList<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000233 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000234 listout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000235 }
236 System.out.print("testList({");
237 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000238 for (int elem : listout) {
Mark Slee95771002006-06-07 06:53:25 +0000239 if (first) {
240 first = false;
241 } else {
242 System.out.print(", ");
243 }
Mark Slee78f58e22006-09-02 04:17:07 +0000244 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000245 }
246 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000247 ArrayList<Integer> listin = testClient.testList(listout);
Mark Slee95771002006-06-07 06:53:25 +0000248 System.out.print(" = {");
249 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000250 for (int elem : listin) {
Mark Slee95771002006-06-07 06:53:25 +0000251 if (first) {
252 first = false;
253 } else {
254 System.out.print(", ");
255 }
Mark Slee78f58e22006-09-02 04:17:07 +0000256 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000257 }
258 System.out.print("}\n");
259
260 /**
261 * ENUM TEST
262 */
263 System.out.print("testEnum(ONE)");
Mark Slee78f58e22006-09-02 04:17:07 +0000264 int ret = testClient.testEnum(Numberz.ONE);
265 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000266
267 System.out.print("testEnum(TWO)");
268 ret = testClient.testEnum(Numberz.TWO);
Mark Slee78f58e22006-09-02 04:17:07 +0000269 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000270
271 System.out.print("testEnum(THREE)");
272 ret = testClient.testEnum(Numberz.THREE);
Mark Slee78f58e22006-09-02 04:17:07 +0000273 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000274
275 System.out.print("testEnum(FIVE)");
276 ret = testClient.testEnum(Numberz.FIVE);
Mark Slee78f58e22006-09-02 04:17:07 +0000277 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000278
279 System.out.print("testEnum(EIGHT)");
280 ret = testClient.testEnum(Numberz.EIGHT);
Mark Slee78f58e22006-09-02 04:17:07 +0000281 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000282
283 /**
284 * TYPEDEF TEST
285 */
286 System.out.print("testTypedef(309858235082523)");
Mark Slee78f58e22006-09-02 04:17:07 +0000287 long uid = testClient.testTypedef(309858235082523L);
288 System.out.print(" = " + uid + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000289
290 /**
291 * NESTED MAP TEST
292 */
293 System.out.print("testMapMap(1)");
Mark Sleea7747c22007-01-29 17:35:54 +0000294 AbstractMap<Integer,AbstractMap<Integer,Integer>> mm =
Mark Slee78f58e22006-09-02 04:17:07 +0000295 testClient.testMapMap(1);
Mark Slee95771002006-06-07 06:53:25 +0000296 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000297 for (int key : mm.keySet()) {
298 System.out.print(key + " => {");
Mark Sleea7747c22007-01-29 17:35:54 +0000299 AbstractMap<Integer,Integer> m2 = mm.get(key);
Mark Slee78f58e22006-09-02 04:17:07 +0000300 for (int k2 : m2.keySet()) {
301 System.out.print(k2 + " => " + m2.get(k2) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000302 }
303 System.out.print("}, ");
304 }
305 System.out.print("}\n");
306
307 /**
308 * INSANITY TEST
309 */
310 Insanity insane = new Insanity();
Mark Sleee129a2d2007-02-21 05:17:48 +0000311 insane.userMap = new HashMap<Integer, Long>();
Mark Slee78f58e22006-09-02 04:17:07 +0000312 insane.userMap.put(Numberz.FIVE, (long)5000);
Mark Slee95771002006-06-07 06:53:25 +0000313 Xtruct truck = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000314 truck.string_thing = "Truck";
315 truck.byte_thing = (byte)8;
316 truck.i32_thing = 8;
317 truck.i64_thing = 8;
Mark Sleee129a2d2007-02-21 05:17:48 +0000318 insane.xtructs = new ArrayList<Xtruct>();
Mark Slee95771002006-06-07 06:53:25 +0000319 insane.xtructs.add(truck);
320 System.out.print("testInsanity()");
Mark Sleea7747c22007-01-29 17:35:54 +0000321 AbstractMap<Long,AbstractMap<Integer,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000322 testClient.testInsanity(insane);
323 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000324 for (long key : whoa.keySet()) {
Mark Sleea7747c22007-01-29 17:35:54 +0000325 AbstractMap<Integer,Insanity> val = whoa.get(key);
Mark Slee78f58e22006-09-02 04:17:07 +0000326 System.out.print(key + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000327
Mark Slee78f58e22006-09-02 04:17:07 +0000328 for (int k2 : val.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000329 Insanity v2 = val.get(k2);
Mark Slee78f58e22006-09-02 04:17:07 +0000330 System.out.print(k2 + " => {");
Mark Sleea7747c22007-01-29 17:35:54 +0000331 AbstractMap<Integer, Long> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000332 System.out.print("{");
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000333 if (userMap != null) {
334 for (int k3 : userMap.keySet()) {
335 System.out.print(k3 + " => " + userMap.get(k3) + ", ");
336 }
Mark Slee95771002006-06-07 06:53:25 +0000337 }
338 System.out.print("}, ");
339
340 ArrayList<Xtruct> xtructs = v2.xtructs;
341 System.out.print("{");
Mark Sleefa6ac9f2007-05-04 18:49:56 +0000342 if (xtructs != null) {
343 for (Xtruct x : xtructs) {
344 System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
345 }
Mark Slee95771002006-06-07 06:53:25 +0000346 }
347 System.out.print("}");
348
349 System.out.print("}, ");
350 }
351 System.out.print("}, ");
352 }
353 System.out.print("}\n");
354
Mark Sleed788b2e2006-09-07 01:26:35 +0000355 long stop = System.nanoTime();
356 long tot = stop-start;
357
358 System.out.println("Total time: " + tot/1000 + "us");
359
360 if (timeMin == 0 || tot < timeMin) {
361 timeMin = tot;
362 }
363 if (tot > timeMax) {
364 timeMax = tot;
365 }
366 timeTot += tot;
Mark Slee95771002006-06-07 06:53:25 +0000367
Mark Sleeb9acf982006-10-10 01:57:32 +0000368 transport.close();
Mark Slee95771002006-06-07 06:53:25 +0000369 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000370
371 long timeAvg = timeTot / numTests;
Mark Slee95771002006-06-07 06:53:25 +0000372
Mark Sleed788b2e2006-09-07 01:26:35 +0000373 System.out.println("Min time: " + timeMin/1000 + "us");
374 System.out.println("Max time: " + timeMax/1000 + "us");
375 System.out.println("Avg time: " + timeAvg/1000 + "us");
376
Mark Slee95771002006-06-07 06:53:25 +0000377 } catch (Exception x) {
378 x.printStackTrace();
Mark Sleed788b2e2006-09-07 01:26:35 +0000379 }
380
Mark Slee95771002006-06-07 06:53:25 +0000381 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000382
Mark Slee95771002006-06-07 06:53:25 +0000383}