Skip to content

Commit

Permalink
all: ensure b.ReportAllocs() in all the benchmarks
Browse files Browse the repository at this point in the history
With this change, we'll get details on the number of
allocations performed by code. Later on when we have
continuous benchmarking infrastructure, this change
will prove useful to flag regressions.

Fixes #8459
  • Loading branch information
odeke-em committed Jan 27, 2021
1 parent e2f510a commit ee2085e
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crypto/armor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func TestUnarmorInfoBytesErrors(t *testing.T) {
}

func BenchmarkBcryptGenerateFromPassword(b *testing.B) {
b.ReportAllocs()

passphrase := []byte("passphrase")
for securityParam := 9; securityParam < 16; securityParam++ {
param := securityParam
Expand Down
1 change: 1 addition & 0 deletions crypto/keys/internal/benchmarking/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (zeroReader) Read(buf []byte) (int, error) {
// BenchmarkKeyGeneration benchmarks the given key generation algorithm using
// a dummy reader.
func BenchmarkKeyGeneration(b *testing.B, generateKey func(reader io.Reader) types.PrivKey) {
b.ReportAllocs()
var zero zeroReader
for i := 0; i < b.N; i++ {
generateKey(zero)
Expand Down
3 changes: 3 additions & 0 deletions crypto/keys/secp256k1/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func BenchmarkKeyGeneration(b *testing.B) {
b.ReportAllocs()
benchmarkKeygenWrapper := func(reader io.Reader) types.PrivKey {
priv := genPrivKey(reader)
return &PrivKey{Key: priv}
Expand All @@ -17,11 +18,13 @@ func BenchmarkKeyGeneration(b *testing.B) {
}

func BenchmarkSigning(b *testing.B) {
b.ReportAllocs()
priv := GenPrivKey()
benchmarking.BenchmarkSigning(b, priv)
}

func BenchmarkVerification(b *testing.B) {
b.ReportAllocs()
priv := GenPrivKey()
benchmarking.BenchmarkVerification(b, priv)
}
2 changes: 2 additions & 0 deletions simapp/sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// Profile with:
// /usr/local/go/bin/go test -benchmem -run=^$ github.com/cosmos/cosmos-sdk/simapp -bench ^BenchmarkFullAppSimulation$ -Commit=true -cpuprofile cpu.out
func BenchmarkFullAppSimulation(b *testing.B) {
b.ReportAllocs()
config, db, dir, logger, _, err := SetupSimulation("goleveldb-app-sim", "Simulation")
if err != nil {
b.Fatalf("simulation setup failed: %s", err.Error())
Expand Down Expand Up @@ -57,6 +58,7 @@ func BenchmarkFullAppSimulation(b *testing.B) {
}

func BenchmarkInvariants(b *testing.B) {
b.ReportAllocs()
config, db, dir, logger, _, err := SetupSimulation("leveldb-app-invariant-bench", "Simulation")
if err != nil {
b.Fatalf("simulation setup failed: %s", err.Error())
Expand Down
1 change: 1 addition & 0 deletions store/cache/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func populate(mgr *CommitKVStoreCacheManager) {
}

func BenchmarkReset(b *testing.B) {
b.ReportAllocs()
mgr := freshMgr()

b.ResetTimer()
Expand Down
1 change: 1 addition & 0 deletions store/cachekv/store_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func benchmarkCacheKVStoreIterator(numKVs int, b *testing.B) {
b.ReportAllocs()
mem := dbadapter.Store{DB: dbm.NewMemDB()}
cstore := cachekv.NewStore(mem)
keys := make([]string, numKVs)
Expand Down
2 changes: 2 additions & 0 deletions store/cachekv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ func (krc *keyRangeCounter) key() int {
func bz(s string) []byte { return []byte(s) }

func BenchmarkCacheKVStoreGetNoKeyFound(b *testing.B) {
b.ReportAllocs()
st := newCacheKVStore()
b.ResetTimer()
// assumes b.N < 2**24
Expand All @@ -525,6 +526,7 @@ func BenchmarkCacheKVStoreGetNoKeyFound(b *testing.B) {
}

func BenchmarkCacheKVStoreGetKeyFound(b *testing.B) {
b.ReportAllocs()
st := newCacheKVStore()
for i := 0; i < b.N; i++ {
arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)}
Expand Down
1 change: 1 addition & 0 deletions store/iavl/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ func TestIAVLStoreQuery(t *testing.T) {
}

func BenchmarkIAVLIteratorNext(b *testing.B) {
b.ReportAllocs()
db := dbm.NewMemDB()
treeSize := 1000
tree, err := iavl.NewMutableTree(db, cacheSize)
Expand Down
2 changes: 2 additions & 0 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ func BenchmarkMultistoreSnapshotRestore1M(b *testing.B) {
}

func benchmarkMultistoreSnapshot(b *testing.B, stores uint8, storeKeys uint64) {
b.ReportAllocs()
b.StopTimer()
source := newMultiStoreWithGeneratedData(dbm.NewMemDB(), stores, storeKeys)
version := source.LastCommitID().Version
Expand Down Expand Up @@ -716,6 +717,7 @@ func benchmarkMultistoreSnapshot(b *testing.B, stores uint8, storeKeys uint64) {
}

func benchmarkMultistoreSnapshotRestore(b *testing.B, stores uint8, storeKeys uint64) {
b.ReportAllocs()
b.StopTimer()
source := newMultiStoreWithGeneratedData(dbm.NewMemDB(), stores, storeKeys)
version := uint64(source.LastCommitID().Version)
Expand Down
2 changes: 2 additions & 0 deletions types/address_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func BenchmarkBech32ifyPubKey(b *testing.B) {
b.ReportAllocs()
pkBz := make([]byte, ed25519.PubKeySize)
pk := &ed25519.PubKey{Key: pkBz}
rng := rand.New(rand.NewSource(time.Now().Unix()))
Expand All @@ -29,6 +30,7 @@ func BenchmarkBech32ifyPubKey(b *testing.B) {
}

func BenchmarkGetPubKeyFromBech32(b *testing.B) {
b.ReportAllocs()
pkBz := make([]byte, ed25519.PubKeySize)
pk := &ed25519.PubKey{Key: pkBz}
rng := rand.New(rand.NewSource(time.Now().Unix()))
Expand Down
2 changes: 2 additions & 0 deletions types/coin_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func coinName(suffix int) string {
}

func BenchmarkCoinsAdditionIntersect(b *testing.B) {
b.ReportAllocs()
benchmarkingFunc := func(numCoinsA int, numCoinsB int) func(b *testing.B) {
return func(b *testing.B) {
coinsA := Coins(make([]Coin, numCoinsA))
Expand Down Expand Up @@ -39,6 +40,7 @@ func BenchmarkCoinsAdditionIntersect(b *testing.B) {
}

func BenchmarkCoinsAdditionNoIntersect(b *testing.B) {
b.ReportAllocs()
benchmarkingFunc := func(numCoinsA int, numCoinsB int) func(b *testing.B) {
return func(b *testing.B) {
coinsA := Coins(make([]Coin, numCoinsA))
Expand Down
1 change: 1 addition & 0 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ func (s *decimalTestSuite) TestOperationOrders() {
}

func BenchmarkMarshalTo(b *testing.B) {
b.ReportAllocs()
bis := []struct {
in sdk.Dec
want []byte
Expand Down
1 change: 1 addition & 0 deletions x/auth/keeper/keeper_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func BenchmarkAccountMapperGetAccountFound(b *testing.B) {
b.ReportAllocs()
app, ctx := createTestApp(false)

// assumes b.N < 2**24
Expand Down
1 change: 1 addition & 0 deletions x/bank/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
var moduleAccAddr = authtypes.NewModuleAddress(stakingtypes.BondedPoolName)

func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
b.ReportAllocs()
// Add an account at genesis
acc := authtypes.BaseAccount{
Address: addr1.String(),
Expand Down
3 changes: 3 additions & 0 deletions x/mint/types/minter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestBlockProvision(t *testing.T) {
// using sdk.Dec operations: (current implementation)
// BenchmarkBlockProvision-4 3000000 429 ns/op
func BenchmarkBlockProvision(b *testing.B) {
b.ReportAllocs()
minter := InitialMinter(sdk.NewDecWithPrec(1, 1))
params := DefaultParams()

Expand All @@ -105,6 +106,7 @@ func BenchmarkBlockProvision(b *testing.B) {
// Next inflation benchmarking
// BenchmarkNextInflation-4 1000000 1828 ns/op
func BenchmarkNextInflation(b *testing.B) {
b.ReportAllocs()
minter := InitialMinter(sdk.NewDecWithPrec(1, 1))
params := DefaultParams()
bondedRatio := sdk.NewDecWithPrec(1, 1)
Expand All @@ -119,6 +121,7 @@ func BenchmarkNextInflation(b *testing.B) {
// Next annual provisions benchmarking
// BenchmarkNextAnnualProvisions-4 5000000 251 ns/op
func BenchmarkNextAnnualProvisions(b *testing.B) {
b.ReportAllocs()
minter := InitialMinter(sdk.NewDecWithPrec(1, 1))
params := DefaultParams()
totalSupply := sdk.NewInt(100000000000000)
Expand Down

0 comments on commit ee2085e

Please sign in to comment.