From 4b11ac66c4087f67b9715f03f329cd971aef2fd2 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 26 Aug 2021 15:20:27 +0700 Subject: [PATCH] all: add benchmarks to core components (#493) Fixes #480 --- app/benchmark_test.go | 44 +++++++++++++++++++++++++++ crypto/ethsecp256k1/benchmark_test.go | 34 +++++++++++++++++++++ crypto/hd/algorithm_test.go | 4 +-- crypto/hd/benchmark_test.go | 31 +++++++++++++++++++ types/benchmark_test.go | 17 +++++++++++ 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 app/benchmark_test.go create mode 100644 crypto/ethsecp256k1/benchmark_test.go create mode 100644 crypto/hd/benchmark_test.go create mode 100644 types/benchmark_test.go diff --git a/app/benchmark_test.go b/app/benchmark_test.go new file mode 100644 index 00000000..a88712e7 --- /dev/null +++ b/app/benchmark_test.go @@ -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) + } + } +} diff --git a/crypto/ethsecp256k1/benchmark_test.go b/crypto/ethsecp256k1/benchmark_test.go new file mode 100644 index 00000000..815cc583 --- /dev/null +++ b/crypto/ethsecp256k1/benchmark_test.go @@ -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) + } +} diff --git a/crypto/hd/algorithm_test.go b/crypto/hd/algorithm_test.go index 48eaa71a..8d2eda77 100644 --- a/crypto/hd/algorithm_test.go +++ b/crypto/hd/algorithm_test.go @@ -22,6 +22,8 @@ func init() { 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) { dir := t.TempDir() mockIn := strings.NewReader("") @@ -66,8 +68,6 @@ func TestKeyring(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) require.NoError(t, err) require.NotEmpty(t, bz) diff --git a/crypto/hd/benchmark_test.go b/crypto/hd/benchmark_test.go new file mode 100644 index 00000000..0ad376ce --- /dev/null +++ b/crypto/hd/benchmark_test.go @@ -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++ { + (ðSecp256k1Algo{}).Generate()(bz) + } +} diff --git a/types/benchmark_test.go b/types/benchmark_test.go new file mode 100644 index 00000000..23d7790a --- /dev/null +++ b/types/benchmark_test.go @@ -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) + } + } +}