Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call Contract Benchmarks #1696

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions x/contracts/runtime/import_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@ import (
"github.com/ava-labs/hypersdk/codec"
)

func BenchmarkDeployContract(b *testing.B) {
require := require.New(b)

ctx := context.Background()
rt := newTestRuntime(ctx)
contract, err := rt.newTestContract("deploy_contract")
require.NoError(err)

runtime := contract.Runtime
otherContractID := ids.GenerateTestID()
err = runtime.AddContract(otherContractID[:], codec.CreateAddress(0, otherContractID), "call_contract")
require.NoError(err)

b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := contract.Call(
"deploy",
otherContractID[:])
require.NoError(err)

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

b.StopTimer()
result, err = runtime.CallContract(newAccount, "simple_call")
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))
b.StartTimer()
}
}

func TestImportContractDeployContract(t *testing.T) {
require := require.New(t)
ctx := context.Background()
Expand Down
172 changes: 171 additions & 1 deletion x/contracts/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,179 @@

"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/x/contracts/test"
)

// Benchmarks calling a contract that returns 0 immediately
// Benchmarks the time it takes to get a module when it is not cached
// BenchmarkRuntimeModuleNotCached-10 145 10262426 ns/op 245882 B/op 9 allocs/op
func BenchmarkRuntimeModuleNotCached(b *testing.B) {
require := require.New(b)

ctx := context.Background()
rt := newTestRuntime(ctx)

contract, err := rt.newTestContract("simple")
require.NoError(err)

callInfo := &CallInfo{
Contract: contract.Address,
State: rt.StateManager,
FunctionName: "get_value",
Params: test.SerializeParams(),
}
newInfo, err := rt.callContext.createCallInfo(callInfo)
require.NoError(err)
programID, err := callInfo.State.GetAccountContract(ctx, newInfo.Contract)
require.NoError(err)

b.ResetTimer()
for i := 0; i < b.N; i++ {

Check failure on line 41 in x/contracts/runtime/runtime_test.go

View workflow job for this annotation

GitHub Actions / hypersdk-lint

empty-lines: extra empty line at the end of a block (revive)
_, err = rt.callContext.r.getModule(ctx, newInfo, programID)
require.NoError(err)

b.StopTimer()
// reset module
rt.callContext.r.contractCache.Flush()
b.StartTimer()

}

Check failure on line 50 in x/contracts/runtime/runtime_test.go

View workflow job for this annotation

GitHub Actions / hypersdk-lint

unnecessary trailing newline (whitespace)
}

// Benchmarks the time it takes to get a module when it is cached
// BenchmarkRuntimeModuleCached-10 2429497 495.1 ns/op 32 B/op 1 allocs/op
func BenchmarkRuntimeModuleCached(b *testing.B) {
require := require.New(b)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

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

result, err := contract.Call("get_value")
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))

b.ResetTimer()

callInfo := &CallInfo{
Contract: contract.Address,
State: rt.StateManager,
FunctionName: "get_value",
Params: test.SerializeParams(),
}
newInfo, err := rt.callContext.createCallInfo(callInfo)
require.NoError(err)
programID, err := callInfo.State.GetAccountContract(ctx, newInfo.Contract)
require.NoError(err)

for i := 0; i < b.N; i++ {
_, err = rt.callContext.r.getModule(ctx, newInfo, programID)
require.NoError(err)
}
}

// Benchmarks the time it takes to get an instance which happens on every contract call
// Wasmitime rust allows you to pre-instantiate the module, which is not implemented in go
//
// https://docs.rs/wasmtime/latest/wasmtime/struct.Linker.html#method.instantiate_pre
//
// BenchmarkRuntimeInstance-10 48392 28894 ns/op 265 B/op 15 allocs/op
func BenchmarkRuntimeInstance(b *testing.B) {
require := require.New(b)

ctx := context.Background()
rt := newTestRuntime(ctx)
contract, err := rt.newTestContract("simple")
require.NoError(err)

result, err := contract.Call("get_value")
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))

b.ResetTimer()

callInfo := &CallInfo{
Contract: contract.Address,
State: rt.StateManager,
FunctionName: "get_value",
Params: test.SerializeParams(),
}
newInfo, err := rt.callContext.createCallInfo(callInfo)
require.NoError(err)
programID, err := newInfo.State.GetAccountContract(ctx, newInfo.Contract)
require.NoError(err)

module, err := rt.callContext.r.getModule(ctx, newInfo, programID)
require.NoError(err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
inst, err := rt.callContext.r.getInstance(module, rt.callContext.r.hostImports)
require.NoError(err)
_ = inst

b.StopTimer()
// reset module
module, err = rt.callContext.r.getModule(ctx, newInfo, programID)
require.NoError(err)
b.StartTimer()
}
}

// Benchmarks the time it takes to call a contract with everything instantiated
// BenchmarkRuntimeInstanceCall-10 32728 33143 ns/op 1629 B/op 191 allocs/op
func BenchmarkRuntimeInstanceCall(b *testing.B) {
require := require.New(b)
ctx := context.Background()

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

result, err := contract.Call("get_value")
require.NoError(err)
require.Equal(uint64(0), into[uint64](result))

b.ResetTimer()

callInfo := &CallInfo{
Contract: contract.Address,
State: rt.StateManager,
FunctionName: "get_value",
Params: test.SerializeParams(),
}
newInfo, err := rt.callContext.createCallInfo(callInfo)
require.NoError(err)
programID, err := newInfo.State.GetAccountContract(ctx, newInfo.Contract)
require.NoError(err)

module, err := rt.callContext.r.getModule(ctx, newInfo, programID)
require.NoError(err)
inst, err := rt.callContext.r.getInstance(module, rt.callContext.r.hostImports)
require.NoError(err)
b.ResetTimer()
newInfo.inst = inst
rt.callContext.r.setCallInfo(inst.store, newInfo)

for i := 0; i < b.N; i++ {
result, err := inst.call(ctx, newInfo)
require.NoError(err)
_ = result

rt.callContext.r.deleteCallInfo(inst.store)
b.StopTimer()
// reset module
module, err = rt.callContext.r.getModule(ctx, newInfo, programID)
require.NoError(err)
inst, err = rt.callContext.r.getInstance(module, rt.callContext.r.hostImports)
require.NoError(err)
newInfo.inst = inst
b.StartTimer()
rt.callContext.r.setCallInfo(inst.store, newInfo)
}
}

func BenchmarkRuntimeCallContractBasic(b *testing.B) {
require := require.New(b)
ctx := context.Background()
Expand Down
Loading