Skip to content

Commit

Permalink
benchmark with seriazliation/deserialization no proto
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Nov 29, 2024
1 parent 6326b22 commit 13e253d
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
77 changes: 77 additions & 0 deletions x/finality/keeper/power_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,51 @@ func (k Keeper) newVotingPowerStore(ctx context.Context) prefix.Store {
return prefix.NewStore(storeAdapter, types.VotingPowerAsListKey)
}

type ActiveFp struct {
FPBTCPK []byte
VotingPower uint64
}

func (afp *ActiveFp) Marshal() []byte {
var buf bytes.Buffer
buf.Write(afp.FPBTCPK)
buf.Write(sdk.Uint64ToBigEndian(afp.VotingPower))
return buf.Bytes()
}

func (afp *ActiveFp) Unmarshal(data []byte) error {
buf := bytes.NewBuffer(data)
afp.FPBTCPK = buf.Next(32)
afp.VotingPower = sdk.BigEndianToUint64(buf.Next(8))
return nil
}

type ActiveFpList struct {
Fps []*ActiveFp
}

func (afl *ActiveFpList) Marshal() []byte {
var buf bytes.Buffer
for _, fp := range afl.Fps {
buf.Write(fp.Marshal())
}
return buf.Bytes()
}

func (afl *ActiveFpList) Unmarshal(data []byte) error {
fps := []*ActiveFp{}
buf := bytes.NewBuffer(data)
for buf.Len() > 0 {
fp := &ActiveFp{}
if err := fp.Unmarshal(buf.Next(40)); err != nil {
return err
}
fps = append(fps, fp)
}
afl.Fps = fps
return nil
}

func (k Keeper) SetVotingPowerAsList(ctx context.Context, height uint64, activeFPs []*types.ActiveFinalityProvider) {
store := k.newVotingPowerStore(ctx)
activeList := types.ActiveFinalityProvidersList{Fps: activeFPs}
Expand All @@ -150,6 +195,14 @@ func (k Keeper) SetVotingPowerAsList(ctx context.Context, height uint64, activeF
store.Set(sdk.Uint64ToBigEndian(height), activeListBytes)
}

func (k Keeper) SetVotingPowerAsListNew1(ctx context.Context, height uint64, activeFPs []*ActiveFp) {
store := k.newVotingPowerStore(ctx)
activeList := ActiveFpList{Fps: activeFPs}
activeListBytes := activeList.Marshal()

store.Set(sdk.Uint64ToBigEndian(height), activeListBytes)
}

func (k Keeper) GetVotingPowerAsList(ctx context.Context, height uint64) map[string]uint64 {
store := k.newVotingPowerStore(ctx)
activeListBytes := store.Get(sdk.Uint64ToBigEndian(height))
Expand Down Expand Up @@ -188,3 +241,27 @@ func (k Keeper) GetVotingPowerNew(ctx context.Context, fpBTCPK []byte, height ui

return 0
}

func (k Keeper) GetVotingPowerNew1(ctx context.Context, fpBTCPK []byte, height uint64) uint64 {
store := k.newVotingPowerStore(ctx)
activeListBytes := store.Get(sdk.Uint64ToBigEndian(height))

if activeListBytes == nil {
return 0
}

buf := bytes.NewBuffer(activeListBytes)

for buf.Len() > 0 {
fp := &ActiveFp{}
if err := fp.Unmarshal(buf.Next(40)); err != nil {
return 0
}

if bytes.Equal(fp.FPBTCPK, fpBTCPK) {
return fp.VotingPower
}
}

return 0
}
63 changes: 63 additions & 0 deletions x/finality/keeper/voting_power_table_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,66 @@ func BenchmarkGetOneFpVotingPowerNew80(b *testing.B) {
func BenchmarkGetOneFpVotingPowerNew160(b *testing.B) {
getOneFpVotingPowerNew(160, height, b)
}

// ************************************************************************************ New new

func saveFinalityProvidersNew1(
numFinalityProvidersPerBlock int,
lastBlockHeight int,
t *testing.B) (*keeper.Keeper, sdk.Context, []byte, func()) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
tempDir, err := os.MkdirTemp("", "bench-fin")
fmt.Printf("tempDir: %s\n", tempDir)
require.NoError(t, err)
k, closeDb, commit, dbWriteAlot, ctx := kt.FinalityKeeperWithDb(t, tempDir, nil, nil, nil)
randIdx := r.Intn(numFinalityProvidersPerBlock)

var activeFPs []*keeper.ActiveFp

var randKey []byte

for i := 0; i < numFinalityProvidersPerBlock; i++ {
key, err := datagen.GenRandomBIP340PubKey(r)
require.NoError(t, err)
randVotingPower := uint64(r.Intn(1000000)) + 10
activeFPs = append(activeFPs, &keeper.ActiveFp{FPBTCPK: key.MustMarshal(), VotingPower: randVotingPower})

if i == randIdx {
randKey = key.MustMarshal()
}
}

for block := 1; block <= lastBlockHeight; block++ {
k.SetVotingPowerAsListNew1(ctx, uint64(block), activeFPs)
}

// first write to disk, this will actually flush the data to memory table
// of the underlaying golang level db
commit()

// write a lot of data to db directly, this will force golab level db
// to flush the data to disk
dbWriteAlot()

cleanup := func() {
closeDb()
os.RemoveAll(tempDir)
}

return k, ctx, randKey, cleanup
}

func getOneFpVotingPowerNew1(numFinalityProviders int, lastBlockHeight int, b *testing.B) {
k, ctx, fpKey, cleanup := saveFinalityProvidersNew1(numFinalityProviders, lastBlockHeight, b)
defer cleanup()

b.ResetTimer()
for n := 0; n < b.N; n++ {
vp := k.GetVotingPowerNew1(ctx, fpKey, height)
require.NotZero(b, vp)
}
}

func BenchmarkGetOneFpVotingPowerNew160New1(b *testing.B) {
getOneFpVotingPowerNew1(160, height, b)
}

0 comments on commit 13e253d

Please sign in to comment.