1
2
3
4
5 package atomic_test
6
7 import (
8 "internal/runtime/atomic"
9 "testing"
10 )
11
12 var sink any
13
14 func BenchmarkAtomicLoad64(b *testing.B) {
15 var x uint64
16 sink = &x
17 for i := 0; i < b.N; i++ {
18 _ = atomic.Load64(&x)
19 }
20 }
21
22 func BenchmarkAtomicStore64(b *testing.B) {
23 var x uint64
24 sink = &x
25 for i := 0; i < b.N; i++ {
26 atomic.Store64(&x, 0)
27 }
28 }
29
30 func BenchmarkAtomicLoad(b *testing.B) {
31 var x uint32
32 sink = &x
33 for i := 0; i < b.N; i++ {
34 _ = atomic.Load(&x)
35 }
36 }
37
38 func BenchmarkAtomicStore(b *testing.B) {
39 var x uint32
40 sink = &x
41 for i := 0; i < b.N; i++ {
42 atomic.Store(&x, 0)
43 }
44 }
45
46 func BenchmarkAtomicLoad8(b *testing.B) {
47 var x uint8
48 sink = &x
49 for i := 0; i < b.N; i++ {
50 atomic.Load8(&x)
51 }
52 }
53
54 func BenchmarkAtomicStore8(b *testing.B) {
55 var x uint8
56 sink = &x
57 for i := 0; i < b.N; i++ {
58 atomic.Store8(&x, 0)
59 }
60 }
61
62 func BenchmarkAnd8(b *testing.B) {
63 var x [512]uint8
64 sink = &x
65 for i := 0; i < b.N; i++ {
66 atomic.And8(&x[255], uint8(i))
67 }
68 }
69
70 func BenchmarkAnd(b *testing.B) {
71 var x [128]uint32
72 sink = &x
73 for i := 0; i < b.N; i++ {
74 atomic.And(&x[63], uint32(i))
75 }
76 }
77
78 func BenchmarkAnd8Parallel(b *testing.B) {
79 var x [512]uint8
80 sink = &x
81 b.RunParallel(func(pb *testing.PB) {
82 i := uint8(0)
83 for pb.Next() {
84 atomic.And8(&x[255], i)
85 i++
86 }
87 })
88 }
89
90 func BenchmarkAndParallel(b *testing.B) {
91 var x [128]uint32
92 sink = &x
93 b.RunParallel(func(pb *testing.PB) {
94 i := uint32(0)
95 for pb.Next() {
96 atomic.And(&x[63], i)
97 i++
98 }
99 })
100 }
101
102 func BenchmarkOr8(b *testing.B) {
103 var x [512]uint8
104 sink = &x
105 for i := 0; i < b.N; i++ {
106 atomic.Or8(&x[255], uint8(i))
107 }
108 }
109
110 func BenchmarkOr(b *testing.B) {
111 var x [128]uint32
112 sink = &x
113 for i := 0; i < b.N; i++ {
114 atomic.Or(&x[63], uint32(i))
115 }
116 }
117
118 func BenchmarkOr8Parallel(b *testing.B) {
119 var x [512]uint8
120 sink = &x
121 b.RunParallel(func(pb *testing.PB) {
122 i := uint8(0)
123 for pb.Next() {
124 atomic.Or8(&x[255], i)
125 i++
126 }
127 })
128 }
129
130 func BenchmarkOrParallel(b *testing.B) {
131 var x [128]uint32
132 sink = &x
133 b.RunParallel(func(pb *testing.PB) {
134 i := uint32(0)
135 for pb.Next() {
136 atomic.Or(&x[63], i)
137 i++
138 }
139 })
140 }
141
142 func BenchmarkXadd(b *testing.B) {
143 var x uint32
144 ptr := &x
145 b.RunParallel(func(pb *testing.PB) {
146 for pb.Next() {
147 atomic.Xadd(ptr, 1)
148 }
149 })
150 }
151
152 func BenchmarkXadd64(b *testing.B) {
153 var x uint64
154 ptr := &x
155 b.RunParallel(func(pb *testing.PB) {
156 for pb.Next() {
157 atomic.Xadd64(ptr, 1)
158 }
159 })
160 }
161
162 func BenchmarkCas(b *testing.B) {
163 var x uint32
164 x = 1
165 ptr := &x
166 b.RunParallel(func(pb *testing.PB) {
167 for pb.Next() {
168 atomic.Cas(ptr, 1, 0)
169 atomic.Cas(ptr, 0, 1)
170 }
171 })
172 }
173
174 func BenchmarkCas64(b *testing.B) {
175 var x uint64
176 x = 1
177 ptr := &x
178 b.RunParallel(func(pb *testing.PB) {
179 for pb.Next() {
180 atomic.Cas64(ptr, 1, 0)
181 atomic.Cas64(ptr, 0, 1)
182 }
183 })
184 }
185 func BenchmarkXchg(b *testing.B) {
186 var x uint32
187 x = 1
188 ptr := &x
189 b.RunParallel(func(pb *testing.PB) {
190 var y uint32
191 y = 1
192 for pb.Next() {
193 y = atomic.Xchg(ptr, y)
194 y += 1
195 }
196 })
197 }
198
199 func BenchmarkXchg64(b *testing.B) {
200 var x uint64
201 x = 1
202 ptr := &x
203 b.RunParallel(func(pb *testing.PB) {
204 var y uint64
205 y = 1
206 for pb.Next() {
207 y = atomic.Xchg64(ptr, y)
208 y += 1
209 }
210 })
211 }
212
View as plain text