github.com/MetalBlockchain/metalgo@v1.11.9/utils/sampler/uniform_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  
    11  // BenchmarkAllUniform
    12  func BenchmarkAllUniform(b *testing.B) {
    13  	sizes := []uint64{
    14  		30,
    15  		35,
    16  		500,
    17  		10000,
    18  		100000,
    19  	}
    20  	for _, s := range uniformSamplers {
    21  		for _, size := range sizes {
    22  			b.Run(fmt.Sprintf("sampler %s with %d elements uniformly", s.name, size), func(b *testing.B) {
    23  				UniformBenchmark(b, s.sampler, size, 30)
    24  			})
    25  		}
    26  	}
    27  }
    28  
    29  func UniformBenchmark(b *testing.B, s Uniform, size uint64, toSample int) {
    30  	s.Initialize(size)
    31  
    32  	b.ResetTimer()
    33  	for i := 0; i < b.N; i++ {
    34  		_, _ = s.Sample(toSample)
    35  	}
    36  }