github.com/MetalBlockchain/metalgo@v1.11.9/utils/sampler/weighted_without_replacement_benchmark_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package sampler
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // BenchmarkAllWeightedWithoutReplacement
    14  func BenchmarkAllWeightedWithoutReplacement(b *testing.B) {
    15  	sizes := []int{
    16  		1,
    17  		5,
    18  		25,
    19  		50,
    20  		75,
    21  		100,
    22  	}
    23  	for _, s := range weightedWithoutReplacementSamplers {
    24  		for _, size := range sizes {
    25  			b.Run(fmt.Sprintf("sampler %s with %d elements", s.name, size), func(b *testing.B) {
    26  				WeightedWithoutReplacementPowBenchmark(
    27  					b,
    28  					s.sampler,
    29  					0,
    30  					100000,
    31  					size,
    32  				)
    33  			})
    34  		}
    35  	}
    36  }
    37  
    38  func WeightedWithoutReplacementPowBenchmark(
    39  	b *testing.B,
    40  	s WeightedWithoutReplacement,
    41  	exponent float64,
    42  	size int,
    43  	count int,
    44  ) {
    45  	require := require.New(b)
    46  
    47  	_, weights, err := CalcWeightedPoW(exponent, size)
    48  	require.NoError(err)
    49  	require.NoError(s.Initialize(weights))
    50  
    51  	b.ResetTimer()
    52  	for i := 0; i < b.N; i++ {
    53  		_, _ = s.Sample(count)
    54  	}
    55  }