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

Function TestSummaryConcurrency

prometheus/summary_test.go:201–280  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

199}
200
201func TestSummaryConcurrency(t *testing.T) {
202 if testing.Short() {
203 t.Skip("Skipping test in short mode.")
204 }
205
206 rand.New(rand.NewSource(42))
207 objMap := map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
208
209 it := func(n uint32) bool {
210 mutations := int(n%1e4 + 1e4)
211 concLevel := int(n%5 + 1)
212 total := mutations * concLevel
213
214 var start, end sync.WaitGroup
215 start.Add(1)
216 end.Add(concLevel)
217
218 sum := NewSummary(SummaryOpts{
219 Name: "test_summary",
220 Help: "helpless",
221 Objectives: objMap,
222 })
223
224 allVars := make([]float64, total)
225 var sampleSum float64
226 for i := 0; i < concLevel; i++ {
227 vals := make([]float64, mutations)
228 for j := 0; j < mutations; j++ {
229 v := rand.NormFloat64()
230 vals[j] = v
231 allVars[i*mutations+j] = v
232 sampleSum += v
233 }
234
235 go func(vals []float64) {
236 start.Wait()
237 for _, v := range vals {
238 sum.Observe(v)
239 }
240 end.Done()
241 }(vals)
242 }
243 sort.Float64s(allVars)
244 start.Done()
245 end.Wait()
246
247 m := &dto.Metric{}
248 sum.Write(m)
249 if got, want := int(*m.Summary.SampleCount), total; got != want {
250 t.Errorf("got sample count %d, want %d", got, want)
251 }
252 if got, want := *m.Summary.SampleSum, sampleSum; math.Abs((got-want)/want) > 0.001 {
253 t.Errorf("got sample sum %f, want %f", got, want)
254 }
255
256 objSlice := make([]float64, 0, len(objMap))
257 for qu := range objMap {
258 objSlice = append(objSlice, qu)

Callers

nothing calls this directly

Calls 7

ObserveMethod · 0.95
getBoundsFunction · 0.85
WaitMethod · 0.80
NewSummaryFunction · 0.70
AddMethod · 0.65
WriteMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected