aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/solomon/race_test.go
blob: 32be6f34fb8e91d412bc37e51194ace79797520b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package solomon

import (
	"bytes"
	"context"
	"fmt"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"github.com/ydb-platform/ydb/library/go/core/metrics"
)

type spinBarrier struct {
	count   int64
	waiting atomic.Int64
	step    atomic.Int64
}

func newSpinBarrier(size int) *spinBarrier {
	return &spinBarrier{count: int64(size)}
}

func (b *spinBarrier) wait() {
	s := b.step.Load()
	w := b.waiting.Add(1)
	if w == b.count {
		b.waiting.Store(0)
		b.step.Add(1)
	} else {
		for s == b.step.Load() {
			// noop
		}
	}
}

func TestRaceDurationHistogramVecVersusStreamJson(t *testing.T) {
	// Regression test: https://github.com/ydb-platform/ydb/review/2690822/details
	registry := NewRegistry(NewRegistryOpts())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	const stepCount = 200

	barrier := newSpinBarrier(2)
	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		// Consumer
		defer wg.Done()
		out := bytes.NewBuffer(nil)
		for i := 0; i < stepCount; i++ {
			out.Reset()
			barrier.wait()
			_, err := registry.StreamJSON(ctx, out)
			if err != nil {
				require.ErrorIs(t, err, context.Canceled)
				break
			}
		}
	}()

	wg.Add(1)
	go func() {
		// Producer
		defer wg.Done()

		const success = "success"
		const version = "version"
		vecs := make([]metrics.TimerVec, 0)
		buckets := metrics.NewDurationBuckets(1, 2, 3)
	ProducerLoop:
		for i := 0; i < stepCount; i++ {
			barrier.wait()
			vec := registry.DurationHistogramVec(
				fmt.Sprintf("latency-%v", i),
				buckets,
				[]string{success, version},
			)
			Rated(vec)
			vecs = append(vecs, vec)
			for _, v := range vecs {
				v.With(map[string]string{success: "ok", version: "123"}).RecordDuration(time.Second)
				v.With(map[string]string{success: "false", version: "123"}).RecordDuration(time.Millisecond)
			}
			select {
			case <-ctx.Done():
				break ProducerLoop
			default:
				// noop
			}
		}
	}()
	wg.Wait()
}

func TestRaceDurationHistogramRecordDurationVersusStreamJson(t *testing.T) {
	// Regression test: https://github.com/ydb-platform/ydb/review/2690822/details

	registry := NewRegistry(NewRegistryOpts())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	const stepCount = 200
	barrier := newSpinBarrier(2)
	wg := sync.WaitGroup{}

	wg.Add(1)
	go func() {
		// Consumer
		defer wg.Done()
		out := bytes.NewBuffer(nil)
		for i := 0; i < stepCount; i++ {
			out.Reset()
			barrier.wait()
			_, err := registry.StreamJSON(ctx, out)
			if err != nil {
				require.ErrorIs(t, err, context.Canceled)
				break
			}
		}
	}()

	wg.Add(1)
	go func() {
		// Producer
		defer wg.Done()

		buckets := metrics.NewDurationBuckets(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
		hist := registry.DurationHistogram("latency", buckets)
		// Rated(hist)

	ProducerLoop:
		for i := 0; i < stepCount; i++ {
			barrier.wait()
			hist.RecordDuration(time.Duration(i % 10))
			select {
			case <-ctx.Done():
				break ProducerLoop
			default:
				// noop
			}
		}
	}()
	wg.Wait()
}