Debian Codegraph @

sdk/metric/internal/aggregate/atomic_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
5
6 import (
7 "math"
8 "sync"
9 "sync/atomic"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 )
14
15 func TestAtomicSumAddFloatConcurrentSafe(t *testing.T) {
16 var wg sync.WaitGroup
17 var aSum atomicCounter[float64]
18 for _, in := range []float64{
19 0.2,
20 0.25,
21 1.6,
22 10.55,
23 42.4,
24 } {
25 wg.Go(func() {
26 aSum.add(in)
27 })
28 }
29 wg.Wait()
30 assert.Equal(t, float64(55), math.Round(aSum.load()))
31 }
32
33 func TestAtomicSumAddIntConcurrentSafe(t *testing.T) {
34 var wg sync.WaitGroup
35 var aSum atomicCounter[int64]
36 for _, in := range []int64{
37 1,
38 2,
39 3,
40 4,
41 5,
42 } {
43 wg.Go(func() {
44 aSum.add(in)
45 })
46 }
47 wg.Wait()
48 assert.Equal(t, int64(15), aSum.load())
49 }
50
51 func BenchmarkAtomicCounter(b *testing.B) {
52 b.Run("Int64", benchmarkAtomicCounter[int64])
53 b.Run("Float64", benchmarkAtomicCounter[float64])
54 }
55
56 func benchmarkAtomicCounter[N int64 | float64](b *testing.B) {
57 b.Run("add", func(b *testing.B) {
58 var a atomicCounter[N]
59 b.RunParallel(func(pb *testing.PB) {
60 for pb.Next() {
61 a.add(2)
62 }
63 })
64 })
65 b.Run("load", func(b *testing.B) {
66 var a atomicCounter[N]
67 a.add(2)
68 var v N
69 b.RunParallel(func(pb *testing.PB) {
70 for pb.Next() {
71 v = a.load()
72 }
73 })
74 assert.Equal(b, N(2), v)
75 })
76 }
77
78 func TestHotColdWaitGroupConcurrentSafe(t *testing.T) {
79 var wg sync.WaitGroup
80 hcwg := &hotColdWaitGroup{}
81 var data [2]atomic.Uint64
82 for range 5 {
83 wg.Go(func() {
84 hotIdx := hcwg.start()
85 defer hcwg.done(hotIdx)
86 data[hotIdx].Add(1)
87 })
88 }
89 for range 2 {
90 readIdx := hcwg.swapHotAndWait()
91 assert.NotPanics(t, func() {
92 // reading without using atomics should not panic since we are
93 // reading from the cold element, and have waited for all writes to
94 // finish.
95 t.Logf("read value %+v", data[readIdx].Load())
96 })
97 }
98 wg.Wait()
99 }
100
101 func TestAtomicN(t *testing.T) {
102 t.Run("Int64", testAtomicN[int64])
103 t.Run("Float64", testAtomicN[float64])
104 }
105
106 func testAtomicN[N int64 | float64](t *testing.T) {
107 var v atomicN[N]
108 assert.Equal(t, N(0), v.Load())
109 assert.True(t, v.CompareAndSwap(0, 6))
110 assert.Equal(t, N(6), v.Load())
111 assert.False(t, v.CompareAndSwap(0, 6))
112 v.Store(22)
113 assert.Equal(t, N(22), v.Load())
114 }
115
116 func TestAtomicNConcurrentSafe(t *testing.T) {
117 t.Run("Int64", testAtomicNConcurrentSafe[int64])
118 t.Run("Float64", testAtomicNConcurrentSafe[float64])
119 }
120
121 func testAtomicNConcurrentSafe[N int64 | float64](t *testing.T) {
122 var wg sync.WaitGroup
123 var v atomicN[N]
124
125 for range 2 {
126 wg.Go(func() {
127 got := v.Load()
128 assert.Equal(t, int64(0), int64(got)%6)
129 })
130 wg.Go(func() {
131 v.Store(12)
132 })
133 wg.Go(func() {
134 v.CompareAndSwap(0, 6)
135 })
136 }
137 wg.Wait()
138 }
139
140 func BenchmarkAtomicN(b *testing.B) {
141 b.Run("Int64", benchmarkAtomicN[int64])
142 b.Run("Float64", benchmarkAtomicN[float64])
143 }
144
145 func benchmarkAtomicN[N int64 | float64](b *testing.B) {
146 b.Run("Load", func(b *testing.B) {
147 var a atomicN[N]
148 a.Store(2)
149 var v N
150 b.RunParallel(func(pb *testing.PB) {
151 for pb.Next() {
152 v = a.Load()
153 }
154 })
155 assert.Equal(b, N(2), v)
156 })
157 b.Run("Store", func(b *testing.B) {
158 var a atomicN[N]
159 b.RunParallel(func(pb *testing.PB) {
160 for pb.Next() {
161 a.Store(3)
162 }
163 })
164 })
165 b.Run("CompareAndSwap", func(b *testing.B) {
166 var a atomicN[N]
167 b.RunParallel(func(pb *testing.PB) {
168 i := 0
169 for pb.Next() {
170 // Make sure we swap back and forth, in-case that matters.
171 if i%2 == 0 {
172 a.CompareAndSwap(0, 1)
173 } else {
174 a.CompareAndSwap(1, 0)
175 }
176 i++
177 }
178 })
179 })
180 }
181
182 func TestAtomicMinMaxConcurrentSafe(t *testing.T) {
183 t.Run("Int64", testAtomicMinMaxConcurrentSafe[int64])
184 t.Run("Float64", testAtomicMinMaxConcurrentSafe[float64])
185 }
186
187 func testAtomicMinMaxConcurrentSafe[N int64 | float64](t *testing.T) {
188 var wg sync.WaitGroup
189 var minMax atomicMinMax[N]
190
191 assert.False(t, minMax.set.Load())
192 for _, i := range []float64{2, 4, 6, 8, -3, 0, 8, 0} {
193 wg.Go(func() {
194 minMax.Update(N(i))
195 })
196 }
197 wg.Wait()
198
199 assert.True(t, minMax.set.Load())
200 assert.Equal(t, N(-3), minMax.minimum.Load())
201 assert.Equal(t, N(8), minMax.maximum.Load())
202 }
203
204 func BenchmarkAtomicMinMax(b *testing.B) {
205 b.Run("Int64", benchmarkAtomicMinMax[int64])
206 b.Run("Float64", benchmarkAtomicMinMax[float64])
207 }
208
209 func benchmarkAtomicMinMax[N int64 | float64](b *testing.B) {
210 b.Run("UpdateIncreasing", func(b *testing.B) {
211 var a atomicMinMax[N]
212 b.RunParallel(func(pb *testing.PB) {
213 i := 0
214 for pb.Next() {
215 a.Update(N(i))
216 i++
217 }
218 })
219 })
220 b.Run("UpdateDecreasing", func(b *testing.B) {
221 var a atomicMinMax[N]
222 b.RunParallel(func(pb *testing.PB) {
223 i := 0
224 for pb.Next() {
225 a.Update(N(i))
226 i--
227 }
228 })
229 })
230 b.Run("UpdateConstant", func(b *testing.B) {
231 var a atomicMinMax[N]
232 b.RunParallel(func(pb *testing.PB) {
233 for pb.Next() {
234 a.Update(N(5))
235 }
236 })
237 })
238 }