MCPcopy
hub / github.com/grafana/k6 / TestSampleBufferConcurrently

Function TestSampleBufferConcurrently

output/helpers_test.go:54–116  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

52}
53
54func TestSampleBufferConcurrently(t *testing.T) {
55 t.Parallel()
56
57 seed := time.Now().UnixNano()
58 r := rand.New(rand.NewSource(seed))
59 t.Logf("Random source seeded with %d\n", seed)
60
61 registry := metrics.NewRegistry()
62 metric, err := registry.NewMetric("my_metric", metrics.Gauge)
63 require.NoError(t, err)
64
65 producersCount := 50 + r.Intn(50)
66 sampleCount := 10 + r.Intn(10)
67 sleepModifier := 10 + r.Intn(10)
68 buffer := SampleBuffer{}
69
70 wg := make(chan struct{})
71 fillBuffer := func() {
72 for i := range sampleCount {
73 buffer.AddMetricSamples([]metrics.SampleContainer{metrics.Sample{
74 TimeSeries: metrics.TimeSeries{
75 Metric: metric,
76 Tags: registry.RootTagSet().WithTagsFromMap(map[string]string{"tag1": "val1"}),
77 },
78 Time: time.Unix(1562324644, 0),
79 Value: float64(i),
80 }})
81 time.Sleep(time.Duration(i*sleepModifier) * time.Microsecond)
82 }
83 wg <- struct{}{}
84 }
85 for range producersCount {
86 go fillBuffer()
87 }
88
89 timer := time.NewTicker(5 * time.Millisecond)
90 timeout := time.After(5 * time.Second)
91 defer timer.Stop()
92 readSamples := make([]metrics.SampleContainer, 0, sampleCount*producersCount)
93 finishedProducers := 0
94loop:
95 for {
96 select {
97 case <-timer.C:
98 readSamples = append(readSamples, buffer.GetBufferedSamples()...)
99 case <-wg:
100 finishedProducers++
101 if finishedProducers == producersCount {
102 readSamples = append(readSamples, buffer.GetBufferedSamples()...)
103 break loop
104 }
105 case <-timeout:
106 t.Fatalf("test timed out")
107 }
108 }
109 assert.Equal(t, sampleCount*producersCount, len(readSamples))
110 for _, s := range readSamples {
111 require.NotNil(t, s)

Callers

nothing calls this directly

Calls 14

NewMetricMethod · 0.95
AddMetricSamplesMethod · 0.95
RootTagSetMethod · 0.95
GetBufferedSamplesMethod · 0.95
NewRegistryFunction · 0.92
LogfMethod · 0.80
WithTagsFromMapMethod · 0.80
DurationMethod · 0.80
FatalfMethod · 0.80
EqualMethod · 0.80
SleepMethod · 0.65
StopMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…