-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pytest: refactor mocknet load_testing_helper to track the nonce local…
…ly (#3103) Previously, load tests would query the nodes for the account nonce on each transaction. This put an additional burden on the nodes which is likely unrealistic. Here, we introduce an `Account` object to track the nonce locally in the test, and thus not query the nodes. Additionally, the `Account` object tracks the timestamps of when it sends transactions, allowing for detailed measurements of input transactions per second (though we do not yet make use of this feature in this change). Note: this does not change the result of running the load test (i.e. tps and bps are not effected by this change), so the nonce querying likely had little impact on the node performance. However, I would still like to make this change since `Account` is a useful, reusable object.
- Loading branch information
Showing
2 changed files
with
107 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import base64 | ||
import json | ||
import requests | ||
import time | ||
|
||
from cluster import Key | ||
from transaction import ( | ||
sign_payment_tx, sign_deploy_contract_tx, sign_function_call_tx, | ||
sign_create_account_with_full_access_key_and_balance_tx, sign_staking_tx) | ||
import utils | ||
|
||
|
||
class Account: | ||
|
||
def __init__(self, key, init_nonce, base_block_hash, rpc_info): | ||
self.key = key | ||
self.nonce = init_nonce | ||
self.base_block_hash = base_block_hash | ||
self.rpc_addr, self.rpc_port = rpc_info | ||
self.tx_timestamps = [] | ||
|
||
def json_rpc(self, method, params): | ||
j = { | ||
'method': method, | ||
'params': params, | ||
'id': 'dontcare', | ||
'jsonrpc': '2.0' | ||
} | ||
r = requests.post(f'http://{self.rpc_addr}:{self.rpc_port}', | ||
json=j, | ||
timeout=10) | ||
return json.loads(r.content) | ||
|
||
def send_tx(self, signed_tx): | ||
return self.json_rpc('broadcast_tx_async', | ||
[base64.b64encode(signed_tx).decode('utf8')]) | ||
|
||
def prep_tx(self): | ||
self.tx_timestamps.append(time.time()) | ||
self.nonce += 1 | ||
|
||
def send_transfer_tx(self, dest_account_id): | ||
self.prep_tx() | ||
transfer_amount = 100 | ||
tx = sign_payment_tx(self.key, dest_account_id, transfer_amount, | ||
self.nonce, self.base_block_hash) | ||
return self.send_tx(tx) | ||
|
||
def send_deploy_contract_tx(self, wasm_filename): | ||
wasm_binary = utils.load_binary_file(wasm_filename) | ||
self.prep_tx() | ||
tx = sign_deploy_contract_tx(self.key, wasm_binary, self.nonce, | ||
self.base_block_hash) | ||
return self.send_tx(tx) | ||
|
||
def send_call_contract_tx(self, method_name, args): | ||
self.prep_tx() | ||
tx = sign_function_call_tx(self.key, self.key.account_id, method_name, | ||
args, 300000000000000, 0, self.nonce, | ||
self.base_block_hash) | ||
return self.send_tx(tx) | ||
|
||
def send_create_account_tx(self, new_account_id): | ||
self.prep_tx() | ||
new_key = Key(new_account_id, self.key.pk, self.key.sk) | ||
tx = sign_create_account_with_full_access_key_and_balance_tx( | ||
self.key, new_account_id, new_key, 100, self.nonce, | ||
self.base_block_hash) | ||
return self.send_tx(tx) | ||
|
||
def send_stake_tx(self, stake_amount): | ||
self.prep_tx() | ||
tx = sign_staking_tx(self.key, self.key, stake_amount, self.nonce, | ||
self.base_block_hash) | ||
return self.send_tx(tx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters