github.com/prysmaticlabs/prysm@v1.4.4/shared/aggregation/attestations/attestations_bench_test.go (about)

     1  package attestations
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/prysmaticlabs/prysm/shared/copyutil"
     8  
     9  	"github.com/prysmaticlabs/go-bitfield"
    10  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    11  	aggtesting "github.com/prysmaticlabs/prysm/shared/aggregation/testing"
    12  	"github.com/prysmaticlabs/prysm/shared/bls"
    13  	"github.com/prysmaticlabs/prysm/shared/featureconfig"
    14  	"github.com/prysmaticlabs/prysm/shared/params"
    15  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    16  )
    17  
    18  func BenchmarkAggregateAttestations_Aggregate(b *testing.B) {
    19  	// Override expensive BLS aggregation method with cheap no-op such that this benchmark profiles
    20  	// the logic of aggregation selection rather than BLS logic.
    21  	aggregateSignatures = func(sigs []bls.Signature) bls.Signature {
    22  		return sigs[0]
    23  	}
    24  	signatureFromBytes = func(sig []byte) (bls.Signature, error) {
    25  		return bls.NewAggregateSignature(), nil
    26  	}
    27  	defer func() {
    28  		aggregateSignatures = bls.AggregateSignatures
    29  		signatureFromBytes = bls.SignatureFromBytes
    30  	}()
    31  
    32  	bitlistLen := params.BeaconConfig().MaxValidatorsPerCommittee
    33  
    34  	tests := []struct {
    35  		name   string
    36  		inputs []bitfield.Bitlist
    37  	}{
    38  		{
    39  			name:   "256 attestations with single bit set",
    40  			inputs: aggtesting.BitlistsWithSingleBitSet(256, bitlistLen),
    41  		},
    42  		{
    43  			name:   "256 attestations with 64 random bits set",
    44  			inputs: aggtesting.BitlistsWithSingleBitSet(256, bitlistLen),
    45  		},
    46  		{
    47  			name:   "512 attestations with single bit set",
    48  			inputs: aggtesting.BitlistsWithSingleBitSet(512, bitlistLen),
    49  		},
    50  		{
    51  			name:   "1024 attestations with 64 random bits set",
    52  			inputs: aggtesting.BitlistsWithMultipleBitSet(b, 1024, bitlistLen, 64),
    53  		},
    54  	}
    55  
    56  	runner := func(atts []*ethpb.Attestation) {
    57  		attsCopy := make([]*ethpb.Attestation, len(atts))
    58  		for i, att := range atts {
    59  			attsCopy[i] = copyutil.CopyAttestation(att)
    60  		}
    61  		_, err := Aggregate(attsCopy)
    62  		require.NoError(b, err)
    63  	}
    64  
    65  	for _, tt := range tests {
    66  		b.Run(fmt.Sprintf("naive_%s", tt.name), func(b *testing.B) {
    67  			b.StopTimer()
    68  			resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{
    69  				AttestationAggregationStrategy: string(NaiveAggregation),
    70  			})
    71  			atts := aggtesting.MakeAttestationsFromBitlists(tt.inputs)
    72  			defer resetCfg()
    73  			b.StartTimer()
    74  			for i := 0; i < b.N; i++ {
    75  				runner(atts)
    76  			}
    77  		})
    78  		b.Run(fmt.Sprintf("max-cover_%s", tt.name), func(b *testing.B) {
    79  			b.StopTimer()
    80  			resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{
    81  				AttestationAggregationStrategy: string(MaxCoverAggregation),
    82  			})
    83  			atts := aggtesting.MakeAttestationsFromBitlists(tt.inputs)
    84  			defer resetCfg()
    85  			b.StartTimer()
    86  			for i := 0; i < b.N; i++ {
    87  				runner(atts)
    88  			}
    89  		})
    90  		b.Run(fmt.Sprintf("opt-max-cover_%s", tt.name), func(b *testing.B) {
    91  			b.StopTimer()
    92  			resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{
    93  				AttestationAggregationStrategy: string(OptMaxCoverAggregation),
    94  			})
    95  			atts := aggtesting.MakeAttestationsFromBitlists(tt.inputs)
    96  			defer resetCfg()
    97  			b.StartTimer()
    98  			for i := 0; i < b.N; i++ {
    99  				runner(atts)
   100  			}
   101  		})
   102  	}
   103  }