blob: fda37de80b3f951e3ab167ed38c86baddcf1dd59 [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 Slee95771002006-06-07 06:53:25 +00006import com.facebook.thrift.transport.TSocket;
7import com.facebook.thrift.transport.TTransportException;
8import com.facebook.thrift.protocol.TBinaryProtocol;
Mark Slee95771002006-06-07 06:53:25 +00009
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.ArrayList;
13
14/**
15 * Test Java client for thrift. Essentially just a copy of the C++ version,
16 * this makes a variety of requests to enable testing for both performance and
17 * correctness of the output.
18 *
19 * @author Mark Slee <mcslee@facebook.com>
20 */
21public class TestClient {
22 public static void main(String [] args) {
23 try {
24 String host = "localhost";
25 int port = 9090;
26 int numTests = 1;
27
28 if (args.length > 0) {
29 host = args[0];
30 }
31 if (args.length > 1) {
32 port = Integer.valueOf(args[1]);
33 }
34 if (args.length > 2) {
35 numTests = Integer.valueOf(args[2]);
36 }
37
38 TSocket tSocket =
39 new TSocket(host, port);
40 TBinaryProtocol binaryProtocol =
41 new TBinaryProtocol();
Mark Slee78f58e22006-09-02 04:17:07 +000042 ThriftTest.Client testClient =
43 new ThriftTest.Client(tSocket, binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +000044
Mark Sleed788b2e2006-09-07 01:26:35 +000045 long timeMin = 0;
46 long timeMax = 0;
47 long timeTot = 0;
48
Mark Slee95771002006-06-07 06:53:25 +000049 for (int test = 0; test < numTests; ++test) {
50
51 /**
52 * CONNECT TEST
53 */
Mark Sleed788b2e2006-09-07 01:26:35 +000054 System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
Mark Slee95771002006-06-07 06:53:25 +000055 try {
56 tSocket.open();
57 } catch (TTransportException ttx) {
58 System.out.println("Connect failed: " + ttx.getMessage());
59 continue;
60 }
61
Mark Sleed788b2e2006-09-07 01:26:35 +000062 long start = System.nanoTime();
Mark Slee95771002006-06-07 06:53:25 +000063
64 /**
65 * VOID TEST
66 */
67 System.out.print("testVoid()");
68 testClient.testVoid();
69 System.out.print(" = void\n");
70
71 /**
72 * STRING TEST
73 */
74 System.out.print("testString(\"Test\")");
Mark Slee78f58e22006-09-02 04:17:07 +000075 String s = testClient.testString("Test");
76 System.out.print(" = \"" + s + "\"\n");
77
Mark Slee95771002006-06-07 06:53:25 +000078 /**
79 * BYTE TEST
80 */
81 System.out.print("testByte(1)");
Mark Slee78f58e22006-09-02 04:17:07 +000082 byte i8 = testClient.testByte((byte)1);
83 System.out.print(" = " + i8 + "\n");
Mark Slee6e536442006-06-30 18:28:50 +000084
Mark Slee95771002006-06-07 06:53:25 +000085 /**
86 * I32 TEST
87 */
88 System.out.print("testI32(-1)");
Mark Slee78f58e22006-09-02 04:17:07 +000089 int i32 = testClient.testI32(-1);
90 System.out.print(" = " + i32 + "\n");
Mark Slee95771002006-06-07 06:53:25 +000091
92 /**
Mark Slee95771002006-06-07 06:53:25 +000093 * I64 TEST
94 */
95 System.out.print("testI64(-34359738368)");
Mark Slee78f58e22006-09-02 04:17:07 +000096 long i64 = testClient.testI64(-34359738368L);
97 System.out.print(" = " + i64 + "\n");
Mark Slee95771002006-06-07 06:53:25 +000098
99 /**
Mark Sleec98d0502006-09-06 02:42:25 +0000100 * DOUBLE TEST
101 */
102 System.out.print("testDouble(5.325098235)");
103 double dub = testClient.testDouble(5.325098235);
104 System.out.print(" = " + dub + "\n");
105
106 /**
Mark Slee95771002006-06-07 06:53:25 +0000107 * STRUCT TEST
108 */
Mark Slee6e536442006-06-30 18:28:50 +0000109 System.out.print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee95771002006-06-07 06:53:25 +0000110 Xtruct out = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000111 out.string_thing = "Zero";
112 out.byte_thing = (byte) 1;
113 out.i32_thing = -3;
114 out.i64_thing = -5;
Mark Slee95771002006-06-07 06:53:25 +0000115 Xtruct in = testClient.testStruct(out);
Mark Sleed788b2e2006-09-07 01:26:35 +0000116 System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000117
118 /**
119 * NESTED STRUCT TEST
120 */
Mark Slee6e536442006-06-30 18:28:50 +0000121 System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Slee95771002006-06-07 06:53:25 +0000122 Xtruct2 out2 = new Xtruct2();
Mark Slee78f58e22006-09-02 04:17:07 +0000123 out2.byte_thing = (short)1;
Mark Slee95771002006-06-07 06:53:25 +0000124 out2.struct_thing = out;
Mark Slee78f58e22006-09-02 04:17:07 +0000125 out2.i32_thing = 5;
Mark Slee95771002006-06-07 06:53:25 +0000126 Xtruct2 in2 = testClient.testNest(out2);
127 in = in2.struct_thing;
Mark Sleed788b2e2006-09-07 01:26:35 +0000128 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 +0000129
130 /**
131 * MAP TEST
132 */
Mark Slee78f58e22006-09-02 04:17:07 +0000133 HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000134 for (int i = 0; i < 5; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000135 mapout.put(i, i-10);
Mark Slee95771002006-06-07 06:53:25 +0000136 }
137 System.out.print("testMap({");
138 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000139 for (int key : mapout.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000140 if (first) {
141 first = false;
142 } else {
143 System.out.print(", ");
144 }
Mark Slee78f58e22006-09-02 04:17:07 +0000145 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000146 }
147 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000148 HashMap<Integer,Integer> mapin = testClient.testMap(mapout);
Mark Slee95771002006-06-07 06:53:25 +0000149 System.out.print(" = {");
150 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000151 for (int key : mapin.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000152 if (first) {
153 first = false;
154 } else {
155 System.out.print(", ");
156 }
Mark Slee78f58e22006-09-02 04:17:07 +0000157 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000158 }
159 System.out.print("}\n");
160
161 /**
162 * SET TEST
163 */
Mark Slee78f58e22006-09-02 04:17:07 +0000164 HashSet<Integer> setout = new HashSet<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000165 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000166 setout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000167 }
168 System.out.print("testSet({");
169 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000170 for (int elem : setout) {
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(elem);
Mark Slee95771002006-06-07 06:53:25 +0000177 }
178 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000179 HashSet<Integer> setin = testClient.testSet(setout);
Mark Slee95771002006-06-07 06:53:25 +0000180 System.out.print(" = {");
181 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000182 for (int elem : setin) {
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(elem);
Mark Slee95771002006-06-07 06:53:25 +0000189 }
190 System.out.print("}\n");
191
192 /**
193 * LIST TEST
194 */
Mark Slee78f58e22006-09-02 04:17:07 +0000195 ArrayList<Integer> listout = new ArrayList<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000196 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000197 listout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000198 }
199 System.out.print("testList({");
200 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000201 for (int elem : listout) {
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 ArrayList<Integer> listin = testClient.testList(listout);
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 : listin) {
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 * ENUM TEST
225 */
226 System.out.print("testEnum(ONE)");
Mark Slee78f58e22006-09-02 04:17:07 +0000227 int ret = testClient.testEnum(Numberz.ONE);
228 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000229
230 System.out.print("testEnum(TWO)");
231 ret = testClient.testEnum(Numberz.TWO);
Mark Slee78f58e22006-09-02 04:17:07 +0000232 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000233
234 System.out.print("testEnum(THREE)");
235 ret = testClient.testEnum(Numberz.THREE);
Mark Slee78f58e22006-09-02 04:17:07 +0000236 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000237
238 System.out.print("testEnum(FIVE)");
239 ret = testClient.testEnum(Numberz.FIVE);
Mark Slee78f58e22006-09-02 04:17:07 +0000240 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000241
242 System.out.print("testEnum(EIGHT)");
243 ret = testClient.testEnum(Numberz.EIGHT);
Mark Slee78f58e22006-09-02 04:17:07 +0000244 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000245
246 /**
247 * TYPEDEF TEST
248 */
249 System.out.print("testTypedef(309858235082523)");
Mark Slee78f58e22006-09-02 04:17:07 +0000250 long uid = testClient.testTypedef(309858235082523L);
251 System.out.print(" = " + uid + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000252
253 /**
254 * NESTED MAP TEST
255 */
256 System.out.print("testMapMap(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000257 HashMap<Integer,HashMap<Integer,Integer>> mm =
258 testClient.testMapMap(1);
Mark Slee95771002006-06-07 06:53:25 +0000259 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000260 for (int key : mm.keySet()) {
261 System.out.print(key + " => {");
262 HashMap<Integer,Integer> m2 = mm.get(key);
263 for (int k2 : m2.keySet()) {
264 System.out.print(k2 + " => " + m2.get(k2) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000265 }
266 System.out.print("}, ");
267 }
268 System.out.print("}\n");
269
270 /**
271 * INSANITY TEST
272 */
273 Insanity insane = new Insanity();
Mark Slee78f58e22006-09-02 04:17:07 +0000274 insane.userMap.put(Numberz.FIVE, (long)5000);
Mark Slee95771002006-06-07 06:53:25 +0000275 Xtruct truck = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000276 truck.string_thing = "Truck";
277 truck.byte_thing = (byte)8;
278 truck.i32_thing = 8;
279 truck.i64_thing = 8;
Mark Slee95771002006-06-07 06:53:25 +0000280 insane.xtructs.add(truck);
281 System.out.print("testInsanity()");
Mark Slee78f58e22006-09-02 04:17:07 +0000282 HashMap<Long,HashMap<Integer,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000283 testClient.testInsanity(insane);
284 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000285 for (long key : whoa.keySet()) {
286 HashMap<Integer,Insanity> val = whoa.get(key);
287 System.out.print(key + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000288
Mark Slee78f58e22006-09-02 04:17:07 +0000289 for (int k2 : val.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000290 Insanity v2 = val.get(k2);
Mark Slee78f58e22006-09-02 04:17:07 +0000291 System.out.print(k2 + " => {");
292 HashMap<Integer, Long> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000293 System.out.print("{");
Mark Slee78f58e22006-09-02 04:17:07 +0000294 for (int k3 : userMap.keySet()) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000295 System.out.print(k3 + " => " + userMap.get(k3) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000296 }
297 System.out.print("}, ");
298
299 ArrayList<Xtruct> xtructs = v2.xtructs;
300 System.out.print("{");
301 for (Xtruct x : xtructs) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000302 System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
Mark Slee95771002006-06-07 06:53:25 +0000303 }
304 System.out.print("}");
305
306 System.out.print("}, ");
307 }
308 System.out.print("}, ");
309 }
310 System.out.print("}\n");
311
Mark Sleed788b2e2006-09-07 01:26:35 +0000312 long stop = System.nanoTime();
313 long tot = stop-start;
314
315 System.out.println("Total time: " + tot/1000 + "us");
316
317 if (timeMin == 0 || tot < timeMin) {
318 timeMin = tot;
319 }
320 if (tot > timeMax) {
321 timeMax = tot;
322 }
323 timeTot += tot;
Mark Slee95771002006-06-07 06:53:25 +0000324
325 tSocket.close();
326 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000327
328 long timeAvg = timeTot / numTests;
Mark Slee95771002006-06-07 06:53:25 +0000329
Mark Sleed788b2e2006-09-07 01:26:35 +0000330 System.out.println("Min time: " + timeMin/1000 + "us");
331 System.out.println("Max time: " + timeMax/1000 + "us");
332 System.out.println("Avg time: " + timeAvg/1000 + "us");
333
Mark Slee95771002006-06-07 06:53:25 +0000334 } catch (Exception x) {
335 x.printStackTrace();
Mark Sleed788b2e2006-09-07 01:26:35 +0000336 }
337
Mark Slee95771002006-06-07 06:53:25 +0000338 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000339
Mark Slee95771002006-06-07 06:53:25 +0000340}