Skip to content

Commit

Permalink
test: a pytest script to generate significant load on a FT contract (n…
Browse files Browse the repository at this point in the history
…ear#8847)

I have rewritten the contract since its initial incarnation and it seems to be working quite well so far, so this is probably a good time to land it.

This has also been structured so that its somewhat easier to adjust to different kinds of workloads (socialdb for example) in the future.

cc @Akashin @jakmeier 

Fixes near#8487
  • Loading branch information
nagisa authored Apr 21, 2023
1 parent 84c4d6d commit c9231fa
Show file tree
Hide file tree
Showing 4 changed files with 566 additions and 4 deletions.
21 changes: 19 additions & 2 deletions pytest/lib/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# Constant for 1 NEAR
NEAR_BASE = 10**24
TGAS = 10**12


class Account:
Expand All @@ -39,7 +40,7 @@ def __init__(self,
self.rpc_infos = rpc_infos
assert key.account_id
self.tx_timestamps = []
logger.info(
logger.debug(
f'Creating Account {key.account_id} {init_nonce} {self.rpc_infos[0]} {key.pk} {key.sk}'
)

Expand All @@ -62,6 +63,10 @@ def send_tx(self, signed_tx):
return self.json_rpc('broadcast_tx_async',
[base64.b64encode(signed_tx).decode('utf-8')])

def send_tx_sync(self, signed_tx):
return self.json_rpc('broadcast_tx_commit',
[base64.b64encode(signed_tx).decode('utf-8')])

def prep_tx(self):
self.tx_timestamps.append(time.time())
self.nonce += 1
Expand Down Expand Up @@ -98,10 +103,22 @@ def send_call_contract_raw_tx(self,
base_block_hash=None):
self.prep_tx()
tx = sign_function_call_tx(self.key, contract_id, method_name, args,
3 * 10**14, deposit, self.nonce,
300 * TGAS, deposit, self.nonce,
base_block_hash or self.base_block_hash)
return self.send_tx(tx)

def send_call_contract_raw_tx_sync(self,
contract_id,
method_name,
args,
deposit,
base_block_hash=None):
self.prep_tx()
tx = sign_function_call_tx(self.key, contract_id, method_name, args,
300 * TGAS, deposit, self.nonce,
base_block_hash or self.base_block_hash)
return self.send_tx_sync(tx)

def send_create_account_tx(self, new_account_id, base_block_hash=None):
self.prep_tx()
new_key = Key(new_account_id, self.key.pk, self.key.sk)
Expand Down
2 changes: 1 addition & 1 deletion pytest/lib/configured_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def new_logger(
name: Optional[str] = None,
level: LogLevel = logging.DEBUG,
level: LogLevel = logging.INFO,
outfile: Optional[str] = None,
stderr: Optional[bool] = None,
) -> logging.Logger:
Expand Down
22 changes: 21 additions & 1 deletion pytest/tests/loadtest/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Loadtest
# Loadtest

This test requires a few steps. Firstly, build the binary:

Expand All @@ -23,3 +23,23 @@ And lastly, run the test:
```shell
python3 pytest/tests/loadtest/loadtest.py --home ~/.near_tmp --num_accounts=5 --num_requests=1000
```

# Load Test version 2

The newer loadtest2.py script currently runs an intense load test with the FT contract.

Much like with the earlier version you will want to build a `neard`. This script can set up a (2
node) cluster for you (nice for testing):

```
env NEAR_ROOT=../target/release/ python3 tests/loadtest/loadtest2.py --fungible-token-wasm=$PWD/../../FT/res/fungible_token.wasm --setup-cluster --accounts=1000 --executors=4
```

Or, you can set up a network yourself, and point the script at your local node’s RPC endpoint:

```
env NEAR_ROOT=../target/release/ python3 tests/stress/perf_ft_transfer.py --fungible-token-wasm=$PWD/../../FT/res/fungible_token.wasm --accounts=1000 --executors=4 --contract-key=~/.near/node.json
```

As seen in commands above, you will need a fungible token contract to test with. There's one you
can get from the `near/near-examples` repository.
Loading

0 comments on commit c9231fa

Please sign in to comment.