1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
r"""
PYTHONPATH=./gen-py:../../lib/py/build/lib... ./FastbinaryTest.py
"""
# TODO(dreiss): Test error cases. Check for memory leaks.
import math
import os
import sys
import timeit
from copy import deepcopy
from pprint import pprint
from thrift.transport import TTransport
from thrift.protocol.TBinaryProtocol import TBinaryProtocol, TBinaryProtocolAccelerated
from thrift.protocol.TCompactProtocol import TCompactProtocol, TCompactProtocolAccelerated
from DebugProtoTest import Srv
from DebugProtoTest.ttypes import Backwards, Bonk, Empty, HolyMoley, OneOfEach, RandomStuff, Wrapper
class TDevNullTransport(TTransport.TTransportBase):
def __init__(self):
pass
def isOpen(self):
return True
ooe1 = OneOfEach()
ooe1.im_true = True
ooe1.im_false = False
ooe1.a_bite = 0xd6
ooe1.integer16 = 27000
ooe1.integer32 = 1 << 24
ooe1.integer64 = 6000 * 1000 * 1000
ooe1.double_precision = math.pi
ooe1.some_characters = "Debug THIS!"
ooe1.zomg_unicode = u"\xd7\n\a\t"
ooe2 = OneOfEach()
ooe2.integer16 = 16
ooe2.integer32 = 32
ooe2.integer64 = 64
ooe2.double_precision = (math.sqrt(5) + 1) / 2
ooe2.some_characters = ":R (me going \"rrrr\")"
ooe2.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"\
"\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe"\
"\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"\
"\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba"\
"\xc7\x83\xe2\x80\xbc"
hm = HolyMoley(**{"big": [], "contain": set(), "bonks": {}})
hm.big.append(ooe1)
hm.big.append(ooe2)
hm.big[0].a_bite = 0x22
hm.big[1].a_bite = 0x22
hm.contain.add(("and a one", "and a two"))
hm.contain.add(("then a one, two", "three!", "FOUR!"))
hm.contain.add(("\xd7\n\a\t",))
hm.contain.add(())
hm.bonks["nothing"] = []
hm.bonks["something"] = [
Bonk(**{"type": 1, "message": "Wait."}),
Bonk(**{"type": 2, "message": "What?"}),
]
hm.bonks["poe"] = [
Bonk(**{"type": 3, "message": "quoth"}),
Bonk(**{"type": 4, "message": "the raven"}),
Bonk(**{"type": 5, "message": "nevermore"}),
]
rs = RandomStuff()
rs.a = 1
rs.b = 2
rs.c = 3
rs.myintlist = list(range(20))
rs.maps = {1: Wrapper(**{"foo": Empty()}), 2: Wrapper(**{"foo": Empty()})}
rs.bigint = 124523452435
rs.triple = 3.14
# make sure this splits two buffers in a buffered protocol
rshuge = RandomStuff()
rshuge.myintlist = list(range(10000))
my_zero = Srv.Janky_result(**{"success": 5})
class Test(object):
def __init__(self, fast, slow):
self._fast = fast
self._slow = slow
def _check_write(self, o):
trans_fast = TTransport.TMemoryBuffer()
trans_slow = TTransport.TMemoryBuffer()
prot_fast = self._fast(trans_fast, fallback=False)
prot_slow = self._slow(trans_slow)
o.write(prot_fast)
o.write(prot_slow)
ORIG = trans_slow.getvalue()
MINE = trans_fast.getvalue()
if ORIG != MINE:
print("actual : %s\nexpected: %s" % (repr(MINE), repr(ORIG)))
raise Exception('write value mismatch')
def _check_read(self, o):
prot = self._slow(TTransport.TMemoryBuffer())
o.write(prot)
slow_version_binary = prot.trans.getvalue()
prot = self._fast(
TTransport.TMemoryBuffer(slow_version_binary), fallback=False)
c = o.__class__()
c.read(prot)
if c != o:
print("actual : ")
pprint(repr(c))
print("expected: ")
pprint(repr(o))
raise Exception('read value mismatch')
prot = self._fast(
TTransport.TBufferedTransport(
TTransport.TMemoryBuffer(slow_version_binary)), fallback=False)
c = o.__class__()
c.read(prot)
if c != o:
print("actual : ")
pprint(repr(c))
print("expected: ")
pprint(repr(o))
raise Exception('read value mismatch')
def do_test(self):
self._check_write(HolyMoley())
self._check_read(HolyMoley())
self._check_write(hm)
no_set = deepcopy(hm)
no_set.contain = set()
self._check_read(no_set)
self._check_read(hm)
self._check_write(rs)
self._check_read(rs)
self._check_write(rshuge)
self._check_read(rshuge)
self._check_write(my_zero)
self._check_read(my_zero)
self._check_read(Backwards(**{"first_tag2": 4, "second_tag1": 2}))
# One case where the serialized form changes, but only superficially.
o = Backwards(**{"first_tag2": 4, "second_tag1": 2})
trans_fast = TTransport.TMemoryBuffer()
trans_slow = TTransport.TMemoryBuffer()
prot_fast = self._fast(trans_fast, fallback=False)
prot_slow = self._slow(trans_slow)
o.write(prot_fast)
o.write(prot_slow)
ORIG = trans_slow.getvalue()
MINE = trans_fast.getvalue()
assert id(ORIG) != id(MINE)
prot = self._fast(TTransport.TMemoryBuffer(), fallback=False)
o.write(prot)
prot = self._slow(
TTransport.TMemoryBuffer(prot.trans.getvalue()))
c = o.__class__()
c.read(prot)
if c != o:
print("copy: ")
pprint(repr(c))
print("orig: ")
pprint(repr(o))
def do_test(fast, slow):
Test(fast, slow).do_test()
def do_benchmark(protocol, iters=5000, skip_slow=False):
setup = """
from __main__ import hm, rs, TDevNullTransport
from thrift.protocol.{0} import {0}{1}
trans = TDevNullTransport()
prot = {0}{1}(trans{2})
"""
setup_fast = setup.format(protocol, 'Accelerated', ', fallback=False')
if not skip_slow:
setup_slow = setup.format(protocol, '', '')
print("Starting Benchmarks")
if not skip_slow:
print("HolyMoley Standard = %f" %
timeit.Timer('hm.write(prot)', setup_slow).timeit(number=iters))
print("HolyMoley Acceler. = %f" %
timeit.Timer('hm.write(prot)', setup_fast).timeit(number=iters))
if not skip_slow:
print("FastStruct Standard = %f" %
timeit.Timer('rs.write(prot)', setup_slow).timeit(number=iters))
print("FastStruct Acceler. = %f" %
timeit.Timer('rs.write(prot)', setup_fast).timeit(number=iters))
if __name__ == '__main__':
print('Testing TBinaryAccelerated')
do_test(TBinaryProtocolAccelerated, TBinaryProtocol)
do_benchmark('TBinaryProtocol')
print('Testing TCompactAccelerated')
do_test(TCompactProtocolAccelerated, TCompactProtocol)
do_benchmark('TCompactProtocol')
|