2021-08-31 12:50:31 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/tests"
|
2021-08-31 12:50:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkCreateAccountNew(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-08-31 12:50:31 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
addr := tests.GenerateAddress()
|
|
|
|
b.StartTimer()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.CreateAccount(addr)
|
2021-08-31 12:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCreateAccountExisting(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-08-31 12:50:31 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.CreateAccount(suite.address)
|
2021-08-31 12:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAddBalance(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-08-31 12:50:31 +00:00
|
|
|
|
|
|
|
amt := big.NewInt(10)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.AddBalance(suite.address, amt)
|
2021-08-31 12:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSetCode(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-08-31 12:50:31 +00:00
|
|
|
|
|
|
|
hash := crypto.Keccak256Hash([]byte("code")).Bytes()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.SetCode(suite.address, hash)
|
2021-08-31 12:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSetState(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-08-31 12:50:31 +00:00
|
|
|
|
|
|
|
hash := crypto.Keccak256Hash([]byte("topic")).Bytes()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.SetCode(suite.address, hash)
|
2021-08-31 12:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAddLog(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-08-31 12:50:31 +00:00
|
|
|
|
|
|
|
topic := crypto.Keccak256Hash([]byte("topic"))
|
|
|
|
txHash := crypto.Keccak256Hash([]byte("tx_hash"))
|
|
|
|
blockHash := crypto.Keccak256Hash([]byte("block_hash"))
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.AddLog(ðtypes.Log{
|
2021-08-31 12:50:31 +00:00
|
|
|
Address: suite.address,
|
|
|
|
Topics: []common.Hash{topic},
|
|
|
|
Data: []byte("data"),
|
|
|
|
BlockNumber: 1,
|
|
|
|
TxHash: txHash,
|
|
|
|
TxIndex: 1,
|
|
|
|
BlockHash: blockHash,
|
|
|
|
Index: 1,
|
|
|
|
Removed: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSnapshot(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-08-31 12:50:31 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
target := vmdb.Snapshot()
|
2021-08-31 12:50:31 +00:00
|
|
|
require.Equal(b, i, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := b.N - 1; i >= 0; i-- {
|
|
|
|
require.NotPanics(b, func() {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.RevertToSnapshot(i)
|
2021-08-31 12:50:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
func BenchmarkSubBalance(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
amt := big.NewInt(10)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.SubBalance(suite.address, amt)
|
2021-09-24 15:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSetNonce(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.SetNonce(suite.address, 1)
|
2021-09-24 15:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAddRefund(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.AddRefund(1)
|
2021-09-24 15:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSuicide(b *testing.B) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
addr := tests.GenerateAddress()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.CreateAccount(addr)
|
2021-09-24 15:58:22 +00:00
|
|
|
b.StartTimer()
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.Suicide(addr)
|
2021-09-24 15:58:22 +00:00
|
|
|
}
|
|
|
|
}
|