blob: f6a869950a3597216931aa70559114065ff94763 [file] [log] [blame]
David Reiss382fc302007-08-25 18:01:30 +00001#!/usr/bin/env python
2r"""
3thrift -py DebugProtoTest.thrift
4./FastbinaryTest.py
5"""
6
7# TODO(dreiss): Test error cases. Check for memory leaks.
8
9import sys
10sys.path.append('./gen-py')
11
12import math
13from DebugProtoTest import Srv
14from DebugProtoTest.ttypes import *
15from thrift.transport import TTransport
16from thrift.protocol import TBinaryProtocol
17
18import timeit
19from cStringIO import StringIO
20from copy import deepcopy
21from pprint import pprint
22
23class TDevNullTransport(TTransport.TTransportBase):
24 def __init__(self):
25 pass
26 def isOpen(self):
27 return True
28
29ooe1 = OneOfEach()
30ooe1.im_true = True;
31ooe1.im_false = False;
32ooe1.a_bite = 0xd6;
33ooe1.integer16 = 27000;
34ooe1.integer32 = 1<<24;
35ooe1.integer64 = 6000 * 1000 * 1000;
36ooe1.double_precision = math.pi;
37ooe1.some_characters = "Debug THIS!";
38ooe1.zomg_unicode = "\xd7\n\a\t";
39
40ooe2 = OneOfEach();
41ooe2.integer16 = 16;
42ooe2.integer32 = 32;
43ooe2.integer64 = 64;
44ooe2.double_precision = (math.sqrt(5)+1)/2;
45ooe2.some_characters = ":R (me going \"rrrr\")";
46ooe2.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";
51
52hm = HolyMoley({"big":[], "contain":set(), "bonks":{}})
53hm.big.append(ooe1)
54hm.big.append(ooe2)
55hm.big[0].a_bite = 0x22;
56hm.big[1].a_bite = 0x22;
57
58hm.contain.add(("and a one", "and a two"))
59hm.contain.add(("then a one, two", "three!", "FOUR!"))
60hm.contain.add(())
61
62hm.bonks["nothing"] = [];
63hm.bonks["something"] = [
64 Bonk({"type":1, "message":"Wait."}),
65 Bonk({"type":2, "message":"What?"}),
66]
67hm.bonks["poe"] = [
68 Bonk({"type":3, "message":"quoth"}),
69 Bonk({"type":4, "message":"the raven"}),
70 Bonk({"type":5, "message":"nevermore"}),
71]
72
73rs = RandomStuff()
74rs.a = 1
75rs.b = 2
76rs.c = 3
77rs.myintlist = range(20)
78rs.maps = {1:Wrapper({"foo":Empty()}),2:Wrapper({"foo":Empty()})}
79rs.bigint = 124523452435L
80rs.triple = 3.14
81
David Reiss2c2e6d22007-09-05 01:14:09 +000082# make sure this splits two buffers in a buffered protocol
83rshuge = RandomStuff()
84rshuge.myintlist=range(10000)
85
David Reiss382fc302007-08-25 18:01:30 +000086my_zero = Srv.Janky_result({"arg":5})
87my_nega = Srv.Janky_args({"success":6})
88
89def checkWrite(o):
90 trans_fast = TTransport.TMemoryBuffer()
91 trans_slow = TTransport.TMemoryBuffer()
92 prot_fast = TBinaryProtocol.TBinaryProtocolAccelerated(trans_fast)
93 prot_slow = TBinaryProtocol.TBinaryProtocol(trans_slow)
94
95 o.write(prot_fast)
96 o.write(prot_slow)
97 ORIG = trans_slow.getvalue()
98 MINE = trans_fast.getvalue()
99 if ORIG != MINE:
100 print "mine: %s\norig: %s" % (repr(MINE), repr(ORIG))
101
102def checkRead(o):
103 prot = TBinaryProtocol.TBinaryProtocol(TTransport.TMemoryBuffer())
104 o.write(prot)
David Reiss2c2e6d22007-09-05 01:14:09 +0000105
106 slow_version_binary = prot.trans.getvalue()
107
David Reiss382fc302007-08-25 18:01:30 +0000108 prot = TBinaryProtocol.TBinaryProtocolAccelerated(
David Reiss2c2e6d22007-09-05 01:14:09 +0000109 TTransport.TMemoryBuffer(slow_version_binary))
110 c = o.__class__()
111 c.read(prot)
112 if c != o:
113 print "copy: "
114 pprint(eval(repr(c)))
115 print "orig: "
116 pprint(eval(repr(o)))
117
118 prot = TBinaryProtocol.TBinaryProtocolAccelerated(
119 TTransport.TBufferedTransport(
120 TTransport.TMemoryBuffer(slow_version_binary)))
David Reiss382fc302007-08-25 18:01:30 +0000121 c = o.__class__()
122 c.read(prot)
123 if c != o:
124 print "copy: "
125 pprint(eval(repr(c)))
126 print "orig: "
127 pprint(eval(repr(o)))
128
129
130def doTest():
131 checkWrite(hm)
132 no_set = deepcopy(hm)
133 no_set.contain = set()
134 checkRead(no_set)
135 checkWrite(rs)
136 checkRead(rs)
David Reiss2c2e6d22007-09-05 01:14:09 +0000137 checkWrite(rshuge)
138 checkRead(rshuge)
David Reiss382fc302007-08-25 18:01:30 +0000139 checkWrite(my_zero)
140 checkRead(my_zero)
141 checkRead(Backwards({"first_tag2":4, "second_tag1":2}))
142 try:
143 checkWrite(my_nega)
144 print "Hey, did this get fixed?"
145 except AttributeError:
146 # Sorry, doesn't work with negative tags.
147 pass
148
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)
155
156 o.write(prot_fast)
157 o.write(prot_slow)
158 ORIG = trans_slow.getvalue()
159 MINE = trans_fast.getvalue()
160 if ORIG == MINE:
161 print "That shouldn't happen."
162
163
164 prot = TBinaryProtocol.TBinaryProtocolAccelerated(TTransport.TMemoryBuffer())
165 o.write(prot)
166 prot = TBinaryProtocol.TBinaryProtocol(
167 TTransport.TMemoryBuffer(
168 prot.trans.getvalue()))
169 c = o.__class__()
170 c.read(prot)
171 if c != o:
172 print "copy: "
173 pprint(eval(repr(c)))
174 print "orig: "
175 pprint(eval(repr(o)))
176
177
178
179def doBenchmark():
180
181 iters = 25000
182
183 setup = """
184from __main__ import hm, rs, TDevNullTransport
185from thrift.protocol import TBinaryProtocol
186trans = TDevNullTransport()
187prot = TBinaryProtocol.TBinaryProtocol%s(trans)
188"""
189
190 setup_fast = setup % "Accelerated"
191 setup_slow = setup % ""
192
193 print "Starting Benchmarks"
194
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)
199
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)
204
205
206
207doTest()
208doBenchmark()
209