Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
all: add benchmarks to core components
Browse files Browse the repository at this point in the history
Fixes #480
  • Loading branch information
Cuong Manh Le authored and cuonglm committed Aug 26, 2021
1 parent 5eb3458 commit 26c2dde
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
44 changes: 44 additions & 0 deletions app/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
34 changes: 34 additions & 0 deletions crypto/ethsecp256k1/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions crypto/hd/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions crypto/hd/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions types/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 26c2dde

Please sign in to comment.