Skip to content

Commit

Permalink
Don't benchmark serializing parameters (#1697)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle authored Oct 29, 2024
1 parent 1c43f7e commit 1ce6ad8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion x/contracts/runtime/import_balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestImportBalanceSendBalanceToAnotherContract(t *testing.T) {
stateManager.Balances[newInstanceAddress] = 0

// contract 2 starts with 0 balance
result, err := r.CallContract(newInstanceAddress, "balance")
result, err := r.CallContract(newInstanceAddress, "balance", nil)
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))

Expand Down
4 changes: 2 additions & 2 deletions x/contracts/runtime/import_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func BenchmarkDeployContract(b *testing.B) {
newAccount := into[codec.Address](result)

b.StopTimer()
result, err = runtime.CallContract(newAccount, "simple_call")
result, err = runtime.CallContract(newAccount, "simple_call", nil)
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))
b.StartTimer()
Expand All @@ -63,7 +63,7 @@ func TestImportContractDeployContract(t *testing.T) {

newAccount := into[codec.Address](result)

result, err = runtime.CallContract(newAccount, "simple_call")
result, err = runtime.CallContract(newAccount, "simple_call", nil)
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))
}
Expand Down
40 changes: 36 additions & 4 deletions x/contracts/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func BenchmarkRuntimeCallContractBasic(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := contract.Call("get_value")
result, err := contract.CallWithSerializedParams("get_value", nil)
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))
}
Expand All @@ -212,9 +212,11 @@ func BenchmarkRuntimeSendValue(b *testing.B) {
require.NoError(err)
contract.Runtime.StateManager.(TestStateManager).Balances[contract.Address] = consts.MaxUint64

params := test.SerializeParams(actor)

b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := contract.Call("send_balance", actor)
result, err := contract.CallWithSerializedParams("send_balance", params)
require.NoError(err)
require.True(into[bool](result))
}
Expand All @@ -235,14 +237,42 @@ func BenchmarkRuntimeBasicExternalCalls(b *testing.B) {
require.NoError(err)
addressOf := codec.CreateAddress(0, ids.GenerateTestID())

params := test.SerializeParams(counterAddress, addressOf)

b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := contract.Call("get_value", counterAddress, addressOf)
result, err := contract.CallWithSerializedParams("get_value", params)
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))
}
}

// Benchmark an NFT
func BenchmarkNFTMint(b *testing.B) {
require := require.New(b)
ctx := context.Background()

rt := newTestRuntime(ctx)
nft, err := rt.newTestContract("nft")
require.NoError(err)

_, err = nft.Call("init", "NFT", "NFT")
require.NoError(err)

actor := codec.CreateAddress(0, ids.GenerateTestID())
params := make([][]byte, b.N)

for i := 0; i < b.N; i++ {
params[i] = test.SerializeParams(actor, i)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = nft.CallWithSerializedParams("mint", params[i])
require.NoError(err)
}
}

// Benchmarks a contract that performs an AMM swap
func BenchmarkAmmSwaps(b *testing.B) {
require := require.New(b)
Expand Down Expand Up @@ -291,9 +321,11 @@ func BenchmarkAmmSwaps(b *testing.B) {
_, err = amm.WithActor(lp).Call("add_liquidity", amountMint, amountMint)
require.NoError(err)

params := test.SerializeParams(tokenX.Address, 150)

b.ResetTimer()
for i := 0; i < b.N; i++ {
received, err := amm.WithActor(swaper).Call("swap", tokenX.Address, 150)
received, err := amm.WithActor(swaper).CallWithSerializedParams("swap", params)
require.NoError(err)
require.NotZero(received)
}
Expand Down
11 changes: 8 additions & 3 deletions x/contracts/runtime/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ func (t *testRuntime) AddContract(contractID ContractID, account codec.Address,
return t.StateManager.(TestStateManager).SetAccountContract(t.Context, account, contractID)
}

func (t *testRuntime) CallContract(contract codec.Address, function string, params ...interface{}) ([]byte, error) {
func (t *testRuntime) CallContract(contract codec.Address, function string, params []byte) ([]byte, error) {
return t.callContext.CallContract(
t.Context,
&CallInfo{
Contract: contract,
State: t.StateManager,
FunctionName: function,
Params: test.SerializeParams(params...),
Params: params,
})
}

Expand Down Expand Up @@ -235,10 +235,15 @@ type testContract struct {
}

func (t *testContract) Call(function string, params ...interface{}) ([]byte, error) {
args := test.SerializeParams(params...)
return t.CallWithSerializedParams(function, args)
}

func (t *testContract) CallWithSerializedParams(function string, params []byte) ([]byte, error) {
return t.Runtime.CallContract(
t.Address,
function,
params...)
params)
}

func (t *testContract) WithStateManager(manager StateManager) *testContract {
Expand Down

0 comments on commit 1ce6ad8

Please sign in to comment.