blob: 07066e394b224660cccb6a0ad100ec7e43524554 [file] [log] [blame]
Mark Slee57cc25e2007-02-28 21:43:54 +00001#!/usr/bin/env python
Mark Sleefc89d392006-09-04 00:04:39 +00002
3import sys
4sys.path.append('./gen-py')
5
Mark Slee57cc25e2007-02-28 21:43:54 +00006from ThriftTest import ThriftTest
7from ThriftTest.ttypes 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 Slee57cc25e2007-02-28 21:43:54 +000012import time
13
Mark Sleea3302652006-10-25 19:03:32 +000014import hotshot
15from hotshot import stats
16prof = None
17
18# Uncomment if you want to profile this biznizzy
19#prof = hotshot.Profile('hotshot_thrift_stats')
20#prof.start()
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
Mark Slee57cc25e2007-02-28 21:43:54 +000058tstart = time.time()
Mark Sleec9676562006-09-05 17:34:52 +000059
Mark Slee57cc25e2007-02-28 21:43:54 +000060try:
61 print "testVoid()"
62 print client.testVoid()
63except TApplicationException, x:
64 print x.message
65 print x.type
66
Mark Sleec9676562006-09-05 17:34:52 +000067print "testString('Python')"
68print client.testString('Python')
Mark Sleefc89d392006-09-04 00:04:39 +000069
70print "testByte(63)"
71print client.testByte(63)
72
Mark Sleec9676562006-09-05 17:34:52 +000073print "testI32(-1)"
74print client.testI32(-1)
75
Mark Sleec98d0502006-09-06 02:42:25 +000076print "testI32(0)"
77print client.testI32(0)
78
Mark Sleec9676562006-09-05 17:34:52 +000079print "testI64(-34359738368)"
80print client.testI64(-34359738368)
81
Mark Sleec98d0502006-09-06 02:42:25 +000082print "testDouble(-5.235098235)"
83print client.testDouble(-5.235098235)
84
Mark Sleec9676562006-09-05 17:34:52 +000085print "testStruct({Zero, 1, -3, -5})"
86x = Xtruct()
87x.string_thing = "Zero"
88x.byte_thing = 1
89x.i32_thing = -3
90x.i64_thing = -5
91x = client.testStruct(x)
92print "{%s, %d, %d, %d}" % (x.string_thing, x.byte_thing, x.i32_thing, x.i64_thing)
93
Mark Sleefc89d392006-09-04 00:04:39 +000094print "testException('Safe')"
95print client.testException('Safe')
96
Mark Sleefc89d392006-09-04 00:04:39 +000097try:
Mark Sleec9676562006-09-05 17:34:52 +000098 print "textException('Xception')"
Mark Sleefc89d392006-09-04 00:04:39 +000099 print client.testException('Xception')
Mark Sleec9676562006-09-05 17:34:52 +0000100
Mark Sleefc89d392006-09-04 00:04:39 +0000101except Xception, x:
Mark Sleec9676562006-09-05 17:34:52 +0000102 print "Xception (%d, %s)" % (x.errorCode, x.message)
103
Mark Slee57cc25e2007-02-28 21:43:54 +0000104tend = time.time()
105ttotal = (tend-tstart)*1000
106print "Total time: %f ms" % (ttotal)
Mark Sleefc89d392006-09-04 00:04:39 +0000107
Mark Sleea3302652006-10-25 19:03:32 +0000108# Close!
Mark Sleefc89d392006-09-04 00:04:39 +0000109transport.close()
Mark Sleea3302652006-10-25 19:03:32 +0000110
111# Profiler output
112if prof != None:
113 prof.stop()
114 prof.close()
115 s = stats.load('hotshot_thrift_stats')
116 s.sort_stats('time').print_stats()