blob: bc950364a80abf665657b8fad49701eed68e8723 [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
Mark Sleea7747c22007-01-29 17:35:54 +000013import java.util.AbstractMap;
Mark Slee95771002006-06-07 06:53:25 +000014import java.util.HashMap;
15import java.util.HashSet;
16import java.util.ArrayList;
17
18/**
19 * Test Java client for thrift. Essentially just a copy of the C++ version,
20 * this makes a variety of requests to enable testing for both performance and
21 * correctness of the output.
22 *
23 * @author Mark Slee <mcslee@facebook.com>
24 */
25public class TestClient {
26 public static void main(String [] args) {
27 try {
28 String host = "localhost";
29 int port = 9090;
Mark Sleeb9acf982006-10-10 01:57:32 +000030 String url = null;
Mark Slee95771002006-06-07 06:53:25 +000031 int numTests = 1;
Mark Sleea3302652006-10-25 19:03:32 +000032 boolean framed = false;
33 boolean framedInput = true;
34 boolean framedOutput = true;
Mark Sleeb9acf982006-10-10 01:57:32 +000035
36 try {
37 for (int i = 0; i < args.length; ++i) {
38 if (args[i].equals("-h")) {
Mark Sleea3302652006-10-25 19:03:32 +000039 String[] hostport = (args[++i]).split(":");
Mark Sleeb9acf982006-10-10 01:57:32 +000040 host = hostport[0];
41 port = Integer.valueOf(hostport[1]);
Mark Sleea3302652006-10-25 19:03:32 +000042 } else if (args[i].equals("-f") || args[i].equals("-framed")) {
43 framed = true;
44 } else if (args[i].equals("-fo")) {
45 framed = true;
46 framedInput = false;
47 } else if (args[i].equals("-u")) {
Mark Sleeb9acf982006-10-10 01:57:32 +000048 url = args[++i];
Mark Sleea3302652006-10-25 19:03:32 +000049 } else if (args[i].equals("-n")) {
Mark Sleeb9acf982006-10-10 01:57:32 +000050 numTests = Integer.valueOf(args[++i]);
51 }
52 }
53 } catch (Exception x) {
54 x.printStackTrace();
Mark Slee95771002006-06-07 06:53:25 +000055 }
56
Mark Sleeb9acf982006-10-10 01:57:32 +000057 TTransport transport;
58
59 if (url != null) {
60 transport = new THttpClient(url);
61 } else {
Mark Sleea3302652006-10-25 19:03:32 +000062 TSocket socket = new TSocket(host, port);
63 socket.setTimeout(1000);
64 transport = socket;
65 if (framed) {
66 transport = new TFramedTransport(transport,
67 framedInput,
68 framedOutput);
69 }
Mark Sleeb9acf982006-10-10 01:57:32 +000070 }
71
Mark Slee95771002006-06-07 06:53:25 +000072 TBinaryProtocol binaryProtocol =
Mark Slee1dd819c2006-10-26 04:56:18 +000073 new TBinaryProtocol(transport, transport);
Mark Slee78f58e22006-09-02 04:17:07 +000074 ThriftTest.Client testClient =
Mark Slee1dd819c2006-10-26 04:56:18 +000075 new ThriftTest.Client(binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +000076
Mark Sleed788b2e2006-09-07 01:26:35 +000077 long timeMin = 0;
78 long timeMax = 0;
79 long timeTot = 0;
80
Mark Slee95771002006-06-07 06:53:25 +000081 for (int test = 0; test < numTests; ++test) {
82
83 /**
84 * CONNECT TEST
85 */
Mark Sleed788b2e2006-09-07 01:26:35 +000086 System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
Mark Slee95771002006-06-07 06:53:25 +000087 try {
Mark Sleeb9acf982006-10-10 01:57:32 +000088 transport.open();
Mark Slee95771002006-06-07 06:53:25 +000089 } catch (TTransportException ttx) {
90 System.out.println("Connect failed: " + ttx.getMessage());
91 continue;
92 }
93
Mark Sleed788b2e2006-09-07 01:26:35 +000094 long start = System.nanoTime();
Mark Slee95771002006-06-07 06:53:25 +000095
96 /**
97 * VOID TEST
98 */
99 System.out.print("testVoid()");
100 testClient.testVoid();
101 System.out.print(" = void\n");
102
103 /**
104 * STRING TEST
105 */
106 System.out.print("testString(\"Test\")");
Mark Slee78f58e22006-09-02 04:17:07 +0000107 String s = testClient.testString("Test");
108 System.out.print(" = \"" + s + "\"\n");
109
Mark Slee95771002006-06-07 06:53:25 +0000110 /**
111 * BYTE TEST
112 */
113 System.out.print("testByte(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000114 byte i8 = testClient.testByte((byte)1);
115 System.out.print(" = " + i8 + "\n");
Mark Slee6e536442006-06-30 18:28:50 +0000116
Mark Slee95771002006-06-07 06:53:25 +0000117 /**
118 * I32 TEST
119 */
120 System.out.print("testI32(-1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000121 int i32 = testClient.testI32(-1);
122 System.out.print(" = " + i32 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000123
124 /**
Mark Slee95771002006-06-07 06:53:25 +0000125 * I64 TEST
126 */
127 System.out.print("testI64(-34359738368)");
Mark Slee78f58e22006-09-02 04:17:07 +0000128 long i64 = testClient.testI64(-34359738368L);
129 System.out.print(" = " + i64 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000130
131 /**
Mark Sleec98d0502006-09-06 02:42:25 +0000132 * DOUBLE TEST
133 */
134 System.out.print("testDouble(5.325098235)");
135 double dub = testClient.testDouble(5.325098235);
136 System.out.print(" = " + dub + "\n");
137
138 /**
Mark Slee95771002006-06-07 06:53:25 +0000139 * STRUCT TEST
140 */
Mark Slee6e536442006-06-30 18:28:50 +0000141 System.out.print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee95771002006-06-07 06:53:25 +0000142 Xtruct out = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000143 out.string_thing = "Zero";
144 out.byte_thing = (byte) 1;
145 out.i32_thing = -3;
146 out.i64_thing = -5;
Mark Slee95771002006-06-07 06:53:25 +0000147 Xtruct in = testClient.testStruct(out);
Mark Sleed788b2e2006-09-07 01:26:35 +0000148 System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000149
150 /**
151 * NESTED STRUCT TEST
152 */
Mark Slee6e536442006-06-30 18:28:50 +0000153 System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Slee95771002006-06-07 06:53:25 +0000154 Xtruct2 out2 = new Xtruct2();
Mark Slee78f58e22006-09-02 04:17:07 +0000155 out2.byte_thing = (short)1;
Mark Slee95771002006-06-07 06:53:25 +0000156 out2.struct_thing = out;
Mark Slee78f58e22006-09-02 04:17:07 +0000157 out2.i32_thing = 5;
Mark Slee95771002006-06-07 06:53:25 +0000158 Xtruct2 in2 = testClient.testNest(out2);
159 in = in2.struct_thing;
Mark Sleed788b2e2006-09-07 01:26:35 +0000160 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 +0000161
162 /**
163 * MAP TEST
164 */
Mark Slee78f58e22006-09-02 04:17:07 +0000165 HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000166 for (int i = 0; i < 5; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000167 mapout.put(i, i-10);
Mark Slee95771002006-06-07 06:53:25 +0000168 }
169 System.out.print("testMap({");
170 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000171 for (int key : mapout.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000172 if (first) {
173 first = false;
174 } else {
175 System.out.print(", ");
176 }
Mark Slee78f58e22006-09-02 04:17:07 +0000177 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000178 }
179 System.out.print("})");
Mark Sleea7747c22007-01-29 17:35:54 +0000180 AbstractMap<Integer,Integer> mapin = testClient.testMap(mapout);
Mark Slee95771002006-06-07 06:53:25 +0000181 System.out.print(" = {");
182 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000183 for (int key : mapin.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000184 if (first) {
185 first = false;
186 } else {
187 System.out.print(", ");
188 }
Mark Slee78f58e22006-09-02 04:17:07 +0000189 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000190 }
191 System.out.print("}\n");
192
193 /**
194 * SET TEST
195 */
Mark Slee78f58e22006-09-02 04:17:07 +0000196 HashSet<Integer> setout = new HashSet<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000197 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000198 setout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000199 }
200 System.out.print("testSet({");
201 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000202 for (int elem : setout) {
Mark Slee95771002006-06-07 06:53:25 +0000203 if (first) {
204 first = false;
205 } else {
206 System.out.print(", ");
207 }
Mark Slee78f58e22006-09-02 04:17:07 +0000208 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000209 }
210 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000211 HashSet<Integer> setin = testClient.testSet(setout);
Mark Slee95771002006-06-07 06:53:25 +0000212 System.out.print(" = {");
213 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000214 for (int elem : setin) {
Mark Slee95771002006-06-07 06:53:25 +0000215 if (first) {
216 first = false;
217 } else {
218 System.out.print(", ");
219 }
Mark Slee78f58e22006-09-02 04:17:07 +0000220 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000221 }
222 System.out.print("}\n");
223
224 /**
225 * LIST TEST
226 */
Mark Slee78f58e22006-09-02 04:17:07 +0000227 ArrayList<Integer> listout = new ArrayList<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000228 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000229 listout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000230 }
231 System.out.print("testList({");
232 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000233 for (int elem : listout) {
Mark Slee95771002006-06-07 06:53:25 +0000234 if (first) {
235 first = false;
236 } else {
237 System.out.print(", ");
238 }
Mark Slee78f58e22006-09-02 04:17:07 +0000239 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000240 }
241 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000242 ArrayList<Integer> listin = testClient.testList(listout);
Mark Slee95771002006-06-07 06:53:25 +0000243 System.out.print(" = {");
244 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000245 for (int elem : listin) {
Mark Slee95771002006-06-07 06:53:25 +0000246 if (first) {
247 first = false;
248 } else {
249 System.out.print(", ");
250 }
Mark Slee78f58e22006-09-02 04:17:07 +0000251 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000252 }
253 System.out.print("}\n");
254
255 /**
256 * ENUM TEST
257 */
258 System.out.print("testEnum(ONE)");
Mark Slee78f58e22006-09-02 04:17:07 +0000259 int ret = testClient.testEnum(Numberz.ONE);
260 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000261
262 System.out.print("testEnum(TWO)");
263 ret = testClient.testEnum(Numberz.TWO);
Mark Slee78f58e22006-09-02 04:17:07 +0000264 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000265
266 System.out.print("testEnum(THREE)");
267 ret = testClient.testEnum(Numberz.THREE);
Mark Slee78f58e22006-09-02 04:17:07 +0000268 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000269
270 System.out.print("testEnum(FIVE)");
271 ret = testClient.testEnum(Numberz.FIVE);
Mark Slee78f58e22006-09-02 04:17:07 +0000272 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000273
274 System.out.print("testEnum(EIGHT)");
275 ret = testClient.testEnum(Numberz.EIGHT);
Mark Slee78f58e22006-09-02 04:17:07 +0000276 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000277
278 /**
279 * TYPEDEF TEST
280 */
281 System.out.print("testTypedef(309858235082523)");
Mark Slee78f58e22006-09-02 04:17:07 +0000282 long uid = testClient.testTypedef(309858235082523L);
283 System.out.print(" = " + uid + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000284
285 /**
286 * NESTED MAP TEST
287 */
288 System.out.print("testMapMap(1)");
Mark Sleea7747c22007-01-29 17:35:54 +0000289 AbstractMap<Integer,AbstractMap<Integer,Integer>> mm =
Mark Slee78f58e22006-09-02 04:17:07 +0000290 testClient.testMapMap(1);
Mark Slee95771002006-06-07 06:53:25 +0000291 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000292 for (int key : mm.keySet()) {
293 System.out.print(key + " => {");
Mark Sleea7747c22007-01-29 17:35:54 +0000294 AbstractMap<Integer,Integer> m2 = mm.get(key);
Mark Slee78f58e22006-09-02 04:17:07 +0000295 for (int k2 : m2.keySet()) {
296 System.out.print(k2 + " => " + m2.get(k2) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000297 }
298 System.out.print("}, ");
299 }
300 System.out.print("}\n");
301
302 /**
303 * INSANITY TEST
304 */
305 Insanity insane = new Insanity();
Mark Slee78f58e22006-09-02 04:17:07 +0000306 insane.userMap.put(Numberz.FIVE, (long)5000);
Mark Slee95771002006-06-07 06:53:25 +0000307 Xtruct truck = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000308 truck.string_thing = "Truck";
309 truck.byte_thing = (byte)8;
310 truck.i32_thing = 8;
311 truck.i64_thing = 8;
Mark Slee95771002006-06-07 06:53:25 +0000312 insane.xtructs.add(truck);
313 System.out.print("testInsanity()");
Mark Sleea7747c22007-01-29 17:35:54 +0000314 AbstractMap<Long,AbstractMap<Integer,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000315 testClient.testInsanity(insane);
316 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000317 for (long key : whoa.keySet()) {
Mark Sleea7747c22007-01-29 17:35:54 +0000318 AbstractMap<Integer,Insanity> val = whoa.get(key);
Mark Slee78f58e22006-09-02 04:17:07 +0000319 System.out.print(key + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000320
Mark Slee78f58e22006-09-02 04:17:07 +0000321 for (int k2 : val.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000322 Insanity v2 = val.get(k2);
Mark Slee78f58e22006-09-02 04:17:07 +0000323 System.out.print(k2 + " => {");
Mark Sleea7747c22007-01-29 17:35:54 +0000324 AbstractMap<Integer, Long> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000325 System.out.print("{");
Mark Slee78f58e22006-09-02 04:17:07 +0000326 for (int k3 : userMap.keySet()) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000327 System.out.print(k3 + " => " + userMap.get(k3) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000328 }
329 System.out.print("}, ");
330
331 ArrayList<Xtruct> xtructs = v2.xtructs;
332 System.out.print("{");
333 for (Xtruct x : xtructs) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000334 System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
Mark Slee95771002006-06-07 06:53:25 +0000335 }
336 System.out.print("}");
337
338 System.out.print("}, ");
339 }
340 System.out.print("}, ");
341 }
342 System.out.print("}\n");
343
Mark Sleed788b2e2006-09-07 01:26:35 +0000344 long stop = System.nanoTime();
345 long tot = stop-start;
346
347 System.out.println("Total time: " + tot/1000 + "us");
348
349 if (timeMin == 0 || tot < timeMin) {
350 timeMin = tot;
351 }
352 if (tot > timeMax) {
353 timeMax = tot;
354 }
355 timeTot += tot;
Mark Slee95771002006-06-07 06:53:25 +0000356
Mark Sleeb9acf982006-10-10 01:57:32 +0000357 transport.close();
Mark Slee95771002006-06-07 06:53:25 +0000358 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000359
360 long timeAvg = timeTot / numTests;
Mark Slee95771002006-06-07 06:53:25 +0000361
Mark Sleed788b2e2006-09-07 01:26:35 +0000362 System.out.println("Min time: " + timeMin/1000 + "us");
363 System.out.println("Max time: " + timeMax/1000 + "us");
364 System.out.println("Avg time: " + timeAvg/1000 + "us");
365
Mark Slee95771002006-06-07 06:53:25 +0000366 } catch (Exception x) {
367 x.printStackTrace();
Mark Sleed788b2e2006-09-07 01:26:35 +0000368 }
369
Mark Slee95771002006-06-07 06:53:25 +0000370 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000371
Mark Slee95771002006-06-07 06:53:25 +0000372}