-
Notifications
You must be signed in to change notification settings - Fork 2
/
bench_memblock_vs_rpcblock.py
executable file
·130 lines (111 loc) · 4.09 KB
/
bench_memblock_vs_rpcblock.py
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
#!/usr/bin/env python2.7
"""
this script bench-tests the following scenarios:
1 - constructing a bitcoin block as a dict in one go, then dropping it
2 - constructing a bitcoin block as a dict, tx by tx, then dropping it
3 - looping through all txouts in a block, then looping through all txins
(without saving any of them)
all tests averaged over 100 large blocks. all data fetched via rpc and assembled
using btc_grunt
"""
import btc_grunt
import cProfile
import pstats
import StringIO
btc_grunt.connect_to_rpc()
required_txin_info = [
"txin_hash",
"txin_index",
"txin_script",
"txin_script_list",
"txin_script_format",
"txin_sig_pubkey_validation_status"
]
required_txout_info = [
"txout_script",
"txout_script_format",
"txout_standard_script_pubkey",
"txout_standard_script_address"
]
# start at a block that is bound to have lots of txs
original_block_height = 390000
num_blocks = 1
explain_errors = True
def scenario1():
required_info = required_txin_info + required_txout_info
for i in xrange(num_blocks):
block_height = original_block_height + i
block_bytes = btc_grunt.get_block(block_height, "bytes")
parsed_block = btc_grunt.block_bin2dict(
block_bytes, block_height, required_info, explain_errors
)
def scenario2():
required_info = required_txin_info + required_txout_info
for i in xrange(num_blocks):
block_dict = {} # start again each block
block_height = original_block_height + i
block_rpc_dict = btc_grunt.get_block(block_height, "json")
for (tx_num, txhash_hex) in enumerate(block_rpc_dict["tx"]):
tx_bytes = btc_grunt.get_transaction(txhash_hex, "bytes")
(parsed_tx, _) = btc_grunt.tx_bin2dict(
tx_bytes, 0, required_info, tx_num, block_height, ["rpc"]
)
block_dict[txhash_hex] = parsed_tx
def scenario3():
for i in xrange(num_blocks):
block_height = original_block_height + i
block_rpc_dict = btc_grunt.get_block(block_height, "json")
# first loop through all txs and get txouts only
for (tx_num, txhash_hex) in enumerate(block_rpc_dict["tx"]):
# get the parsed tx - no need to save it
tx_bytes = btc_grunt.get_transaction(txhash_hex, "bytes")
(parsed_tx, _) = btc_grunt.tx_bin2dict(
tx_bytes, 0, required_txout_info, tx_num, block_height, ["rpc"]
)
# then loop through all txs and get txins only
for (tx_num, txhash_hex) in enumerate(block_rpc_dict["tx"]):
# get the parsed tx - no need to save it
tx_bytes = btc_grunt.get_transaction(txhash_hex, "bytes")
(parsed_tx, _) = btc_grunt.tx_bin2dict(
tx_bytes, 0, required_txin_info, tx_num, block_height, ["rpc"]
)
redo = False
action = "benching" if redo else "retrieving"
# thanks to
# https://zameermanji.com/blog/2012/6/30/undocumented-cprofile-features/
print "\n\nstart %s scenario 1" % action
if redo:
pr1 = cProfile.Profile()
pr1.enable()
scenario1()
pr1.disable()
pr1.dump_stats("bench_memblock.stats")
stream1 = StringIO.StringIO()
stats1 = pstats.Stats("bench_memblock.stats", stream = stream1)
stats1.strip_dirs().sort_stats("cumulative").print_stats()
print stream1.getvalue()
print "finished %s scenario 1" % action
print "\n\nstart %s scenario 2" % action
if redo:
pr2 = cProfile.Profile()
pr2.enable()
scenario2()
pr2.disable()
pr2.dump_stats("bench_memtxs.stats")
stream2 = StringIO.StringIO()
stats2 = pstats.Stats("bench_memtxs.stats", stream = stream2)
stats2.strip_dirs().sort_stats("cumulative").print_stats()
print stream2.getvalue()
print "finished %s scenario 2" % action
print "\n\nstart %s scenario 3" % action
if redo:
pr3 = cProfile.Profile()
pr3.enable()
scenario3()
pr3.disable()
pr3.dump_stats("bench_rpcblock.stats")
stream3 = StringIO.StringIO()
stats3 = pstats.Stats("bench_rpcblock.stats", stream = stream3)
stats3.strip_dirs().sort_stats("cumulative").print_stats()
print stream3.getvalue()
print "finished %s scenario 3" % action