From 68c95b9b133781295676937d9fd5f04b72ae36cc Mon Sep 17 00:00:00 2001 From: Shivanth Date: Wed, 4 Dec 2024 13:03:08 +0100 Subject: [PATCH] Remove random number generator from histogram generation --- cmd/telemetrygen/internal/metrics/worker.go | 68 ++++++++++++++----- .../internal/metrics/worker_test.go | 18 ----- 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/cmd/telemetrygen/internal/metrics/worker.go b/cmd/telemetrygen/internal/metrics/worker.go index e0b2b381bac8..bde5891575fa 100644 --- a/cmd/telemetrygen/internal/metrics/worker.go +++ b/cmd/telemetrygen/internal/metrics/worker.go @@ -5,7 +5,6 @@ package metrics import ( "context" - rand "math/rand/v2" "sync" "sync/atomic" "time" @@ -31,6 +30,53 @@ type worker struct { index int // worker index } +var histogramBucketSamples = []struct { + bucketCounts []uint64 + sum int64 +}{ + { + []uint64{0, 0, 0, 0, 0}, + 0, + }, + + { + []uint64{0, 1, 0, 0, 0}, + 1, + }, + { + []uint64{0, 1, 0, 1, 0}, + 4, + }, + { + []uint64{1, 1, 1, 0, 0}, + 3, + }, + { + []uint64{0, 0, 1, 2, 1}, + 12, + }, + { + []uint64{0, 0, 0, 0, 0}, + 0, + }, + { + []uint64{0, 1, 0, 0, 0}, + 1, + }, + { + []uint64{0, 2, 0, 0, 0}, + 2, + }, + { + []uint64{1, 0, 1, 1, 0}, + 5, + }, + { + []uint64{2, 0, 1, 0, 1}, + 6, + }, +} + func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdkmetric.Exporter, error), signalAttrs []attribute.KeyValue) { limiter := rate.NewLimiter(w.limitPerSecond, 1) @@ -39,8 +85,6 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk w.logger.Error("failed to create the exporter", zap.Error(err)) return } - randomPCG := rand.NewPCG(0, 0) - randomgenerator := rand.New(randomPCG) defer func() { w.logger.Info("stopping the exporter") @@ -86,8 +130,9 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk }, }) case metricTypeHistogram: - iteration := uint64(i) % 5 - sum, bucketCounts := generateHistogramBuckets[int64](iteration, randomgenerator) + iteration := uint64(i) % 10 + sum := histogramBucketSamples[iteration].sum + bucketCounts := histogramBucketSamples[iteration].bucketCounts metrics = append(metrics, metricdata.Metrics{ Name: w.metricName, Data: metricdata.Histogram[int64]{ @@ -133,16 +178,3 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk w.logger.Info("metrics generated", zap.Int64("metrics", i)) w.wg.Done() } - -func generateHistogramBuckets[T int64 | float64](count uint64, randomgenerator *rand.Rand) (sum T, bucketCounts []uint64) { - sum = 0 - bucketCounts = make([]uint64, 5) - var i uint64 - for i = 0; i < count; i++ { - sample := randomgenerator.IntN(5) - // See bounds above - sum += T(sample) - bucketCounts[sample]++ - } - return -} diff --git a/cmd/telemetrygen/internal/metrics/worker_test.go b/cmd/telemetrygen/internal/metrics/worker_test.go index b8e2bd7dff14..7574ac203757 100644 --- a/cmd/telemetrygen/internal/metrics/worker_test.go +++ b/cmd/telemetrygen/internal/metrics/worker_test.go @@ -5,7 +5,6 @@ package metrics import ( "context" - rand "math/rand/v2" "testing" "time" @@ -391,20 +390,3 @@ func configWithMultipleAttributes(metric metricType, qty int) *Config { MetricType: metric, } } - -func TestGenerateHistogramBuckets(t *testing.T) { - randomPCG := rand.NewPCG(0, 0) - randomgenerator := rand.New(randomPCG) - for i := uint64(0); i < 5; i++ { - _, buckets := generateHistogramBuckets[int64](i, randomgenerator) - assert.Equal(t, sumofArray(buckets), i) - } -} - -func sumofArray[T int64 | float64 | uint64](array []T) T { - var sum T - for _, num := range array { - sum += num - } - return sum -}