3 thrift -py DebugProtoTest.thrift
7 # TODO(dreiss): Test error cases. Check for memory leaks.
10 sys
.path
.append('./gen-py')
13 from DebugProtoTest
import Srv
14 from DebugProtoTest
.ttypes
import *
15 from thrift
.transport
import TTransport
16 from thrift
.protocol
import TBinaryProtocol
19 from cStringIO
import StringIO
20 from copy
import deepcopy
21 from pprint
import pprint
23 class TDevNullTransport(TTransport
.TTransportBase
):
31 ooe1
.im_false
= False;
33 ooe1
.integer16
= 27000;
34 ooe1
.integer32
= 1<<24;
35 ooe1
.integer64
= 6000 * 1000 * 1000;
36 ooe1
.double_precision
= math
.pi
;
37 ooe1
.some_characters
= "Debug THIS!";
38 ooe1
.zomg_unicode
= "\xd7\n\a\t";
44 ooe2
.double_precision
= (math
.sqrt(5)+1)/2;
45 ooe2
.some_characters
= ":R (me going \"rrrr\")";
46 ooe2
.zomg_unicode
= "\xd3\x80\xe2\x85\xae\xce\x9d\x20"\
47 "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe"\
48 "\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"\
49 "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba"\
50 "\xc7\x83\xe2\x80\xbc";
52 hm
= HolyMoley({"big":[], "contain":set(), "bonks":{}})
55 hm
.big
[0].a_bite
= 0x22;
56 hm
.big
[1].a_bite
= 0x22;
58 hm
.contain
.add(("and a one", "and a two"))
59 hm
.contain
.add(("then a one, two", "three!", "FOUR!"))
62 hm
.bonks
["nothing"] = [];
63 hm
.bonks
["something"] = [
64 Bonk({"type":1, "message":"Wait."}),
65 Bonk({"type":2, "message":"What?"}),
68 Bonk({"type":3, "message":"quoth"}),
69 Bonk({"type":4, "message":"the raven"}),
70 Bonk({"type":5, "message":"nevermore"}),
77 rs
.myintlist
= range(20)
78 rs
.maps
= {1:Wrapper({"foo":Empty()}),2:Wrapper({"foo":Empty()})}
79 rs
.bigint
= 124523452435L
82 # make sure this splits two buffers in a buffered protocol
83 rshuge
= RandomStuff()
84 rshuge
.myintlist
=range(10000)
86 my_zero
= Srv
.Janky_result({"arg":5})
87 my_nega
= Srv
.Janky_args({"success":6})
90 trans_fast
= TTransport
.TMemoryBuffer()
91 trans_slow
= TTransport
.TMemoryBuffer()
92 prot_fast
= TBinaryProtocol
.TBinaryProtocolAccelerated(trans_fast
)
93 prot_slow
= TBinaryProtocol
.TBinaryProtocol(trans_slow
)
97 ORIG
= trans_slow
.getvalue()
98 MINE
= trans_fast
.getvalue()
100 print "mine: %s\norig: %s" % (repr(MINE
), repr(ORIG
))
103 prot
= TBinaryProtocol
.TBinaryProtocol(TTransport
.TMemoryBuffer())
106 slow_version_binary
= prot
.trans
.getvalue()
108 prot
= TBinaryProtocol
.TBinaryProtocolAccelerated(
109 TTransport
.TMemoryBuffer(slow_version_binary
))
114 pprint(eval(repr(c
)))
116 pprint(eval(repr(o
)))
118 prot
= TBinaryProtocol
.TBinaryProtocolAccelerated(
119 TTransport
.TBufferedTransport(
120 TTransport
.TMemoryBuffer(slow_version_binary
)))
125 pprint(eval(repr(c
)))
127 pprint(eval(repr(o
)))
132 no_set
= deepcopy(hm
)
133 no_set
.contain
= set()
141 checkRead(Backwards({"first_tag2":4, "second_tag1":2}))
144 print "Hey, did this get fixed?"
145 except AttributeError:
146 # Sorry, doesn't work with negative tags.
149 # One case where the serialized form changes, but only superficially.
150 o
= Backwards({"first_tag2":4, "second_tag1":2})
151 trans_fast
= TTransport
.TMemoryBuffer()
152 trans_slow
= TTransport
.TMemoryBuffer()
153 prot_fast
= TBinaryProtocol
.TBinaryProtocolAccelerated(trans_fast
)
154 prot_slow
= TBinaryProtocol
.TBinaryProtocol(trans_slow
)
158 ORIG
= trans_slow
.getvalue()
159 MINE
= trans_fast
.getvalue()
161 print "That shouldn't happen."
164 prot
= TBinaryProtocol
.TBinaryProtocolAccelerated(TTransport
.TMemoryBuffer())
166 prot
= TBinaryProtocol
.TBinaryProtocol(
167 TTransport
.TMemoryBuffer(
168 prot
.trans
.getvalue()))
173 pprint(eval(repr(c
)))
175 pprint(eval(repr(o
)))
184 from __main__ import hm, rs, TDevNullTransport
185 from thrift.protocol import TBinaryProtocol
186 trans = TDevNullTransport()
187 prot = TBinaryProtocol.TBinaryProtocol%s(trans)
190 setup_fast
= setup
% "Accelerated"
191 setup_slow
= setup
% ""
193 print "Starting Benchmarks"
195 print "HolyMoley Standard = %f" % \
196 timeit
.Timer('hm.write(prot)', setup_slow
).timeit(number
=iters
)
197 print "HolyMoley Acceler. = %f" % \
198 timeit
.Timer('hm.write(prot)', setup_fast
).timeit(number
=iters
)
200 print "FastStruct Standard = %f" % \
201 timeit
.Timer('rs.write(prot)', setup_slow
).timeit(number
=iters
)
202 print "FastStruct Acceler. = %f" % \
203 timeit
.Timer('rs.write(prot)', setup_fast
).timeit(number
=iters
)