Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 2 | |
| 3 | import sys |
| 4 | sys.path.append('./gen-py') |
| 5 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 6 | from ThriftTest import ThriftTest |
| 7 | from ThriftTest.ttypes import * |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 8 | from thrift.transport import TTransport |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 9 | from thrift.transport import TSocket |
| 10 | from thrift.protocol import TBinaryProtocol |
| 11 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 12 | import time |
| 13 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 14 | import hotshot |
| 15 | from hotshot import stats |
| 16 | prof = None |
| 17 | |
| 18 | # Uncomment if you want to profile this biznizzy |
| 19 | #prof = hotshot.Profile('hotshot_thrift_stats') |
| 20 | #prof.start() |
| 21 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 22 | host = 'localhost' |
| 23 | port = 9090 |
| 24 | framed = False |
| 25 | framedInput = True |
| 26 | argi = 1 |
| 27 | |
| 28 | # Parse args |
| 29 | while argi < len(sys.argv): |
| 30 | if sys.argv[argi] == '-h': |
| 31 | parts = sys.argv[argi+1].split(':') |
| 32 | host = parts[0] |
| 33 | port = int(parts[1]) |
| 34 | argi += 1 |
| 35 | elif sys.argv[argi] == '-f' or sys.argv[argi] == '-framed': |
| 36 | framed = True |
| 37 | elif sys.argv[argi] == '-fo': |
| 38 | framed = True |
| 39 | framedInput = False |
| 40 | argi += 1 |
| 41 | |
| 42 | # Make socket |
| 43 | socket = TSocket.TSocket(host, port) |
| 44 | |
| 45 | # Frame or buffer depending upon args |
| 46 | if framed: |
| 47 | transport = TTransport.TFramedTransport(socket, framedInput, True) |
| 48 | else: |
| 49 | transport = TTransport.TBufferedTransport(socket) |
| 50 | |
Mark Slee | 1dd819c | 2006-10-26 04:56:18 +0000 | [diff] [blame] | 51 | protocol = TBinaryProtocol.TBinaryProtocol(transport) |
| 52 | client = ThriftTest.Client(protocol) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 53 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 54 | # Connect! |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 55 | transport.open() |
| 56 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 57 | # Start debug timing |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 58 | tstart = time.time() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 59 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 60 | try: |
| 61 | print "testVoid()" |
| 62 | print client.testVoid() |
| 63 | except TApplicationException, x: |
| 64 | print x.message |
| 65 | print x.type |
| 66 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 67 | print "testString('Python')" |
| 68 | print client.testString('Python') |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 69 | |
| 70 | print "testByte(63)" |
| 71 | print client.testByte(63) |
| 72 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 73 | print "testI32(-1)" |
| 74 | print client.testI32(-1) |
| 75 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 76 | print "testI32(0)" |
| 77 | print client.testI32(0) |
| 78 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 79 | print "testI64(-34359738368)" |
| 80 | print client.testI64(-34359738368) |
| 81 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 82 | print "testDouble(-5.235098235)" |
| 83 | print client.testDouble(-5.235098235) |
| 84 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 85 | print "testStruct({Zero, 1, -3, -5})" |
| 86 | x = Xtruct() |
| 87 | x.string_thing = "Zero" |
| 88 | x.byte_thing = 1 |
| 89 | x.i32_thing = -3 |
| 90 | x.i64_thing = -5 |
| 91 | x = client.testStruct(x) |
| 92 | print "{%s, %d, %d, %d}" % (x.string_thing, x.byte_thing, x.i32_thing, x.i64_thing) |
| 93 | |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 94 | print "testException('Safe')" |
| 95 | print client.testException('Safe') |
| 96 | |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 97 | try: |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 98 | print "textException('Xception')" |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 99 | print client.testException('Xception') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 100 | |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 101 | except Xception, x: |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 102 | print "Xception (%d, %s)" % (x.errorCode, x.message) |
| 103 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 104 | tend = time.time() |
| 105 | ttotal = (tend-tstart)*1000 |
| 106 | print "Total time: %f ms" % (ttotal) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 107 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 108 | # Close! |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 109 | transport.close() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 110 | |
| 111 | # Profiler output |
| 112 | if prof != None: |
| 113 | prof.stop() |
| 114 | prof.close() |
| 115 | s = stats.load('hotshot_thrift_stats') |
| 116 | s.sort_stats('time').print_stats() |