refactor: use b.Loop() to simplify the code and improve performance (#25379)

Signed-off-by: vastonus <vastonus@outlook.com>
This commit is contained in:
vastonus 2025-10-01 22:34:05 +08:00 committed by GitHub
parent fc6df7e651
commit e61ee1d389
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 9 additions and 15 deletions

View File

@ -222,9 +222,8 @@ func TestRecoverSanity(t *testing.T) {
func BenchmarkSign(b *testing.B) {
_, seckey := generateKeyPair()
msg := csprngEntropy(32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
Sign(msg, seckey)
}
}
@ -233,9 +232,8 @@ func BenchmarkRecover(b *testing.B) {
msg := csprngEntropy(32)
_, seckey := generateKeyPair()
sig, _ := Sign(msg, seckey)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
RecoverPubkey(msg, sig)
}
}

View File

@ -55,10 +55,8 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) {
ctx := app.NewContext(false)
b.ResetTimer()
// assumes b.N < 2**24
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)}
addr := sdk.AccAddress(arr)
acc := accountKeeper.NewAccountWithAddress(ctx, addr)

View File

@ -86,13 +86,12 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
// pre-compute all txs
txs, err := genSequenceOfTxs(txGen, []sdk.Msg{sendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
require.NoError(b, err)
b.ResetTimer()
height := int64(2)
// Run this with a profiler, so its easy to distinguish what time comes from
// Committing, and what time comes from Check/Deliver Tx.
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
_, _, err := baseApp.SimCheck(txEncoder, txs[i])
if err != nil {
panic(fmt.Errorf("failed to simulate tx: %w", err))
@ -144,13 +143,12 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
// pre-compute all txs
txs, err := genSequenceOfTxs(txGen, []sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
require.NoError(b, err)
b.ResetTimer()
height := int64(2)
// Run this with a profiler, so its easy to distinguish what time comes from
// Committing, and what time comes from Check/Deliver Tx.
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
_, _, err := baseApp.SimCheck(txEncoder, txs[i])
if err != nil {
panic(fmt.Errorf("failed to simulate tx: %w", err))

View File

@ -104,7 +104,7 @@ func BenchmarkBlockProvision(b *testing.B) {
minter.AnnualProvisions = math.LegacyNewDec(r1.Int63n(1000000))
// run the BlockProvision function b.N times
for n := 0; n < b.N; n++ {
for b.Loop() {
minter.BlockProvision(params)
}
}
@ -118,7 +118,7 @@ func BenchmarkNextInflation(b *testing.B) {
bondedRatio := math.LegacyNewDecWithPrec(1, 1)
// run the NextInflationRate function b.N times
for n := 0; n < b.N; n++ {
for b.Loop() {
minter.NextInflationRate(params, bondedRatio)
}
}
@ -132,7 +132,7 @@ func BenchmarkNextAnnualProvisions(b *testing.B) {
totalSupply := math.NewInt(100000000000000)
// run the NextAnnualProvisions function b.N times
for n := 0; n < b.N; n++ {
for b.Loop() {
minter.NextAnnualProvisions(params, totalSupply)
}
}

View File

@ -35,7 +35,7 @@ func (s *StringSuite) TestUnsafeStrToBytes() {
}
func BenchmarkUnsafeStrToBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
UnsafeStrToBytes(strconv.Itoa(i))
}
}