2020-01-17 17:25:35 +00:00
|
|
|
package state
|
2019-11-26 18:09:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-01-31 20:57:01 +00:00
|
|
|
beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2020-01-17 17:25:35 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/benchutil"
|
2019-11-26 18:09:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
var runAmount = 25
|
|
|
|
|
|
|
|
func TestBenchmarkExecuteStateTransition(t *testing.T) {
|
2020-01-17 17:25:35 +00:00
|
|
|
benchutil.SetBenchmarkConfig()
|
|
|
|
beaconState, err := benchutil.PreGenState1Epoch()
|
2019-11-26 18:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-01-17 17:25:35 +00:00
|
|
|
block, err := benchutil.PreGenFullBlock()
|
2019-11-26 18:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-01-17 17:25:35 +00:00
|
|
|
if _, err := ExecuteStateTransition(context.Background(), beaconState, block); err != nil {
|
2019-11-26 18:09:57 +00:00
|
|
|
t.Fatalf("failed to process block, benchmarks will fail: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 00:14:33 +00:00
|
|
|
func BenchmarkExecuteStateTransition_FullBlock(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
benchutil.SetBenchmarkConfig()
|
|
|
|
beaconState, err := benchutil.PreGenState1Epoch()
|
2019-11-26 18:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
cleanStates := clonedStates(beaconState)
|
2020-01-17 17:25:35 +00:00
|
|
|
block, err := benchutil.PreGenFullBlock()
|
2019-11-26 18:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.N = runAmount
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-01-17 17:25:35 +00:00
|
|
|
if _, err := ExecuteStateTransition(context.Background(), cleanStates[i], block); err != nil {
|
2019-11-26 18:09:57 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkExecuteStateTransition_WithCache(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
benchutil.SetBenchmarkConfig()
|
2019-11-26 18:09:57 +00:00
|
|
|
|
2020-01-17 17:25:35 +00:00
|
|
|
beaconState, err := benchutil.PreGenState1Epoch()
|
2019-11-26 18:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
cleanStates := clonedStates(beaconState)
|
2020-01-17 17:25:35 +00:00
|
|
|
block, err := benchutil.PreGenFullBlock()
|
2019-11-26 18:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to reset slot back to last epoch to hydrate cache. Since
|
|
|
|
// some attestations in block are from previous epoch
|
2020-01-31 20:57:01 +00:00
|
|
|
currentSlot := beaconState.Slot()
|
|
|
|
beaconState.SetSlot(beaconState.Slot() - params.BeaconConfig().SlotsPerEpoch)
|
2020-01-03 23:47:54 +00:00
|
|
|
if err := helpers.UpdateCommitteeCache(beaconState, helpers.CurrentEpoch(beaconState)); err != nil {
|
2019-11-26 18:09:57 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
beaconState.SetSlot(currentSlot)
|
2019-12-02 20:54:57 +00:00
|
|
|
// Run the state transition once to populate the cache.
|
2020-01-17 17:25:35 +00:00
|
|
|
if _, err := ExecuteStateTransition(context.Background(), beaconState, block); err != nil {
|
2019-12-02 20:54:57 +00:00
|
|
|
b.Fatalf("failed to process block, benchmarks will fail: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-26 18:09:57 +00:00
|
|
|
b.N = runAmount
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-01-17 17:25:35 +00:00
|
|
|
if _, err := ExecuteStateTransition(context.Background(), cleanStates[i], block); err != nil {
|
2019-11-26 18:09:57 +00:00
|
|
|
b.Fatalf("failed to process block, benchmarks will fail: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkProcessEpoch_2FullEpochs(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
benchutil.SetBenchmarkConfig()
|
|
|
|
beaconState, err := benchutil.PreGenState2FullEpochs()
|
2019-11-26 18:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-12-02 20:54:57 +00:00
|
|
|
// We have to reset slot back to last epoch to hydrate cache. Since
|
|
|
|
// some attestations in block are from previous epoch
|
2020-01-31 20:57:01 +00:00
|
|
|
currentSlot := beaconState.Slot()
|
|
|
|
beaconState.SetSlot(beaconState.Slot() - params.BeaconConfig().SlotsPerEpoch)
|
2020-01-03 23:47:54 +00:00
|
|
|
if err := helpers.UpdateCommitteeCache(beaconState, helpers.CurrentEpoch(beaconState)); err != nil {
|
2019-12-02 20:54:57 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
beaconState.SetSlot(currentSlot)
|
2019-12-02 20:54:57 +00:00
|
|
|
|
2019-11-26 18:09:57 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-12-10 23:32:11 +00:00
|
|
|
// ProcessEpochPrecompute is the optimized version of process epoch. It's enabled by default
|
|
|
|
// at run time.
|
2020-02-06 03:26:15 +00:00
|
|
|
if _, err := ProcessEpochPrecompute(context.Background(), beaconState.Copy()); err != nil {
|
2019-11-26 18:09:57 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashTreeRoot_FullState(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
beaconState, err := benchutil.PreGenState2FullEpochs()
|
2019-11-26 18:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.N = 50
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if _, err := ssz.HashTreeRoot(beaconState); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 00:14:33 +00:00
|
|
|
func BenchmarkHashTreeRootState_FullState(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
beaconState, err := benchutil.PreGenState2FullEpochs()
|
2019-12-11 00:14:33 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hydrate the HashTreeRootState cache.
|
2020-01-31 20:57:01 +00:00
|
|
|
if _, err := beaconState.HashTreeRoot(); err != nil {
|
2019-12-11 00:14:33 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.N = 50
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-01-31 20:57:01 +00:00
|
|
|
if _, err := beaconState.HashTreeRoot(); err != nil {
|
2019-12-11 00:14:33 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
func clonedStates(beaconState *beaconstate.BeaconState) []*beaconstate.BeaconState {
|
|
|
|
clonedStates := make([]*beaconstate.BeaconState, runAmount)
|
2019-11-26 18:09:57 +00:00
|
|
|
for i := 0; i < runAmount; i++ {
|
2020-02-06 03:26:15 +00:00
|
|
|
clonedStates[i] = beaconState.Copy()
|
2019-11-26 18:09:57 +00:00
|
|
|
}
|
|
|
|
return clonedStates
|
|
|
|
}
|