~ubuntu-branches/debian/sid/pyrlp/sid

« back to all changes in this revision

Viewing changes to tests/speed.py

  • Committer: Package Import Robot
  • Author(s): Ben Finney
  • Date: 2017-07-15 05:25:42 UTC
  • Revision ID: package-import@ubuntu.com-20170715052542-a20zzsypt1qfecq1
Tags: upstream-0.5.1
ImportĀ upstreamĀ versionĀ 0.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
util to benchmark known usecase
 
3
"""
 
4
import random
 
5
import rlp
 
6
from rlp.sedes import big_endian_int, BigEndianInt, Binary, binary, CountableList
 
7
import time
 
8
 
 
9
address = Binary.fixed_length(20, allow_empty=True)
 
10
int20 = BigEndianInt(20)
 
11
int32 = BigEndianInt(32)
 
12
int256 = BigEndianInt(256)
 
13
hash32 = Binary.fixed_length(32)
 
14
trie_root = Binary.fixed_length(32, allow_empty=True)
 
15
 
 
16
 
 
17
def zpad(x, l):
 
18
    return b'\x00' * max(0, l - len(x)) + x
 
19
 
 
20
 
 
21
class Transaction(rlp.Serializable):
 
22
    fields = [
 
23
        ('nonce', big_endian_int),
 
24
        ('gasprice', big_endian_int),
 
25
        ('startgas', big_endian_int),
 
26
        ('to', address),
 
27
        ('value', big_endian_int),
 
28
        ('data', binary),
 
29
        ('v', big_endian_int),
 
30
        ('r', big_endian_int),
 
31
        ('s', big_endian_int),
 
32
    ]
 
33
 
 
34
    def __init__(self, nonce, gasprice, startgas, to, value, data, v=0, r=0, s=0):
 
35
        super(Transaction, self).__init__(nonce, gasprice, startgas, to, value, data, v, r, s)
 
36
 
 
37
 
 
38
class BlockHeader(rlp.Serializable):
 
39
    fields = [
 
40
        ('prevhash', hash32),
 
41
        ('uncles_hash', hash32),
 
42
        ('coinbase', address),
 
43
        ('state_root', trie_root),
 
44
        ('tx_list_root', trie_root),
 
45
        ('receipts_root', trie_root),
 
46
        ('bloom', int256),
 
47
        ('difficulty', big_endian_int),
 
48
        ('number', big_endian_int),
 
49
        ('gas_limit', big_endian_int),
 
50
        ('gas_used', big_endian_int),
 
51
        ('timestamp', big_endian_int),
 
52
        ('extra_data', binary),
 
53
        ('mixhash', binary),
 
54
        ('nonce', binary)
 
55
    ]
 
56
 
 
57
    def __init__(self, *args, **kargs):
 
58
        super(BlockHeader, self).__init__(*args, **kargs)
 
59
 
 
60
 
 
61
class Block(rlp.Serializable):
 
62
    fields = [
 
63
        ('header', BlockHeader),
 
64
        ('transaction_list', CountableList(Transaction)),
 
65
        ('uncles', CountableList(BlockHeader))
 
66
    ]
 
67
 
 
68
    def __init__(self, header, transaction_list=[], uncles=[]):
 
69
        super(Block, self).__init__(header, transaction_list, uncles)
 
70
 
 
71
 
 
72
def rand_bytes(num=32):
 
73
    return zpad(big_endian_int.serialize(random.getrandbits(num * 8)), num)
 
74
 
 
75
rand_bytes32 = rand_bytes
 
76
 
 
77
 
 
78
def rand_address():
 
79
    return rand_bytes(20)
 
80
 
 
81
 
 
82
def rand_bytes8():
 
83
    return rand_bytes(8)
 
84
 
 
85
 
 
86
def rand_bigint():
 
87
    return random.getrandbits(256)
 
88
 
 
89
 
 
90
def rand_int():
 
91
    return random.getrandbits(32)
 
92
 
 
93
 
 
94
rand_map = {hash32: rand_bytes32, trie_root: rand_bytes32, binary: rand_bytes32,
 
95
            address: rand_address, Binary: rand_bytes8, big_endian_int: rand_int,
 
96
            int256: rand_bigint}
 
97
 
 
98
assert Binary in rand_map
 
99
 
 
100
 
 
101
def mk_transaction():
 
102
    return Transaction(rand_int(), rand_int(), rand_int(), rand_address(), rand_int(),
 
103
                       rand_bytes32(), 27, rand_bigint(), rand_bigint())
 
104
rlp.decode(rlp.encode(mk_transaction()), Transaction)
 
105
 
 
106
 
 
107
def mk_block_header():
 
108
    return BlockHeader(*[rand_map[t]() for _, t in BlockHeader.fields])
 
109
rlp.decode(rlp.encode(mk_block_header()), BlockHeader)
 
110
 
 
111
 
 
112
def mk_block(num_transactions=10, num_uncles=1):
 
113
    return Block(mk_block_header(),
 
114
                 [mk_transaction() for i in range(num_transactions)],
 
115
                 [mk_block_header() for i in range(num_uncles)])
 
116
rlp.decode(rlp.encode(mk_block()), Block)
 
117
 
 
118
 
 
119
def do_test_serialize(block, rounds=100):
 
120
    for i in range(rounds):
 
121
        x = rlp.encode(block)
 
122
    return x
 
123
 
 
124
 
 
125
def do_test_deserialize(data, rounds=100, sedes=Block):
 
126
    for i in range(rounds):
 
127
        x = rlp.decode(data, sedes)
 
128
    return x
 
129
 
 
130
 
 
131
def main(rounds=10000):
 
132
    st = time.time()
 
133
    d = do_test_serialize(mk_block(), rounds)
 
134
    elapsed = time.time() - st
 
135
    print 'Block serializations / sec: %.2f' % (rounds / elapsed)
 
136
 
 
137
    st = time.time()
 
138
    d = do_test_deserialize(d, rounds)
 
139
    elapsed = time.time() - st
 
140
    print 'Block deserializations / sec: %.2f' % (rounds / elapsed)
 
141
 
 
142
    st = time.time()
 
143
    d = do_test_serialize(mk_transaction(), rounds)
 
144
    elapsed = time.time() - st
 
145
    print 'TX serializations / sec: %.2f' % (rounds / elapsed)
 
146
 
 
147
    st = time.time()
 
148
    d = do_test_deserialize(d, rounds, sedes=Transaction)
 
149
    elapsed = time.time() - st
 
150
    print 'TX deserializations / sec: %.2f' % (rounds / elapsed)
 
151
 
 
152
 
 
153
if __name__ == '__main__':
 
154
    main()
 
155
    """
 
156
    py2
 
157
    serializations / sec: 658.64
 
158
    deserializations / sec: 1331.62
 
159
 
 
160
    pypy2
 
161
    serializations / sec: 4628.81      : x7 speedup
 
162
    deserializations / sec: 4753.84    : x3.5 speedup
 
163
    """