MCPcopy
hub / github.com/prometheus/client_golang / TestHistogramConcurrency

Function TestHistogramConcurrency

prometheus/histogram_test.go:155–239  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

153var testBuckets = []float64{-2, -1, -0.5, 0, 0.5, 1, 2, math.Inf(+1)}
154
155func TestHistogramConcurrency(t *testing.T) {
156 if testing.Short() {
157 t.Skip("Skipping test in short mode.")
158 }
159
160 rand.New(rand.NewSource(42))
161
162 it := func(n uint32) bool {
163 mutations := int(n%1e4 + 1e4)
164 concLevel := int(n%5 + 1)
165 total := mutations * concLevel
166
167 var start, end sync.WaitGroup
168 start.Add(1)
169 end.Add(concLevel)
170
171 his := NewHistogram(HistogramOpts{
172 Name: "test_histogram",
173 Help: "helpless",
174 Buckets: testBuckets,
175 })
176
177 allVars := make([]float64, total)
178 var sampleSum float64
179 for i := 0; i < concLevel; i++ {
180 vals := make([]float64, mutations)
181 for j := 0; j < mutations; j++ {
182 v := rand.NormFloat64()
183 vals[j] = v
184 allVars[i*mutations+j] = v
185 sampleSum += v
186 }
187
188 go func(vals []float64) {
189 start.Wait()
190 for _, v := range vals {
191 if n%2 == 0 {
192 his.Observe(v)
193 } else {
194 his.(ExemplarObserver).ObserveWithExemplar(v, Labels{"foo": "bar"})
195 }
196 }
197 end.Done()
198 }(vals)
199 }
200 sort.Float64s(allVars)
201 start.Done()
202 end.Wait()
203
204 m := &dto.Metric{}
205 his.Write(m)
206 if got, want := int(*m.Histogram.SampleCount), total; got != want {
207 t.Errorf("got sample count %d, want %d", got, want)
208 }
209 if got, want := *m.Histogram.SampleSum, sampleSum; math.Abs((got-want)/want) > 0.001 {
210 t.Errorf("got sample sum %f, want %f", got, want)
211 }
212

Callers

nothing calls this directly

Calls 8

ObserveMethod · 0.95
getCumulativeCountsFunction · 0.85
WaitMethod · 0.80
NewHistogramFunction · 0.70
AddMethod · 0.65
ObserveWithExemplarMethod · 0.65
WriteMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected