| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package exemplar |
| 5 |
|
| 6 |
import ( |
| 7 |
"math" |
| 8 |
"math/rand/v2" |
| 9 |
"slices" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
) |
| 16 |
|
| 17 |
func TestNewFixedSizeReservoir(t *testing.T) { |
| 18 |
t.Run("Int64", ReservoirTest[int64](func(n int) (ReservoirProvider, int) { |
| 19 |
provider := FixedSizeReservoirProvider(n) |
| 20 |
return provider, int(provider(attribute.NewSet()).(*FixedSizeReservoir).k) |
| 21 |
})) |
| 22 |
|
| 23 |
t.Run("Float64", ReservoirTest[float64](func(n int) (ReservoirProvider, int) { |
| 24 |
provider := FixedSizeReservoirProvider(n) |
| 25 |
return provider, int(provider(attribute.NewSet()).(*FixedSizeReservoir).k) |
| 26 |
})) |
| 27 |
} |
| 28 |
|
| 29 |
func TestNewFixedSizeReservoirSamplingCorrectness(t *testing.T) { |
| 30 |
intensity := 0.1 |
| 31 |
sampleSize := 1000 |
| 32 |
|
| 33 |
u := rand.Uint32() |
| 34 |
seed := [32]byte{byte(u), byte(u >> 8), byte(u >> 16), byte(u >> 24)} |
| 35 |
t.Logf("rng seed: %x", seed) |
| 36 |
rng := rand.New(rand.NewChaCha8(seed)) |
| 37 |
|
| 38 |
data := make([]float64, sampleSize*1000) |
| 39 |
for i := range data { |
| 40 |
// Generate exponentially distributed data. |
| 41 |
data[i] = (-1.0 / intensity) * math.Log(rng.Float64()) |
| 42 |
} |
| 43 |
// Sort to test position bias. |
| 44 |
slices.Sort(data) |
| 45 |
|
| 46 |
r := NewFixedSizeReservoir(sampleSize) |
| 47 |
for _, value := range data { |
| 48 |
r.Offer(t.Context(), staticTime, NewValue(value), nil) |
| 49 |
} |
| 50 |
|
| 51 |
var sum float64 |
| 52 |
for i := range r.measurements { |
| 53 |
sum += r.measurements[i].Value.Float64() |
| 54 |
} |
| 55 |
mean := sum / float64(sampleSize) |
| 56 |
|
| 57 |
// Check the intensity/rate of the sampled distribution is preserved |
| 58 |
// ensuring no bias in our random sampling algorithm. |
| 59 |
assert.InDelta(t, 1/mean, intensity, 0.02) // Within 5σ. |
| 60 |
} |
| 61 |
|
| 62 |
func TestFixedSizeReservoirConcurrentSafe(t *testing.T) { |
| 63 |
t.Run("Int64", reservoirConcurrentSafeTest[int64](func(n int) (ReservoirProvider, int) { |
| 64 |
return FixedSizeReservoirProvider(n), n |
| 65 |
})) |
| 66 |
t.Run("Float64", reservoirConcurrentSafeTest[float64](func(n int) (ReservoirProvider, int) { |
| 67 |
return FixedSizeReservoirProvider(n), n |
| 68 |
})) |
| 69 |
} |
| 70 |
|
| 71 |
func TestNextTrackerAtomics(t *testing.T) { |
| 72 |
capacity := uint32(10) |
| 73 |
nt := newNextTracker(capacity) |
| 74 |
nt.setCountAndNext(0, 11) |
| 75 |
count, next := nt.incrementCount() |
| 76 |
assert.Equal(t, uint32(0), count) |
| 77 |
assert.Equal(t, uint32(11), next) |
| 78 |
count, secondNext := nt.incrementCount() |
| 79 |
assert.Equal(t, uint32(1), count) |
| 80 |
assert.Equal(t, next, secondNext) |
| 81 |
nt.setCountAndNext(50, 100) |
| 82 |
count, next = nt.incrementCount() |
| 83 |
assert.Equal(t, uint32(50), count) |
| 84 |
assert.Equal(t, uint32(100), next) |
| 85 |
} |