forked from FISCO-BCOS/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_perf.py
83 lines (73 loc) · 2.76 KB
/
demo_perf.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
'''
bcosliteclientpy is a python client for FISCO BCOS2.0 (https://github.com/FISCO-BCOS/)
bcosliteclientpy is free software: you can redistribute it and/or modify it under the
terms of the MIT License as published by the Free Software Foundation. This project is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Thanks for
authors and contributors of eth-abi, eth-account, eth-hash,eth-keys, eth-typing, eth-utils,
rlp, eth-rlp , hexbytes ... and relative projects
@file: transaction_common.py
@function:
@author: yujiechen
@date: 2019-07
'''
from client.common.transaction_common import TransactionCommon
from concurrent.futures import ThreadPoolExecutor
from client.bcoserror import BcosException, BcosError
import multiprocessing
import random
import sys
contract_addr = ""
contract_path = "contracts"
contract_name = "Ok"
process_num = 8
total_count = int(sys.argv[1])
step = int(total_count / process_num)
def sendRequest(client):
try:
# generate random number
trans_num = random.randint(1, 1000)
fn_name = "trans"
fn_args = [trans_num]
client.send_transaction_getReceipt(fn_name, fn_args)
except BcosException as e:
print("call trans contract failed, reason: {}".format(e))
except BcosError as e:
print("call trans contract failed, reason: {}".format(e))
def sendMultiRequests(process_id):
"""
send trans request
"""
client = TransactionCommon("",
contract_path,
contract_name)
executor = ThreadPoolExecutor(max_workers=10)
for i in range(0, step):
executor.submit(sendRequest, (client))
print("process {}, send {}%, tx_num: {}".format(process_id, step * 100 / total_count, step))
executor.shutdown(True)
print("\t\t process {}, receive {} %, tx_num:{}".format(
process_id, step * 100 / total_count, step))
def main(argv):
"""
1. deploy Ok.sol
2. send transactions: set
"""
# deploy Ok contract
gasPrice = 30000000
client = TransactionCommon("",
contract_path,
contract_name)
result = client.send_transaction_getReceipt(None, None, gasPrice, True)[0]
contract_addr = result['contractAddress']
client.set_contract_addr(contract_addr)
pool = multiprocessing.Pool(process_num)
args_list = []
for i in range(0, process_num):
args_list.append(i)
pool.map(sendMultiRequests, args_list)
pool.close()
pool.join()
client.finish()
if __name__ == "__main__":
main(sys.argv[1:])