Skip to content

Complete b.Loop() migration in benchmark tests #25465

@jaramyweb3

Description

@jaramyweb3

Current situation

Some files are already updated (22 files), but we still have 19 files with the old pattern (33 total occurrences). It's inconsistent and we should fix it.

Why this matters

The Go team recommends using b.Loop() now instead of the manual for loop. Benefits:

  • Better performance measurement accuracy
  • Cleaner code that's easier to read
  • Follows modern Go practices

Files that need updating

Found these with the old pattern:

  • x/bank/types/balance_test.go
  • x/auth/keeper/keeper_bench_test.go (2 places)
  • tools/cosmovisor/args_test.go
  • tests/integration/tx/benchmark/benchmarks_test.go
  • store/rootmulti/snapshot_test.go (2 places)
  • store/internal/maps/bench_test.go
  • store/internal/conv/string_test.go
  • store/cachekv/search_benchmark_test.go
  • store/cachekv/benchmark_test.go
  • store/cache/benchmark_test.go
  • math/legacy_dec_test.go (8 places - this one has a lot)
  • math/int_test.go (2 places)
  • internal/conv/string_test.go
  • crypto/types/compact_bit_array_test.go
  • crypto/keys/internal/benchmarking/bench.go (3 places)
  • crypto/armor_test.go
  • contrib/x/nft/internal/conv/string_test.go
  • codec/unknownproto/benchmarks_test.go (2 places)
  • codec/types/any_test.go

The change is simple

// Old way:
for i := 0; i < b.N; i++ {
    // benchmark code
}

// New way:
for b.Loop() {
    // benchmark code  
}

// Special case when you need the index:
for i := 0; b.Loop(); i++ {
    // benchmark code that uses i
}

Testing plan

Before making changes:

go test -bench=. -benchmem ./... > before.txt

After changes:

go test -bench=. -benchmem ./... > after.txt

Then compare the results to make sure nothing broke and performance improved.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions