Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused Samplers #3219

Merged
merged 17 commits into from
Jul 31, 2024
25 changes: 10 additions & 15 deletions utils/sampler/uniform_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,24 @@ import (
"testing"
)

// BenchmarkAllUniform
func BenchmarkAllUniform(b *testing.B) {
func BenchmarkUniform(b *testing.B) {
sizes := []uint64{
30,
35,
500,
10000,
100000,
}
for _, s := range uniformSamplers {
for _, size := range sizes {
b.Run(fmt.Sprintf("sampler %s with %d elements uniformly", s.name, size), func(b *testing.B) {
UniformBenchmark(b, s.sampler, size, 30)
})
}
}
}
for _, size := range sizes {
b.Run(fmt.Sprintf("%d elements uniformly", size), func(b *testing.B) {
s := NewUniform()

func UniformBenchmark(b *testing.B, s Uniform, size uint64, toSample int) {
s.Initialize(size)
s.Initialize(size)

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = s.Sample(toSample)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = s.Sample(30)
}
})
}
}
72 changes: 0 additions & 72 deletions utils/sampler/uniform_best.go

This file was deleted.

58 changes: 0 additions & 58 deletions utils/sampler/uniform_resample.go

This file was deleted.

89 changes: 14 additions & 75 deletions utils/sampler/uniform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,15 @@
package sampler

import (
"fmt"
"math"
"slices"
"testing"

"github.com/stretchr/testify/require"
)

var (
uniformSamplers = []struct {
name string
sampler Uniform
}{
{
name: "replacer",
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
sampler: &uniformReplacer{
rng: globalRNG,
},
},
{
name: "resampler",
sampler: &uniformResample{
rng: globalRNG,
},
},
{
name: "best",
sampler: NewBestUniform(30),
},
}
uniformTests = []struct {
name string
test func(*testing.T, Uniform)
}{
{
name: "can sample large values",
test: UniformInitializeMaxUint64Test,
},
{
name: "out of range",
test: UniformOutOfRangeTest,
},
{
name: "empty",
test: UniformEmptyTest,
},
{
name: "singleton",
test: UniformSingletonTest,
},
{
name: "distribution",
test: UniformDistributionTest,
},
{
name: "over sample",
test: UniformOverSampleTest,
},
{
name: "lazily sample",
test: UniformLazilySample,
},
}
)

func TestAllUniform(t *testing.T) {
for _, s := range uniformSamplers {
for _, test := range uniformTests {
t.Run(fmt.Sprintf("sampler %s test %s", s.name, test.name), func(t *testing.T) {
test.test(t, s.sampler)
})
}
}
}

func UniformInitializeMaxUint64Test(t *testing.T, s Uniform) {
func TestUniformInitializeMaxUint64(t *testing.T) {
s := NewUniform()
s.Initialize(math.MaxUint64)

for {
Expand All @@ -92,15 +25,17 @@ func UniformInitializeMaxUint64Test(t *testing.T, s Uniform) {
}
}

func UniformOutOfRangeTest(t *testing.T, s Uniform) {
func TestUniformOutOfRange(t *testing.T) {
s := NewUniform()
s.Initialize(0)

_, ok := s.Sample(1)
require.False(t, ok)
}

func UniformEmptyTest(t *testing.T, s Uniform) {
func TestUniformEmpty(t *testing.T) {
require := require.New(t)
s := NewUniform()

s.Initialize(1)

Expand All @@ -109,8 +44,9 @@ func UniformEmptyTest(t *testing.T, s Uniform) {
require.Empty(val)
}

func UniformSingletonTest(t *testing.T, s Uniform) {
func TestUniformSingleton(t *testing.T) {
require := require.New(t)
s := NewUniform()

s.Initialize(1)

Expand All @@ -119,8 +55,9 @@ func UniformSingletonTest(t *testing.T, s Uniform) {
require.Equal([]uint64{0}, val)
}

func UniformDistributionTest(t *testing.T, s Uniform) {
func TestUniformDistribution(t *testing.T) {
require := require.New(t)
s := NewUniform()

s.Initialize(3)

Expand All @@ -131,15 +68,17 @@ func UniformDistributionTest(t *testing.T, s Uniform) {
require.Equal([]uint64{0, 1, 2}, val)
}

func UniformOverSampleTest(t *testing.T, s Uniform) {
func TestUniformOverSample(t *testing.T) {
s := NewUniform()
s.Initialize(3)

_, ok := s.Sample(4)
require.False(t, ok)
}

func UniformLazilySample(t *testing.T, s Uniform) {
func TestUniformLazilySample(t *testing.T) {
require := require.New(t)
s := NewUniform()

s.Initialize(3)

Expand Down
14 changes: 0 additions & 14 deletions utils/sampler/weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ type Weighted interface {
Sample(sampleValue uint64) (int, bool)
}

// NewWeighted returns a new sampler
func NewWeighted() Weighted {
return &weightedBest{
samplers: []Weighted{
&weightedArray{},
&weightedHeap{},
&weightedUniform{
maxWeight: 1024,
},
},
benchmarkIterations: 100,
}
}

func NewDeterministicWeighted() Weighted {
return &weightedHeap{}
}
Loading
Loading