all: add benchmarks to core components (#493)

Fixes #480
This commit is contained in:
Cuong Manh Le 2021-08-26 15:20:27 +07:00 committed by GitHub
parent 5eb3458d11
commit 4b11ac66c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 128 additions and 2 deletions

44
app/benchmark_test.go Normal file
View File

@ -0,0 +1,44 @@
package app
import (
"encoding/json"
"io"
"testing"
"github.com/cosmos/cosmos-sdk/simapp"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
"github.com/tharsis/ethermint/encoding"
)
func BenchmarkEthermintApp_ExportAppStateAndValidators(b *testing.B) {
db := dbm.NewMemDB()
app := NewEthermintApp(log.NewTMLogger(io.Discard), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
genesisState := NewDefaultGenesisState()
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
b.Fatal(err)
}
// Initialize the chain
app.InitChain(
abci.RequestInitChain{
ChainId: "ethermint_9000-1",
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)
app.Commit()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Making a new app object with the db, so that initchain hasn't been called
app2 := NewEthermintApp(log.NewTMLogger(log.NewSyncWriter(io.Discard)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
if _, err := app2.ExportAppStateAndValidators(false, []string{}); err != nil {
b.Fatal(err)
}
}
}

View File

@ -0,0 +1,34 @@
package ethsecp256k1
import (
"fmt"
"testing"
)
func BenchmarkGenerateKey(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := GenerateKey(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkPubKey_VerifySignature(b *testing.B) {
privKey, err := GenerateKey()
if err != nil {
b.Fatal(err)
}
pubKey := privKey.PubKey()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
msg := []byte(fmt.Sprintf("%10d", i))
sig, err := privKey.Sign(msg)
if err != nil {
b.Fatal(err)
}
pubKey.VerifySignature(msg, sig)
}
}

View File

@ -22,6 +22,8 @@ func init() {
cryptocodec.RegisterCrypto(amino) cryptocodec.RegisterCrypto(amino)
} }
const mnemonic = "picnic rent average infant boat squirrel federal assault mercy purity very motor fossil wheel verify upset box fresh horse vivid copy predict square regret"
func TestKeyring(t *testing.T) { func TestKeyring(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
mockIn := strings.NewReader("") mockIn := strings.NewReader("")
@ -66,8 +68,6 @@ func TestKeyring(t *testing.T) {
} }
func TestDerivation(t *testing.T) { func TestDerivation(t *testing.T) {
mnemonic := "picnic rent average infant boat squirrel federal assault mercy purity very motor fossil wheel verify upset box fresh horse vivid copy predict square regret"
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, ethermint.BIP44HDPath) bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, ethermint.BIP44HDPath)
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, bz) require.NotEmpty(t, bz)

View File

@ -0,0 +1,31 @@
package hd
import (
"testing"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
ethermint "github.com/tharsis/ethermint/types"
)
func BenchmarkEthSecp256k1Algo_Derive(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
deriveFn := EthSecp256k1.Derive()
if _, err := deriveFn(mnemonic, keyring.DefaultBIP39Passphrase, ethermint.BIP44HDPath); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkEthSecp256k1Algo_Generate(b *testing.B) {
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, ethermint.BIP44HDPath)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
(&ethSecp256k1Algo{}).Generate()(bz)
}
}

17
types/benchmark_test.go Normal file
View File

@ -0,0 +1,17 @@
package types
import (
"fmt"
"testing"
)
func BenchmarkParseChainID(b *testing.B) {
b.ReportAllocs()
// Start at 1, for valid EIP155, see regexEIP155 variable.
for i := 1; i < b.N; i++ {
chainID := fmt.Sprintf("ethermint_1-%d", i)
if _, err := ParseChainID(chainID); err != nil {
b.Fatal(err)
}
}
}