Skip to content

Commit 68c95b9

Browse files
committed
Remove random number generator from histogram generation
1 parent 1683833 commit 68c95b9

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

cmd/telemetrygen/internal/metrics/worker.go

+50-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package metrics
55

66
import (
77
"context"
8-
rand "math/rand/v2"
98
"sync"
109
"sync/atomic"
1110
"time"
@@ -31,6 +30,53 @@ type worker struct {
3130
index int // worker index
3231
}
3332

33+
var histogramBucketSamples = []struct {
34+
bucketCounts []uint64
35+
sum int64
36+
}{
37+
{
38+
[]uint64{0, 0, 0, 0, 0},
39+
0,
40+
},
41+
42+
{
43+
[]uint64{0, 1, 0, 0, 0},
44+
1,
45+
},
46+
{
47+
[]uint64{0, 1, 0, 1, 0},
48+
4,
49+
},
50+
{
51+
[]uint64{1, 1, 1, 0, 0},
52+
3,
53+
},
54+
{
55+
[]uint64{0, 0, 1, 2, 1},
56+
12,
57+
},
58+
{
59+
[]uint64{0, 0, 0, 0, 0},
60+
0,
61+
},
62+
{
63+
[]uint64{0, 1, 0, 0, 0},
64+
1,
65+
},
66+
{
67+
[]uint64{0, 2, 0, 0, 0},
68+
2,
69+
},
70+
{
71+
[]uint64{1, 0, 1, 1, 0},
72+
5,
73+
},
74+
{
75+
[]uint64{2, 0, 1, 0, 1},
76+
6,
77+
},
78+
}
79+
3480
func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdkmetric.Exporter, error), signalAttrs []attribute.KeyValue) {
3581
limiter := rate.NewLimiter(w.limitPerSecond, 1)
3682

@@ -39,8 +85,6 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
3985
w.logger.Error("failed to create the exporter", zap.Error(err))
4086
return
4187
}
42-
randomPCG := rand.NewPCG(0, 0)
43-
randomgenerator := rand.New(randomPCG)
4488

4589
defer func() {
4690
w.logger.Info("stopping the exporter")
@@ -86,8 +130,9 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
86130
},
87131
})
88132
case metricTypeHistogram:
89-
iteration := uint64(i) % 5
90-
sum, bucketCounts := generateHistogramBuckets[int64](iteration, randomgenerator)
133+
iteration := uint64(i) % 10
134+
sum := histogramBucketSamples[iteration].sum
135+
bucketCounts := histogramBucketSamples[iteration].bucketCounts
91136
metrics = append(metrics, metricdata.Metrics{
92137
Name: w.metricName,
93138
Data: metricdata.Histogram[int64]{
@@ -133,16 +178,3 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
133178
w.logger.Info("metrics generated", zap.Int64("metrics", i))
134179
w.wg.Done()
135180
}
136-
137-
func generateHistogramBuckets[T int64 | float64](count uint64, randomgenerator *rand.Rand) (sum T, bucketCounts []uint64) {
138-
sum = 0
139-
bucketCounts = make([]uint64, 5)
140-
var i uint64
141-
for i = 0; i < count; i++ {
142-
sample := randomgenerator.IntN(5)
143-
// See bounds above
144-
sum += T(sample)
145-
bucketCounts[sample]++
146-
}
147-
return
148-
}

cmd/telemetrygen/internal/metrics/worker_test.go

-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package metrics
55

66
import (
77
"context"
8-
rand "math/rand/v2"
98
"testing"
109
"time"
1110

@@ -391,20 +390,3 @@ func configWithMultipleAttributes(metric metricType, qty int) *Config {
391390
MetricType: metric,
392391
}
393392
}
394-
395-
func TestGenerateHistogramBuckets(t *testing.T) {
396-
randomPCG := rand.NewPCG(0, 0)
397-
randomgenerator := rand.New(randomPCG)
398-
for i := uint64(0); i < 5; i++ {
399-
_, buckets := generateHistogramBuckets[int64](i, randomgenerator)
400-
assert.Equal(t, sumofArray(buckets), i)
401-
}
402-
}
403-
404-
func sumofArray[T int64 | float64 | uint64](array []T) T {
405-
var sum T
406-
for _, num := range array {
407-
sum += num
408-
}
409-
return sum
410-
}

0 commit comments

Comments
 (0)