Skip to content

Commit

Permalink
Remove unused Samplers (#3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Jul 31, 2024
1 parent 0d0d1e7 commit c26cbaf
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 951 deletions.
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",
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

0 comments on commit c26cbaf

Please sign in to comment.