blob: 546265c0b2b65e44b56dcc236792c3eacbc4e051 [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 Sleeb9acf982006-10-10 01:57:32 +00006import com.facebook.thrift.transport.TTransport;
Mark Slee95771002006-06-07 06:53:25 +00007import com.facebook.thrift.transport.TSocket;
Mark Sleeb9acf982006-10-10 01:57:32 +00008import com.facebook.thrift.transport.THttpClient;
Mark Sleea3302652006-10-25 19:03:32 +00009import com.facebook.thrift.transport.TFramedTransport;
Mark Slee95771002006-06-07 06:53:25 +000010import com.facebook.thrift.transport.TTransportException;
11import com.facebook.thrift.protocol.TBinaryProtocol;
Mark Slee95771002006-06-07 06:53:25 +000012
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.ArrayList;
16
17/**
18 * Test Java client for thrift. Essentially just a copy of the C++ version,
19 * this makes a variety of requests to enable testing for both performance and
20 * correctness of the output.
21 *
22 * @author Mark Slee <mcslee@facebook.com>
23 */
24public class TestClient {
25 public static void main(String [] args) {
26 try {
27 String host = "localhost";
28 int port = 9090;
Mark Sleeb9acf982006-10-10 01:57:32 +000029 String url = null;
Mark Slee95771002006-06-07 06:53:25 +000030 int numTests = 1;
Mark Sleea3302652006-10-25 19:03:32 +000031 boolean framed = false;
32 boolean framedInput = true;
33 boolean framedOutput = true;
Mark Sleeb9acf982006-10-10 01:57:32 +000034
35 try {
36 for (int i = 0; i < args.length; ++i) {
37 if (args[i].equals("-h")) {
Mark Sleea3302652006-10-25 19:03:32 +000038 String[] hostport = (args[++i]).split(":");
Mark Sleeb9acf982006-10-10 01:57:32 +000039 host = hostport[0];
40 port = Integer.valueOf(hostport[1]);
Mark Sleea3302652006-10-25 19:03:32 +000041 } else if (args[i].equals("-f") || args[i].equals("-framed")) {
42 framed = true;
43 } else if (args[i].equals("-fo")) {
44 framed = true;
45 framedInput = false;
46 } else if (args[i].equals("-u")) {
Mark Sleeb9acf982006-10-10 01:57:32 +000047 url = args[++i];
Mark Sleea3302652006-10-25 19:03:32 +000048 } else if (args[i].equals("-n")) {
Mark Sleeb9acf982006-10-10 01:57:32 +000049 numTests = Integer.valueOf(args[++i]);
50 }
51 }
52 } catch (Exception x) {
53 x.printStackTrace();
Mark Slee95771002006-06-07 06:53:25 +000054 }
55
Mark Sleeb9acf982006-10-10 01:57:32 +000056 TTransport transport;
57
58 if (url != null) {
59 transport = new THttpClient(url);
60 } else {
Mark Sleea3302652006-10-25 19:03:32 +000061 TSocket socket = new TSocket(host, port);
62 socket.setTimeout(1000);
63 transport = socket;
64 if (framed) {
65 transport = new TFramedTransport(transport,
66 framedInput,
67 framedOutput);
68 }
Mark Sleeb9acf982006-10-10 01:57:32 +000069 }
70
Mark Slee95771002006-06-07 06:53:25 +000071 TBinaryProtocol binaryProtocol =
72 new TBinaryProtocol();
Mark Slee78f58e22006-09-02 04:17:07 +000073 ThriftTest.Client testClient =
Mark Sleeb9acf982006-10-10 01:57:32 +000074 new ThriftTest.Client(transport, binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +000075
Mark Sleed788b2e2006-09-07 01:26:35 +000076 long timeMin = 0;
77 long timeMax = 0;
78 long timeTot = 0;
79
Mark Slee95771002006-06-07 06:53:25 +000080 for (int test = 0; test < numTests; ++test) {
81
82 /**
83 * CONNECT TEST
84 */
Mark Sleed788b2e2006-09-07 01:26:35 +000085 System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
Mark Slee95771002006-06-07 06:53:25 +000086 try {
Mark Sleeb9acf982006-10-10 01:57:32 +000087 transport.open();
Mark Slee95771002006-06-07 06:53:25 +000088 } catch (TTransportException ttx) {
89 System.out.println("Connect failed: " + ttx.getMessage());
90 continue;
91 }
92
Mark Sleed788b2e2006-09-07 01:26:35 +000093 long start = System.nanoTime();
Mark Slee95771002006-06-07 06:53:25 +000094
95 /**
96 * VOID TEST
97 */
98 System.out.print("testVoid()");
99 testClient.testVoid();
100 System.out.print(" = void\n");
101
102 /**
103 * STRING TEST
104 */
105 System.out.print("testString(\"Test\")");
Mark Slee78f58e22006-09-02 04:17:07 +0000106 String s = testClient.testString("Test");
107 System.out.print(" = \"" + s + "\"\n");
108
Mark Slee95771002006-06-07 06:53:25 +0000109 /**
110 * BYTE TEST
111 */
112 System.out.print("testByte(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000113 byte i8 = testClient.testByte((byte)1);
114 System.out.print(" = " + i8 + "\n");
Mark Slee6e536442006-06-30 18:28:50 +0000115
Mark Slee95771002006-06-07 06:53:25 +0000116 /**
117 * I32 TEST
118 */
119 System.out.print("testI32(-1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000120 int i32 = testClient.testI32(-1);
121 System.out.print(" = " + i32 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000122
123 /**
Mark Slee95771002006-06-07 06:53:25 +0000124 * I64 TEST
125 */
126 System.out.print("testI64(-34359738368)");
Mark Slee78f58e22006-09-02 04:17:07 +0000127 long i64 = testClient.testI64(-34359738368L);
128 System.out.print(" = " + i64 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000129
130 /**
Mark Sleec98d0502006-09-06 02:42:25 +0000131 * DOUBLE TEST
132 */
133 System.out.print("testDouble(5.325098235)");
134 double dub = testClient.testDouble(5.325098235);
135 System.out.print(" = " + dub + "\n");
136
137 /**
Mark Slee95771002006-06-07 06:53:25 +0000138 * STRUCT TEST
139 */
Mark Slee6e536442006-06-30 18:28:50 +0000140 System.out.print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee95771002006-06-07 06:53:25 +0000141 Xtruct out = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000142 out.string_thing = "Zero";
143 out.byte_thing = (byte) 1;
144 out.i32_thing = -3;
145 out.i64_thing = -5;
Mark Slee95771002006-06-07 06:53:25 +0000146 Xtruct in = testClient.testStruct(out);
Mark Sleed788b2e2006-09-07 01:26:35 +0000147 System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000148
149 /**
150 * NESTED STRUCT TEST
151 */
Mark Slee6e536442006-06-30 18:28:50 +0000152 System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Slee95771002006-06-07 06:53:25 +0000153 Xtruct2 out2 = new Xtruct2();
Mark Slee78f58e22006-09-02 04:17:07 +0000154 out2.byte_thing = (short)1;
Mark Slee95771002006-06-07 06:53:25 +0000155 out2.struct_thing = out;
Mark Slee78f58e22006-09-02 04:17:07 +0000156 out2.i32_thing = 5;
Mark Slee95771002006-06-07 06:53:25 +0000157 Xtruct2 in2 = testClient.testNest(out2);
158 in = in2.struct_thing;
Mark Sleed788b2e2006-09-07 01:26:35 +0000159 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 +0000160
161 /**
162 * MAP TEST
163 */
Mark Slee78f58e22006-09-02 04:17:07 +0000164 HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000165 for (int i = 0; i < 5; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000166 mapout.put(i, i-10);
Mark Slee95771002006-06-07 06:53:25 +0000167 }
168 System.out.print("testMap({");
169 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000170 for (int key : mapout.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000171 if (first) {
172 first = false;
173 } else {
174 System.out.print(", ");
175 }
Mark Slee78f58e22006-09-02 04:17:07 +0000176 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000177 }
178 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000179 HashMap<Integer,Integer> mapin = testClient.testMap(mapout);
Mark Slee95771002006-06-07 06:53:25 +0000180 System.out.print(" = {");
181 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000182 for (int key : mapin.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000183 if (first) {
184 first = false;
185 } else {
186 System.out.print(", ");
187 }
Mark Slee78f58e22006-09-02 04:17:07 +0000188 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000189 }
190 System.out.print("}\n");
191
192 /**
193 * SET TEST
194 */
Mark Slee78f58e22006-09-02 04:17:07 +0000195 HashSet<Integer> setout = new HashSet<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000196 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000197 setout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000198 }
199 System.out.print("testSet({");
200 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000201 for (int elem : setout) {
Mark Slee95771002006-06-07 06:53:25 +0000202 if (first) {
203 first = false;
204 } else {
205 System.out.print(", ");
206 }
Mark Slee78f58e22006-09-02 04:17:07 +0000207 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000208 }
209 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000210 HashSet<Integer> setin = testClient.testSet(setout);
Mark Slee95771002006-06-07 06:53:25 +0000211 System.out.print(" = {");
212 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000213 for (int elem : setin) {
Mark Slee95771002006-06-07 06:53:25 +0000214 if (first) {
215 first = false;
216 } else {
217 System.out.print(", ");
218 }
Mark Slee78f58e22006-09-02 04:17:07 +0000219 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000220 }
221 System.out.print("}\n");
222
223 /**
224 * LIST TEST
225 */
Mark Slee78f58e22006-09-02 04:17:07 +0000226 ArrayList<Integer> listout = new ArrayList<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000227 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000228 listout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000229 }
230 System.out.print("testList({");
231 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000232 for (int elem : listout) {
Mark Slee95771002006-06-07 06:53:25 +0000233 if (first) {
234 first = false;
235 } else {
236 System.out.print(", ");
237 }
Mark Slee78f58e22006-09-02 04:17:07 +0000238 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000239 }
240 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000241 ArrayList<Integer> listin = testClient.testList(listout);
Mark Slee95771002006-06-07 06:53:25 +0000242 System.out.print(" = {");
243 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000244 for (int elem : listin) {
Mark Slee95771002006-06-07 06:53:25 +0000245 if (first) {
246 first = false;
247 } else {
248 System.out.print(", ");
249 }
Mark Slee78f58e22006-09-02 04:17:07 +0000250 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000251 }
252 System.out.print("}\n");
253
254 /**
255 * ENUM TEST
256 */
257 System.out.print("testEnum(ONE)");
Mark Slee78f58e22006-09-02 04:17:07 +0000258 int ret = testClient.testEnum(Numberz.ONE);
259 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000260
261 System.out.print("testEnum(TWO)");
262 ret = testClient.testEnum(Numberz.TWO);
Mark Slee78f58e22006-09-02 04:17:07 +0000263 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000264
265 System.out.print("testEnum(THREE)");
266 ret = testClient.testEnum(Numberz.THREE);
Mark Slee78f58e22006-09-02 04:17:07 +0000267 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000268
269 System.out.print("testEnum(FIVE)");
270 ret = testClient.testEnum(Numberz.FIVE);
Mark Slee78f58e22006-09-02 04:17:07 +0000271 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000272
273 System.out.print("testEnum(EIGHT)");
274 ret = testClient.testEnum(Numberz.EIGHT);
Mark Slee78f58e22006-09-02 04:17:07 +0000275 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000276
277 /**
278 * TYPEDEF TEST
279 */
280 System.out.print("testTypedef(309858235082523)");
Mark Slee78f58e22006-09-02 04:17:07 +0000281 long uid = testClient.testTypedef(309858235082523L);
282 System.out.print(" = " + uid + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000283
284 /**
285 * NESTED MAP TEST
286 */
287 System.out.print("testMapMap(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000288 HashMap<Integer,HashMap<Integer,Integer>> mm =
289 testClient.testMapMap(1);
Mark Slee95771002006-06-07 06:53:25 +0000290 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000291 for (int key : mm.keySet()) {
292 System.out.print(key + " => {");
293 HashMap<Integer,Integer> m2 = mm.get(key);
294 for (int k2 : m2.keySet()) {
295 System.out.print(k2 + " => " + m2.get(k2) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000296 }
297 System.out.print("}, ");
298 }
299 System.out.print("}\n");
300
301 /**
302 * INSANITY TEST
303 */
304 Insanity insane = new Insanity();
Mark Slee78f58e22006-09-02 04:17:07 +0000305 insane.userMap.put(Numberz.FIVE, (long)5000);
Mark Slee95771002006-06-07 06:53:25 +0000306 Xtruct truck = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000307 truck.string_thing = "Truck";
308 truck.byte_thing = (byte)8;
309 truck.i32_thing = 8;
310 truck.i64_thing = 8;
Mark Slee95771002006-06-07 06:53:25 +0000311 insane.xtructs.add(truck);
312 System.out.print("testInsanity()");
Mark Slee78f58e22006-09-02 04:17:07 +0000313 HashMap<Long,HashMap<Integer,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000314 testClient.testInsanity(insane);
315 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000316 for (long key : whoa.keySet()) {
317 HashMap<Integer,Insanity> val = whoa.get(key);
318 System.out.print(key + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000319
Mark Slee78f58e22006-09-02 04:17:07 +0000320 for (int k2 : val.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000321 Insanity v2 = val.get(k2);
Mark Slee78f58e22006-09-02 04:17:07 +0000322 System.out.print(k2 + " => {");
323 HashMap<Integer, Long> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000324 System.out.print("{");
Mark Slee78f58e22006-09-02 04:17:07 +0000325 for (int k3 : userMap.keySet()) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000326 System.out.print(k3 + " => " + userMap.get(k3) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000327 }
328 System.out.print("}, ");
329
330 ArrayList<Xtruct> xtructs = v2.xtructs;
331 System.out.print("{");
332 for (Xtruct x : xtructs) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000333 System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
Mark Slee95771002006-06-07 06:53:25 +0000334 }
335 System.out.print("}");
336
337 System.out.print("}, ");
338 }
339 System.out.print("}, ");
340 }
341 System.out.print("}\n");
342
Mark Sleed788b2e2006-09-07 01:26:35 +0000343 long stop = System.nanoTime();
344 long tot = stop-start;
345
346 System.out.println("Total time: " + tot/1000 + "us");
347
348 if (timeMin == 0 || tot < timeMin) {
349 timeMin = tot;
350 }
351 if (tot > timeMax) {
352 timeMax = tot;
353 }
354 timeTot += tot;
Mark Slee95771002006-06-07 06:53:25 +0000355
Mark Sleeb9acf982006-10-10 01:57:32 +0000356 transport.close();
Mark Slee95771002006-06-07 06:53:25 +0000357 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000358
359 long timeAvg = timeTot / numTests;
Mark Slee95771002006-06-07 06:53:25 +0000360
Mark Sleed788b2e2006-09-07 01:26:35 +0000361 System.out.println("Min time: " + timeMin/1000 + "us");
362 System.out.println("Max time: " + timeMax/1000 + "us");
363 System.out.println("Avg time: " + timeAvg/1000 + "us");
364
Mark Slee95771002006-06-07 06:53:25 +0000365 } catch (Exception x) {
366 x.printStackTrace();
Mark Sleed788b2e2006-09-07 01:26:35 +0000367 }
368
Mark Slee95771002006-06-07 06:53:25 +0000369 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000370
Mark Slee95771002006-06-07 06:53:25 +0000371}