Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
package org.apache.thrift.test;
21
 
22
// Generated code
23
import thrift.test.*;
24
 
25
import org.apache.thrift.TApplicationException;
26
import org.apache.thrift.TSerializer;
27
import org.apache.thrift.transport.TTransport;
28
import org.apache.thrift.transport.TSocket;
29
import org.apache.thrift.transport.THttpClient;
30
import org.apache.thrift.transport.TFramedTransport;
31
import org.apache.thrift.transport.TTransportException;
32
import org.apache.thrift.protocol.TBinaryProtocol;
33
import org.apache.thrift.protocol.TSimpleJSONProtocol;
34
 
35
import java.util.Map;
36
import java.util.HashMap;
37
import java.util.Set;
38
import java.util.HashSet;
39
import java.util.List;
40
import java.util.ArrayList;
41
 
42
/**
43
 * Test Java client for thrift. Essentially just a copy of the C++ version,
44
 * this makes a variety of requests to enable testing for both performance and
45
 * correctness of the output.
46
 *
47
 */
48
public class TestClient {
49
  public static void main(String [] args) {
50
    try {
51
      String host = "localhost";
52
      int port = 9090;
53
      String url = null;
54
      int numTests = 1;
55
      boolean framed = false;
56
 
57
      int socketTimeout = 1000;
58
 
59
      try {
60
        for (int i = 0; i < args.length; ++i) {
61
          if (args[i].equals("-h")) {
62
            String[] hostport = (args[++i]).split(":");
63
            host = hostport[0];
64
            port = Integer.valueOf(hostport[1]);
65
          } else if (args[i].equals("-f") || args[i].equals("-framed")) {
66
            framed = true;
67
          } else if (args[i].equals("-u")) {
68
            url = args[++i];
69
          } else if (args[i].equals("-n")) {
70
            numTests = Integer.valueOf(args[++i]);
71
          } else if (args[i].equals("-timeout")) {
72
            socketTimeout = Integer.valueOf(args[++i]);
73
          }
74
        }
75
      } catch (Exception x) {
76
        x.printStackTrace();
77
      }
78
 
79
      TTransport transport;
80
 
81
      if (url != null) {
82
        transport = new THttpClient(url);
83
      } else {
84
        TSocket socket = new TSocket(host, port);
85
        socket.setTimeout(socketTimeout);
86
        transport = socket;
87
        if (framed) {
88
          transport = new TFramedTransport(transport);
89
        }
90
      }
91
 
92
      TBinaryProtocol binaryProtocol =
93
        new TBinaryProtocol(transport);
94
      ThriftTest.Client testClient =
95
        new ThriftTest.Client(binaryProtocol);
96
      Insanity insane = new Insanity();
97
 
98
      long timeMin = 0;
99
      long timeMax = 0;
100
      long timeTot = 0;
101
 
102
      for (int test = 0; test < numTests; ++test) {
103
 
104
        /**
105
         * CONNECT TEST
106
         */
107
        System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
108
        try {
109
          transport.open();
110
        } catch (TTransportException ttx) {
111
          System.out.println("Connect failed: " + ttx.getMessage());
112
          continue;
113
        }
114
 
115
        long start = System.nanoTime();
116
 
117
        /**
118
         * VOID TEST
119
         */
120
        try {
121
          System.out.print("testVoid()");
122
          testClient.testVoid();
123
          System.out.print(" = void\n");
124
        } catch (TApplicationException tax) {
125
          tax.printStackTrace();
126
        }
127
 
128
        /**
129
         * STRING TEST
130
         */
131
        System.out.print("testString(\"Test\")");
132
        String s = testClient.testString("Test");
133
        System.out.print(" = \"" + s + "\"\n");
134
 
135
        /**
136
         * BYTE TEST
137
         */
138
        System.out.print("testByte(1)");
139
        byte i8 = testClient.testByte((byte)1);
140
        System.out.print(" = " + i8 + "\n");
141
 
142
        /**
143
         * I32 TEST
144
         */
145
        System.out.print("testI32(-1)");
146
        int i32 = testClient.testI32(-1);
147
        System.out.print(" = " + i32 + "\n");
148
 
149
        /**
150
         * I64 TEST
151
         */
152
        System.out.print("testI64(-34359738368)");
153
        long i64 = testClient.testI64(-34359738368L);
154
        System.out.print(" = " + i64 + "\n");
155
 
156
        /**
157
         * DOUBLE TEST
158
         */
159
        System.out.print("testDouble(5.325098235)");
160
        double dub = testClient.testDouble(5.325098235);
161
        System.out.print(" = " + dub + "\n");
162
 
163
        /**
164
         * STRUCT TEST
165
         */
166
        System.out.print("testStruct({\"Zero\", 1, -3, -5})");
167
        Xtruct out = new Xtruct();
168
        out.string_thing = "Zero";
169
        out.byte_thing = (byte) 1;
170
        out.i32_thing = -3;
171
        out.i64_thing = -5;
172
        Xtruct in = testClient.testStruct(out);
173
        System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
174
 
175
        /**
176
         * NESTED STRUCT TEST
177
         */
178
        System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
179
        Xtruct2 out2 = new Xtruct2();
180
        out2.byte_thing = (short)1;
181
        out2.struct_thing = out;
182
        out2.i32_thing = 5;
183
        Xtruct2 in2 = testClient.testNest(out2);
184
        in = in2.struct_thing;
185
        System.out.print(" = {" + in2.byte_thing + ", {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}, " + in2.i32_thing + "}\n");
186
 
187
        /**
188
         * MAP TEST
189
         */
190
        Map<Integer,Integer> mapout = new HashMap<Integer,Integer>();
191
        for (int i = 0; i < 5; ++i) {
192
          mapout.put(i, i-10);
193
        }
194
        System.out.print("testMap({");
195
        boolean first = true;
196
        for (int key : mapout.keySet()) {
197
          if (first) {
198
            first = false;
199
          } else {
200
            System.out.print(", ");
201
          }
202
          System.out.print(key + " => " + mapout.get(key));
203
        }
204
        System.out.print("})");
205
        Map<Integer,Integer> mapin = testClient.testMap(mapout);
206
        System.out.print(" = {");
207
        first = true;
208
        for (int key : mapin.keySet()) {
209
          if (first) {
210
            first = false;
211
          } else {
212
            System.out.print(", ");
213
          }
214
          System.out.print(key + " => " + mapout.get(key));
215
        }
216
        System.out.print("}\n");
217
 
218
        /**
219
         * SET TEST
220
         */
221
        Set<Integer> setout = new HashSet<Integer>();
222
        for (int i = -2; i < 3; ++i) {
223
          setout.add(i);
224
        }
225
        System.out.print("testSet({");
226
        first = true;
227
        for (int elem : setout) {
228
          if (first) {
229
            first = false;
230
          } else {
231
            System.out.print(", ");
232
          }
233
          System.out.print(elem);
234
        }
235
        System.out.print("})");
236
        Set<Integer> setin = testClient.testSet(setout);
237
        System.out.print(" = {");
238
        first = true;
239
        for (int elem : setin) {
240
          if (first) {
241
            first = false;
242
          } else {
243
            System.out.print(", ");
244
          }
245
          System.out.print(elem);
246
        }
247
        System.out.print("}\n");
248
 
249
        /**
250
         * LIST TEST
251
         */
252
        List<Integer> listout = new ArrayList<Integer>();
253
        for (int i = -2; i < 3; ++i) {
254
          listout.add(i);
255
        }
256
        System.out.print("testList({");
257
        first = true;
258
        for (int elem : listout) {
259
          if (first) {
260
            first = false;
261
          } else {
262
            System.out.print(", ");
263
          }
264
          System.out.print(elem);
265
        }
266
        System.out.print("})");
267
        List<Integer> listin = testClient.testList(listout);
268
        System.out.print(" = {");
269
        first = true;
270
        for (int elem : listin) {
271
          if (first) {
272
            first = false;
273
          } else {
274
            System.out.print(", ");
275
          }
276
          System.out.print(elem);
277
        }
278
        System.out.print("}\n");
279
 
280
        /**
281
         * ENUM TEST
282
         */
283
        System.out.print("testEnum(ONE)");
284
        Numberz ret = testClient.testEnum(Numberz.ONE);
285
        System.out.print(" = " + ret + "\n");
286
 
287
        System.out.print("testEnum(TWO)");
288
        ret = testClient.testEnum(Numberz.TWO);
289
        System.out.print(" = " + ret + "\n");
290
 
291
        System.out.print("testEnum(THREE)");
292
        ret = testClient.testEnum(Numberz.THREE);
293
        System.out.print(" = " + ret + "\n");
294
 
295
        System.out.print("testEnum(FIVE)");
296
        ret = testClient.testEnum(Numberz.FIVE);
297
        System.out.print(" = " + ret + "\n");
298
 
299
        System.out.print("testEnum(EIGHT)");
300
        ret = testClient.testEnum(Numberz.EIGHT);
301
        System.out.print(" = " + ret + "\n");
302
 
303
        /**
304
         * TYPEDEF TEST
305
         */
306
        System.out.print("testTypedef(309858235082523)");
307
        long uid = testClient.testTypedef(309858235082523L);
308
        System.out.print(" = " + uid + "\n");
309
 
310
        /**
311
         * NESTED MAP TEST
312
         */
313
        System.out.print("testMapMap(1)");
314
        Map<Integer,Map<Integer,Integer>> mm =
315
          testClient.testMapMap(1);
316
        System.out.print(" = {");
317
        for (int key : mm.keySet()) {
318
          System.out.print(key + " => {");
319
          Map<Integer,Integer> m2 = mm.get(key);
320
          for (int k2 : m2.keySet()) {
321
            System.out.print(k2 + " => " + m2.get(k2) + ", ");
322
          }
323
          System.out.print("}, ");
324
        }
325
        System.out.print("}\n");
326
 
327
        /**
328
         * INSANITY TEST
329
         */
330
        insane = new Insanity();
331
        insane.userMap = new HashMap<Numberz, Long>();
332
        insane.userMap.put(Numberz.FIVE, (long)5000);
333
        Xtruct truck = new Xtruct();
334
        truck.string_thing = "Truck";
335
        truck.byte_thing = (byte)8;
336
        truck.i32_thing = 8;
337
        truck.i64_thing = 8;
338
        insane.xtructs = new ArrayList<Xtruct>();
339
        insane.xtructs.add(truck);
340
        System.out.print("testInsanity()");
341
        Map<Long,Map<Numberz,Insanity>> whoa =
342
          testClient.testInsanity(insane);
343
        System.out.print(" = {");
344
        for (long key : whoa.keySet()) {
345
          Map<Numberz,Insanity> val = whoa.get(key);
346
          System.out.print(key + " => {");
347
 
348
          for (Numberz k2 : val.keySet()) {
349
            Insanity v2 = val.get(k2);
350
            System.out.print(k2 + " => {");
351
            Map<Numberz, Long> userMap = v2.userMap;
352
            System.out.print("{");
353
            if (userMap != null) {
354
              for (Numberz k3 : userMap.keySet()) {
355
                System.out.print(k3 + " => " + userMap.get(k3) + ", ");
356
              }
357
            }
358
            System.out.print("}, ");
359
 
360
            List<Xtruct> xtructs = v2.xtructs;
361
            System.out.print("{");
362
            if (xtructs != null) {
363
              for (Xtruct x : xtructs) {
364
                System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
365
              }
366
            }
367
            System.out.print("}");
368
 
369
            System.out.print("}, ");
370
          }
371
          System.out.print("}, ");
372
        }
373
        System.out.print("}\n");
374
 
375
        // Test oneway
376
        System.out.print("testOneway(3)...");
377
        long startOneway = System.nanoTime();
378
        testClient.testOneway(3);
379
        long onewayElapsedMillis = (System.nanoTime() - startOneway) / 1000000;
380
        if (onewayElapsedMillis > 200) {
381
          throw new Exception("Oneway test failed: took " +
382
                              Long.toString(onewayElapsedMillis) +
383
                              "ms");
384
        } else {
385
          System.out.println("Success - took " +
386
                             Long.toString(onewayElapsedMillis) +
387
                             "ms");
388
        }
389
 
390
 
391
        long stop = System.nanoTime();
392
        long tot = stop-start;
393
 
394
        System.out.println("Total time: " + tot/1000 + "us");
395
 
396
        if (timeMin == 0 || tot < timeMin) {
397
          timeMin = tot;
398
        }
399
        if (tot > timeMax) {
400
          timeMax = tot;
401
        }
402
        timeTot += tot;
403
 
404
        transport.close();
405
      }
406
 
407
      long timeAvg = timeTot / numTests;
408
 
409
      System.out.println("Min time: " + timeMin/1000 + "us");
410
      System.out.println("Max time: " + timeMax/1000 + "us");
411
      System.out.println("Avg time: " + timeAvg/1000 + "us");
412
 
413
      String json = (new TSerializer(new TSimpleJSONProtocol.Factory())).toString(insane);
414
 
415
      System.out.println("\nFor good meausre here is some JSON:\n" + json);
416
 
417
    } catch (Exception x) {
418
      x.printStackTrace();
419
    }
420
 
421
  }
422
 
423
}