From c6a3928d37530b61f6a5ba1e2f260af1042158c2 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Tue, 18 Sep 2018 20:00:49 -0700 Subject: [PATCH] Added a couple of benchmarks (#2353) These method were surprisingly slow from the profiling. (Mapper.Get accounted for 2.7% of time taken, GetKey also took an interestingly large portion of time) --- store/cachekvstore_test.go | 22 ++++++++++++++++++++++ x/auth/mapper_test.go | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/store/cachekvstore_test.go b/store/cachekvstore_test.go index e7958dfcd..9d02d57d3 100644 --- a/store/cachekvstore_test.go +++ b/store/cachekvstore_test.go @@ -493,3 +493,25 @@ func (krc *keyRangeCounter) key() int { //-------------------------------------------------------- func bz(s string) []byte { return []byte(s) } + +func BenchmarkCacheKVStoreGetNoKeyFound(b *testing.B) { + st := newCacheKVStore() + b.ResetTimer() + // assumes b.N < 2**24 + for i := 0; i < b.N; i++ { + st.Get([]byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)}) + } +} + +func BenchmarkCacheKVStoreGetKeyFound(b *testing.B) { + st := newCacheKVStore() + for i := 0; i < b.N; i++ { + arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} + st.Set(arr, arr) + } + b.ResetTimer() + // assumes b.N < 2**24 + for i := 0; i < b.N; i++ { + st.Get([]byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)}) + } +} diff --git a/x/auth/mapper_test.go b/x/auth/mapper_test.go index 96dc57b79..9580d3133 100644 --- a/x/auth/mapper_test.go +++ b/x/auth/mapper_test.go @@ -60,3 +60,26 @@ func TestAccountMapperGetSet(t *testing.T) { require.NotNil(t, acc) require.Equal(t, newSequence, acc.GetSequence()) } + +func BenchmarkAccountMapperGetAccountFound(b *testing.B) { + ms, capKey, _ := setupMultiStore() + cdc := codec.New() + RegisterBaseAccount(cdc) + + // make context and mapper + ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger()) + mapper := NewAccountMapper(cdc, capKey, ProtoBaseAccount) + + // assumes b.N < 2**24 + for i := 0; i < b.N; i++ { + arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} + addr := sdk.AccAddress(arr) + acc := mapper.NewAccountWithAddress(ctx, addr) + mapper.SetAccount(ctx, acc) + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} + mapper.GetAccount(ctx, sdk.AccAddress(arr)) + } +}