blob: 46a4ca5aeb9ff9a4b8b515cf98ea260cdd5502e6 [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 Slee95771002006-06-07 06:53:25 +00009import com.facebook.thrift.transport.TTransportException;
10import com.facebook.thrift.protocol.TBinaryProtocol;
Mark Slee95771002006-06-07 06:53:25 +000011
12import java.util.HashMap;
13import java.util.HashSet;
14import java.util.ArrayList;
15
16/**
17 * Test Java client for thrift. Essentially just a copy of the C++ version,
18 * this makes a variety of requests to enable testing for both performance and
19 * correctness of the output.
20 *
21 * @author Mark Slee <mcslee@facebook.com>
22 */
23public class TestClient {
24 public static void main(String [] args) {
25 try {
26 String host = "localhost";
27 int port = 9090;
Mark Sleeb9acf982006-10-10 01:57:32 +000028 String url = null;
Mark Slee95771002006-06-07 06:53:25 +000029 int numTests = 1;
Mark Sleeb9acf982006-10-10 01:57:32 +000030
31 try {
32 for (int i = 0; i < args.length; ++i) {
33 if (args[i].equals("-h")) {
34 String[] hostport = (args[++i]).split(";");
35 host = hostport[0];
36 port = Integer.valueOf(hostport[1]);
37 }
38
39 if (args[i].equals("-u")) {
40 url = args[++i];
41 }
42
43 if (args[i].equals("-n")) {
44 numTests = Integer.valueOf(args[++i]);
45 }
46 }
47 } catch (Exception x) {
48 x.printStackTrace();
Mark Slee95771002006-06-07 06:53:25 +000049 }
50
Mark Sleeb9acf982006-10-10 01:57:32 +000051 TTransport transport;
52
53 if (url != null) {
54 transport = new THttpClient(url);
55 } else {
56 transport = new TSocket(host, port);
57 }
58
Mark Slee95771002006-06-07 06:53:25 +000059 TBinaryProtocol binaryProtocol =
60 new TBinaryProtocol();
Mark Slee78f58e22006-09-02 04:17:07 +000061 ThriftTest.Client testClient =
Mark Sleeb9acf982006-10-10 01:57:32 +000062 new ThriftTest.Client(transport, binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +000063
Mark Sleed788b2e2006-09-07 01:26:35 +000064 long timeMin = 0;
65 long timeMax = 0;
66 long timeTot = 0;
67
Mark Slee95771002006-06-07 06:53:25 +000068 for (int test = 0; test < numTests; ++test) {
69
70 /**
71 * CONNECT TEST
72 */
Mark Sleed788b2e2006-09-07 01:26:35 +000073 System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
Mark Slee95771002006-06-07 06:53:25 +000074 try {
Mark Sleeb9acf982006-10-10 01:57:32 +000075 transport.open();
Mark Slee95771002006-06-07 06:53:25 +000076 } catch (TTransportException ttx) {
77 System.out.println("Connect failed: " + ttx.getMessage());
78 continue;
79 }
80
Mark Sleed788b2e2006-09-07 01:26:35 +000081 long start = System.nanoTime();
Mark Slee95771002006-06-07 06:53:25 +000082
83 /**
84 * VOID TEST
85 */
86 System.out.print("testVoid()");
87 testClient.testVoid();
88 System.out.print(" = void\n");
89
90 /**
91 * STRING TEST
92 */
93 System.out.print("testString(\"Test\")");
Mark Slee78f58e22006-09-02 04:17:07 +000094 String s = testClient.testString("Test");
95 System.out.print(" = \"" + s + "\"\n");
96
Mark Slee95771002006-06-07 06:53:25 +000097 /**
98 * BYTE TEST
99 */
100 System.out.print("testByte(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000101 byte i8 = testClient.testByte((byte)1);
102 System.out.print(" = " + i8 + "\n");
Mark Slee6e536442006-06-30 18:28:50 +0000103
Mark Slee95771002006-06-07 06:53:25 +0000104 /**
105 * I32 TEST
106 */
107 System.out.print("testI32(-1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000108 int i32 = testClient.testI32(-1);
109 System.out.print(" = " + i32 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000110
111 /**
Mark Slee95771002006-06-07 06:53:25 +0000112 * I64 TEST
113 */
114 System.out.print("testI64(-34359738368)");
Mark Slee78f58e22006-09-02 04:17:07 +0000115 long i64 = testClient.testI64(-34359738368L);
116 System.out.print(" = " + i64 + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000117
118 /**
Mark Sleec98d0502006-09-06 02:42:25 +0000119 * DOUBLE TEST
120 */
121 System.out.print("testDouble(5.325098235)");
122 double dub = testClient.testDouble(5.325098235);
123 System.out.print(" = " + dub + "\n");
124
125 /**
Mark Slee95771002006-06-07 06:53:25 +0000126 * STRUCT TEST
127 */
Mark Slee6e536442006-06-30 18:28:50 +0000128 System.out.print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee95771002006-06-07 06:53:25 +0000129 Xtruct out = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000130 out.string_thing = "Zero";
131 out.byte_thing = (byte) 1;
132 out.i32_thing = -3;
133 out.i64_thing = -5;
Mark Slee95771002006-06-07 06:53:25 +0000134 Xtruct in = testClient.testStruct(out);
Mark Sleed788b2e2006-09-07 01:26:35 +0000135 System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000136
137 /**
138 * NESTED STRUCT TEST
139 */
Mark Slee6e536442006-06-30 18:28:50 +0000140 System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Slee95771002006-06-07 06:53:25 +0000141 Xtruct2 out2 = new Xtruct2();
Mark Slee78f58e22006-09-02 04:17:07 +0000142 out2.byte_thing = (short)1;
Mark Slee95771002006-06-07 06:53:25 +0000143 out2.struct_thing = out;
Mark Slee78f58e22006-09-02 04:17:07 +0000144 out2.i32_thing = 5;
Mark Slee95771002006-06-07 06:53:25 +0000145 Xtruct2 in2 = testClient.testNest(out2);
146 in = in2.struct_thing;
Mark Sleed788b2e2006-09-07 01:26:35 +0000147 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 +0000148
149 /**
150 * MAP TEST
151 */
Mark Slee78f58e22006-09-02 04:17:07 +0000152 HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000153 for (int i = 0; i < 5; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000154 mapout.put(i, i-10);
Mark Slee95771002006-06-07 06:53:25 +0000155 }
156 System.out.print("testMap({");
157 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000158 for (int key : mapout.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000159 if (first) {
160 first = false;
161 } else {
162 System.out.print(", ");
163 }
Mark Slee78f58e22006-09-02 04:17:07 +0000164 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000165 }
166 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000167 HashMap<Integer,Integer> mapin = testClient.testMap(mapout);
Mark Slee95771002006-06-07 06:53:25 +0000168 System.out.print(" = {");
169 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000170 for (int key : mapin.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("}\n");
179
180 /**
181 * SET TEST
182 */
Mark Slee78f58e22006-09-02 04:17:07 +0000183 HashSet<Integer> setout = new HashSet<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000184 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000185 setout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000186 }
187 System.out.print("testSet({");
188 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000189 for (int elem : setout) {
Mark Slee95771002006-06-07 06:53:25 +0000190 if (first) {
191 first = false;
192 } else {
193 System.out.print(", ");
194 }
Mark Slee78f58e22006-09-02 04:17:07 +0000195 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000196 }
197 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000198 HashSet<Integer> setin = testClient.testSet(setout);
Mark Slee95771002006-06-07 06:53:25 +0000199 System.out.print(" = {");
200 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000201 for (int elem : setin) {
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("}\n");
210
211 /**
212 * LIST TEST
213 */
Mark Slee78f58e22006-09-02 04:17:07 +0000214 ArrayList<Integer> listout = new ArrayList<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000215 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000216 listout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000217 }
218 System.out.print("testList({");
219 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000220 for (int elem : listout) {
Mark Slee95771002006-06-07 06:53:25 +0000221 if (first) {
222 first = false;
223 } else {
224 System.out.print(", ");
225 }
Mark Slee78f58e22006-09-02 04:17:07 +0000226 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000227 }
228 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000229 ArrayList<Integer> listin = testClient.testList(listout);
Mark Slee95771002006-06-07 06:53:25 +0000230 System.out.print(" = {");
231 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000232 for (int elem : listin) {
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("}\n");
241
242 /**
243 * ENUM TEST
244 */
245 System.out.print("testEnum(ONE)");
Mark Slee78f58e22006-09-02 04:17:07 +0000246 int ret = testClient.testEnum(Numberz.ONE);
247 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000248
249 System.out.print("testEnum(TWO)");
250 ret = testClient.testEnum(Numberz.TWO);
Mark Slee78f58e22006-09-02 04:17:07 +0000251 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000252
253 System.out.print("testEnum(THREE)");
254 ret = testClient.testEnum(Numberz.THREE);
Mark Slee78f58e22006-09-02 04:17:07 +0000255 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000256
257 System.out.print("testEnum(FIVE)");
258 ret = testClient.testEnum(Numberz.FIVE);
Mark Slee78f58e22006-09-02 04:17:07 +0000259 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000260
261 System.out.print("testEnum(EIGHT)");
262 ret = testClient.testEnum(Numberz.EIGHT);
Mark Slee78f58e22006-09-02 04:17:07 +0000263 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000264
265 /**
266 * TYPEDEF TEST
267 */
268 System.out.print("testTypedef(309858235082523)");
Mark Slee78f58e22006-09-02 04:17:07 +0000269 long uid = testClient.testTypedef(309858235082523L);
270 System.out.print(" = " + uid + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000271
272 /**
273 * NESTED MAP TEST
274 */
275 System.out.print("testMapMap(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000276 HashMap<Integer,HashMap<Integer,Integer>> mm =
277 testClient.testMapMap(1);
Mark Slee95771002006-06-07 06:53:25 +0000278 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000279 for (int key : mm.keySet()) {
280 System.out.print(key + " => {");
281 HashMap<Integer,Integer> m2 = mm.get(key);
282 for (int k2 : m2.keySet()) {
283 System.out.print(k2 + " => " + m2.get(k2) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000284 }
285 System.out.print("}, ");
286 }
287 System.out.print("}\n");
288
289 /**
290 * INSANITY TEST
291 */
292 Insanity insane = new Insanity();
Mark Slee78f58e22006-09-02 04:17:07 +0000293 insane.userMap.put(Numberz.FIVE, (long)5000);
Mark Slee95771002006-06-07 06:53:25 +0000294 Xtruct truck = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000295 truck.string_thing = "Truck";
296 truck.byte_thing = (byte)8;
297 truck.i32_thing = 8;
298 truck.i64_thing = 8;
Mark Slee95771002006-06-07 06:53:25 +0000299 insane.xtructs.add(truck);
300 System.out.print("testInsanity()");
Mark Slee78f58e22006-09-02 04:17:07 +0000301 HashMap<Long,HashMap<Integer,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000302 testClient.testInsanity(insane);
303 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000304 for (long key : whoa.keySet()) {
305 HashMap<Integer,Insanity> val = whoa.get(key);
306 System.out.print(key + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000307
Mark Slee78f58e22006-09-02 04:17:07 +0000308 for (int k2 : val.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000309 Insanity v2 = val.get(k2);
Mark Slee78f58e22006-09-02 04:17:07 +0000310 System.out.print(k2 + " => {");
311 HashMap<Integer, Long> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000312 System.out.print("{");
Mark Slee78f58e22006-09-02 04:17:07 +0000313 for (int k3 : userMap.keySet()) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000314 System.out.print(k3 + " => " + userMap.get(k3) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000315 }
316 System.out.print("}, ");
317
318 ArrayList<Xtruct> xtructs = v2.xtructs;
319 System.out.print("{");
320 for (Xtruct x : xtructs) {
Mark Sleed788b2e2006-09-07 01:26:35 +0000321 System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
Mark Slee95771002006-06-07 06:53:25 +0000322 }
323 System.out.print("}");
324
325 System.out.print("}, ");
326 }
327 System.out.print("}, ");
328 }
329 System.out.print("}\n");
330
Mark Sleed788b2e2006-09-07 01:26:35 +0000331 long stop = System.nanoTime();
332 long tot = stop-start;
333
334 System.out.println("Total time: " + tot/1000 + "us");
335
336 if (timeMin == 0 || tot < timeMin) {
337 timeMin = tot;
338 }
339 if (tot > timeMax) {
340 timeMax = tot;
341 }
342 timeTot += tot;
Mark Slee95771002006-06-07 06:53:25 +0000343
Mark Sleeb9acf982006-10-10 01:57:32 +0000344 transport.close();
Mark Slee95771002006-06-07 06:53:25 +0000345 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000346
347 long timeAvg = timeTot / numTests;
Mark Slee95771002006-06-07 06:53:25 +0000348
Mark Sleed788b2e2006-09-07 01:26:35 +0000349 System.out.println("Min time: " + timeMin/1000 + "us");
350 System.out.println("Max time: " + timeMax/1000 + "us");
351 System.out.println("Avg time: " + timeAvg/1000 + "us");
352
Mark Slee95771002006-06-07 06:53:25 +0000353 } catch (Exception x) {
354 x.printStackTrace();
Mark Sleed788b2e2006-09-07 01:26:35 +0000355 }
356
Mark Slee95771002006-06-07 06:53:25 +0000357 }
Mark Sleed788b2e2006-09-07 01:26:35 +0000358
Mark Slee95771002006-06-07 06:53:25 +0000359}