Files
prysm/beacon-chain/core/transition/benchmarks_test.go
Preston Van Loon 2fd6bd8150 Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
2025-11-14 01:27:22 +00:00

177 lines
5.5 KiB
Go

package transition_test
import (
"testing"
"github.com/OffchainLabs/prysm/v7/beacon-chain/core/helpers"
"github.com/OffchainLabs/prysm/v7/beacon-chain/core/time"
coreState "github.com/OffchainLabs/prysm/v7/beacon-chain/core/transition"
"github.com/OffchainLabs/prysm/v7/beacon-chain/state"
state_native "github.com/OffchainLabs/prysm/v7/beacon-chain/state/state-native"
"github.com/OffchainLabs/prysm/v7/config/params"
"github.com/OffchainLabs/prysm/v7/consensus-types/blocks"
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
"github.com/OffchainLabs/prysm/v7/testing/benchmark"
"github.com/OffchainLabs/prysm/v7/testing/require"
"google.golang.org/protobuf/proto"
)
var runAmount = 25
func BenchmarkExecuteStateTransition_FullBlock(b *testing.B) {
undo, err := benchmark.SetBenchmarkConfig()
require.NoError(b, err)
defer undo()
beaconState, err := benchmark.PreGenState1Epoch()
require.NoError(b, err)
cleanStates := clonedStates(beaconState)
block, err := benchmark.PreGenFullBlock()
require.NoError(b, err)
for i := 0; b.Loop(); i++ {
wsb, err := blocks.NewSignedBeaconBlock(block)
require.NoError(b, err)
_, err = coreState.ExecuteStateTransition(b.Context(), cleanStates[i], wsb)
require.NoError(b, err)
}
}
func BenchmarkExecuteStateTransition_WithCache(b *testing.B) {
undo, err := benchmark.SetBenchmarkConfig()
require.NoError(b, err)
defer undo()
beaconState, err := benchmark.PreGenState1Epoch()
require.NoError(b, err)
cleanStates := clonedStates(beaconState)
block, err := benchmark.PreGenFullBlock()
require.NoError(b, err)
// We have to reset slot back to last epoch to hydrate cache. Since
// some attestations in block are from previous epoch
currentSlot := beaconState.Slot()
require.NoError(b, beaconState.SetSlot(beaconState.Slot()-params.BeaconConfig().SlotsPerEpoch))
require.NoError(b, helpers.UpdateCommitteeCache(b.Context(), beaconState, time.CurrentEpoch(beaconState)))
require.NoError(b, beaconState.SetSlot(currentSlot))
// Run the state transition once to populate the cache.
wsb, err := blocks.NewSignedBeaconBlock(block)
require.NoError(b, err)
_, err = coreState.ExecuteStateTransition(b.Context(), beaconState, wsb)
require.NoError(b, err, "Failed to process block, benchmarks will fail")
for i := 0; b.Loop(); i++ {
wsb, err := blocks.NewSignedBeaconBlock(block)
require.NoError(b, err)
_, err = coreState.ExecuteStateTransition(b.Context(), cleanStates[i], wsb)
require.NoError(b, err, "Failed to process block, benchmarks will fail")
}
}
func BenchmarkProcessEpoch_2FullEpochs(b *testing.B) {
undo, err := benchmark.SetBenchmarkConfig()
require.NoError(b, err)
defer undo()
beaconState, err := benchmark.PreGenstateFullEpochs()
require.NoError(b, err)
// We have to reset slot back to last epoch to hydrate cache. Since
// some attestations in block are from previous epoch
currentSlot := beaconState.Slot()
require.NoError(b, beaconState.SetSlot(beaconState.Slot()-params.BeaconConfig().SlotsPerEpoch))
require.NoError(b, helpers.UpdateCommitteeCache(b.Context(), beaconState, time.CurrentEpoch(beaconState)))
require.NoError(b, beaconState.SetSlot(currentSlot))
for b.Loop() {
// ProcessEpochPrecompute is the optimized version of process epoch. It's enabled by default
// at run time.
_, err := coreState.ProcessEpochPrecompute(b.Context(), beaconState.Copy())
require.NoError(b, err)
}
}
func BenchmarkHashTreeRoot_FullState(b *testing.B) {
beaconState, err := benchmark.PreGenstateFullEpochs()
require.NoError(b, err)
for b.Loop() {
_, err := beaconState.HashTreeRoot(b.Context())
require.NoError(b, err)
}
}
func BenchmarkHashTreeRootState_FullState(b *testing.B) {
beaconState, err := benchmark.PreGenstateFullEpochs()
require.NoError(b, err)
ctx := b.Context()
// Hydrate the HashTreeRootState cache.
_, err = beaconState.HashTreeRoot(ctx)
require.NoError(b, err)
for b.Loop() {
_, err := beaconState.HashTreeRoot(ctx)
require.NoError(b, err)
}
}
func BenchmarkMarshalState_FullState(b *testing.B) {
beaconState, err := benchmark.PreGenstateFullEpochs()
require.NoError(b, err)
natState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe())
require.NoError(b, err)
b.Run("Proto_Marshal", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, err := proto.Marshal(natState)
require.NoError(b, err)
}
})
b.Run("Fast_SSZ_Marshal", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, err := natState.MarshalSSZ()
require.NoError(b, err)
}
})
}
func BenchmarkUnmarshalState_FullState(b *testing.B) {
beaconState, err := benchmark.PreGenstateFullEpochs()
require.NoError(b, err)
natState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe())
require.NoError(b, err)
protoObject, err := proto.Marshal(natState)
require.NoError(b, err)
sszObject, err := natState.MarshalSSZ()
require.NoError(b, err)
b.Run("Proto_Unmarshal", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
require.NoError(b, proto.Unmarshal(protoObject, &ethpb.BeaconState{}))
}
})
b.Run("Fast_SSZ_Unmarshal", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
sszState := &ethpb.BeaconState{}
require.NoError(b, sszState.UnmarshalSSZ(sszObject))
}
})
}
func clonedStates(beaconState state.BeaconState) []state.BeaconState {
clonedStates := make([]state.BeaconState, runAmount)
for i := range runAmount {
clonedStates[i] = beaconState.Copy()
}
return clonedStates
}