blob: 3e487337e7097845fdd430939953ca459d00cb69 [file] [log] [blame]
Mark Sleefc89d392006-09-04 00:04:39 +00001#!/usr/bin/python
2
3import sys
4sys.path.append('./gen-py')
5
6import ThriftTest
7from ThriftTest_types import *
Mark Sleec9676562006-09-05 17:34:52 +00008from thrift.transport import TTransport
Mark Sleefc89d392006-09-04 00:04:39 +00009from thrift.transport import TSocket
10from thrift.protocol import TBinaryProtocol
11
Mark Sleea3302652006-10-25 19:03:32 +000012import hotshot
13from hotshot import stats
14prof = None
15
16# Uncomment if you want to profile this biznizzy
17#prof = hotshot.Profile('hotshot_thrift_stats')
18#prof.start()
19
Mark Sleec9676562006-09-05 17:34:52 +000020import timing
21
Mark Sleea3302652006-10-25 19:03:32 +000022host = 'localhost'
23port = 9090
24framed = False
25framedInput = True
26argi = 1
27
28# Parse args
29while 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
43socket = TSocket.TSocket(host, port)
44
45# Frame or buffer depending upon args
46if framed:
47 transport = TTransport.TFramedTransport(socket, framedInput, True)
48else:
49 transport = TTransport.TBufferedTransport(socket)
50
Mark Slee1dd819c2006-10-26 04:56:18 +000051protocol = TBinaryProtocol.TBinaryProtocol(transport)
52client = ThriftTest.Client(protocol)
Mark Sleefc89d392006-09-04 00:04:39 +000053
Mark Sleea3302652006-10-25 19:03:32 +000054# Connect!
Mark Sleefc89d392006-09-04 00:04:39 +000055transport.open()
56
Mark Sleec9676562006-09-05 17:34:52 +000057# Start debug timing
58timing.start()
59
Mark Sleefc89d392006-09-04 00:04:39 +000060print "testVoid()"
61print client.testVoid()
62
Mark Sleec9676562006-09-05 17:34:52 +000063print "testString('Python')"
64print client.testString('Python')
Mark Sleefc89d392006-09-04 00:04:39 +000065
66print "testByte(63)"
67print client.testByte(63)
68
Mark Sleec9676562006-09-05 17:34:52 +000069print "testI32(-1)"
70print client.testI32(-1)
71
Mark Sleec98d0502006-09-06 02:42:25 +000072print "testI32(0)"
73print client.testI32(0)
74
Mark Sleec9676562006-09-05 17:34:52 +000075print "testI64(-34359738368)"
76print client.testI64(-34359738368)
77
Mark Sleec98d0502006-09-06 02:42:25 +000078print "testDouble(-5.235098235)"
79print client.testDouble(-5.235098235)
80
Mark Sleec9676562006-09-05 17:34:52 +000081print "testStruct({Zero, 1, -3, -5})"
82x = Xtruct()
83x.string_thing = "Zero"
84x.byte_thing = 1
85x.i32_thing = -3
86x.i64_thing = -5
87x = client.testStruct(x)
88print "{%s, %d, %d, %d}" % (x.string_thing, x.byte_thing, x.i32_thing, x.i64_thing)
89
Mark Sleefc89d392006-09-04 00:04:39 +000090print "testException('Safe')"
91print client.testException('Safe')
92
Mark Sleefc89d392006-09-04 00:04:39 +000093try:
Mark Sleec9676562006-09-05 17:34:52 +000094 print "textException('Xception')"
Mark Sleefc89d392006-09-04 00:04:39 +000095 print client.testException('Xception')
Mark Sleec9676562006-09-05 17:34:52 +000096
Mark Sleefc89d392006-09-04 00:04:39 +000097except Xception, x:
Mark Sleec9676562006-09-05 17:34:52 +000098 print "Xception (%d, %s)" % (x.errorCode, x.message)
99
100timing.finish()
101print "Total time: %d microsecs" % timing.micro()
Mark Sleefc89d392006-09-04 00:04:39 +0000102
Mark Sleea3302652006-10-25 19:03:32 +0000103# Close!
Mark Sleefc89d392006-09-04 00:04:39 +0000104transport.close()
Mark Sleea3302652006-10-25 19:03:32 +0000105
106# Profiler output
107if prof != None:
108 prof.stop()
109 prof.close()
110 s = stats.load('hotshot_thrift_stats')
111 s.sort_stats('time').print_stats()