Skip to content

Commit 1ce6ad8

Browse files
Don't benchmark serializing parameters (#1697)
1 parent 1c43f7e commit 1ce6ad8

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

x/contracts/runtime/import_balance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestImportBalanceSendBalanceToAnotherContract(t *testing.T) {
3333
stateManager.Balances[newInstanceAddress] = 0
3434

3535
// contract 2 starts with 0 balance
36-
result, err := r.CallContract(newInstanceAddress, "balance")
36+
result, err := r.CallContract(newInstanceAddress, "balance", nil)
3737
require.NoError(err)
3838
require.Equal(uint64(0), into[uint64](result))
3939

x/contracts/runtime/import_contract_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func BenchmarkDeployContract(b *testing.B) {
3636
newAccount := into[codec.Address](result)
3737

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

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

66-
result, err = runtime.CallContract(newAccount, "simple_call")
66+
result, err = runtime.CallContract(newAccount, "simple_call", nil)
6767
require.NoError(err)
6868
require.Equal(uint64(0), into[uint64](result))
6969
}

x/contracts/runtime/runtime_test.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func BenchmarkRuntimeCallContractBasic(b *testing.B) {
194194

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

215+
params := test.SerializeParams(actor)
216+
215217
b.ResetTimer()
216218
for i := 0; i < b.N; i++ {
217-
result, err := contract.Call("send_balance", actor)
219+
result, err := contract.CallWithSerializedParams("send_balance", params)
218220
require.NoError(err)
219221
require.True(into[bool](result))
220222
}
@@ -235,14 +237,42 @@ func BenchmarkRuntimeBasicExternalCalls(b *testing.B) {
235237
require.NoError(err)
236238
addressOf := codec.CreateAddress(0, ids.GenerateTestID())
237239

240+
params := test.SerializeParams(counterAddress, addressOf)
241+
238242
b.ResetTimer()
239243
for i := 0; i < b.N; i++ {
240-
result, err := contract.Call("get_value", counterAddress, addressOf)
244+
result, err := contract.CallWithSerializedParams("get_value", params)
241245
require.NoError(err)
242246
require.Equal(uint64(0), into[uint64](result))
243247
}
244248
}
245249

250+
// Benchmark an NFT
251+
func BenchmarkNFTMint(b *testing.B) {
252+
require := require.New(b)
253+
ctx := context.Background()
254+
255+
rt := newTestRuntime(ctx)
256+
nft, err := rt.newTestContract("nft")
257+
require.NoError(err)
258+
259+
_, err = nft.Call("init", "NFT", "NFT")
260+
require.NoError(err)
261+
262+
actor := codec.CreateAddress(0, ids.GenerateTestID())
263+
params := make([][]byte, b.N)
264+
265+
for i := 0; i < b.N; i++ {
266+
params[i] = test.SerializeParams(actor, i)
267+
}
268+
269+
b.ResetTimer()
270+
for i := 0; i < b.N; i++ {
271+
_, err = nft.CallWithSerializedParams("mint", params[i])
272+
require.NoError(err)
273+
}
274+
}
275+
246276
// Benchmarks a contract that performs an AMM swap
247277
func BenchmarkAmmSwaps(b *testing.B) {
248278
require := require.New(b)
@@ -291,9 +321,11 @@ func BenchmarkAmmSwaps(b *testing.B) {
291321
_, err = amm.WithActor(lp).Call("add_liquidity", amountMint, amountMint)
292322
require.NoError(err)
293323

324+
params := test.SerializeParams(tokenX.Address, 150)
325+
294326
b.ResetTimer()
295327
for i := 0; i < b.N; i++ {
296-
received, err := amm.WithActor(swaper).Call("swap", tokenX.Address, 150)
328+
received, err := amm.WithActor(swaper).CallWithSerializedParams("swap", params)
297329
require.NoError(err)
298330
require.NotZero(received)
299331
}

x/contracts/runtime/util_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ func (t *testRuntime) AddContract(contractID ContractID, account codec.Address,
186186
return t.StateManager.(TestStateManager).SetAccountContract(t.Context, account, contractID)
187187
}
188188

189-
func (t *testRuntime) CallContract(contract codec.Address, function string, params ...interface{}) ([]byte, error) {
189+
func (t *testRuntime) CallContract(contract codec.Address, function string, params []byte) ([]byte, error) {
190190
return t.callContext.CallContract(
191191
t.Context,
192192
&CallInfo{
193193
Contract: contract,
194194
State: t.StateManager,
195195
FunctionName: function,
196-
Params: test.SerializeParams(params...),
196+
Params: params,
197197
})
198198
}
199199

@@ -235,10 +235,15 @@ type testContract struct {
235235
}
236236

237237
func (t *testContract) Call(function string, params ...interface{}) ([]byte, error) {
238+
args := test.SerializeParams(params...)
239+
return t.CallWithSerializedParams(function, args)
240+
}
241+
242+
func (t *testContract) CallWithSerializedParams(function string, params []byte) ([]byte, error) {
238243
return t.Runtime.CallContract(
239244
t.Address,
240245
function,
241-
params...)
246+
params)
242247
}
243248

244249
func (t *testContract) WithStateManager(manager StateManager) *testContract {

0 commit comments

Comments
 (0)