github.com/primecitizens/pcz/std@v0.2.1/core/atomic/bench_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2016 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package atomic_test 9 10 import ( 11 "testing" 12 13 "github.com/primecitizens/pcz/std/core/atomic" 14 ) 15 16 var sink any 17 18 func BenchmarkAtomicLoad64(b *testing.B) { 19 var x uint64 20 sink = &x 21 for i := 0; i < b.N; i++ { 22 _ = atomic.Load64(&x) 23 } 24 } 25 26 func BenchmarkAtomicStore64(b *testing.B) { 27 var x uint64 28 sink = &x 29 for i := 0; i < b.N; i++ { 30 atomic.Store64(&x, 0) 31 } 32 } 33 34 func BenchmarkAtomicLoad32(b *testing.B) { 35 var x uint32 36 sink = &x 37 for i := 0; i < b.N; i++ { 38 _ = atomic.Load32(&x) 39 } 40 } 41 42 func BenchmarkStore32(b *testing.B) { 43 var x uint32 44 sink = &x 45 for i := 0; i < b.N; i++ { 46 atomic.Store32(&x, 0) 47 } 48 } 49 50 func BenchmarkAnd8(b *testing.B) { 51 var x [512]uint8 // give byte its own cache line 52 sink = &x 53 for i := 0; i < b.N; i++ { 54 atomic.And8(&x[255], uint8(i)) 55 } 56 } 57 58 func BenchmarkAnd32(b *testing.B) { 59 var x [128]uint32 // give x its own cache line 60 sink = &x 61 for i := 0; i < b.N; i++ { 62 atomic.And32(&x[63], uint32(i)) 63 } 64 } 65 66 func BenchmarkAnd8Parallel(b *testing.B) { 67 var x [512]uint8 // give byte its own cache line 68 sink = &x 69 b.RunParallel(func(pb *testing.PB) { 70 i := uint8(0) 71 for pb.Next() { 72 atomic.And8(&x[255], i) 73 i++ 74 } 75 }) 76 } 77 78 func BenchmarkAnd32Parallel(b *testing.B) { 79 var x [128]uint32 // give x its own cache line 80 sink = &x 81 b.RunParallel(func(pb *testing.PB) { 82 i := uint32(0) 83 for pb.Next() { 84 atomic.And32(&x[63], i) 85 i++ 86 } 87 }) 88 } 89 90 func BenchmarkOr8(b *testing.B) { 91 var x [512]uint8 // give byte its own cache line 92 sink = &x 93 for i := 0; i < b.N; i++ { 94 atomic.Or8(&x[255], uint8(i)) 95 } 96 } 97 98 func BenchmarkOr32(b *testing.B) { 99 var x [128]uint32 // give x its own cache line 100 sink = &x 101 for i := 0; i < b.N; i++ { 102 atomic.Or32(&x[63], uint32(i)) 103 } 104 } 105 106 func BenchmarkOr8Parallel(b *testing.B) { 107 var x [512]uint8 // give byte its own cache line 108 sink = &x 109 b.RunParallel(func(pb *testing.PB) { 110 i := uint8(0) 111 for pb.Next() { 112 atomic.Or8(&x[255], i) 113 i++ 114 } 115 }) 116 } 117 118 func BenchmarkOr32Parallel(b *testing.B) { 119 var x [128]uint32 // give x its own cache line 120 sink = &x 121 b.RunParallel(func(pb *testing.PB) { 122 i := uint32(0) 123 for pb.Next() { 124 atomic.Or32(&x[63], i) 125 i++ 126 } 127 }) 128 } 129 130 func BenchmarkAdd32(b *testing.B) { 131 var x uint32 132 ptr := &x 133 b.RunParallel(func(pb *testing.PB) { 134 for pb.Next() { 135 atomic.Add32(ptr, 1) 136 } 137 }) 138 } 139 140 func BenchmarkAdd64(b *testing.B) { 141 var x uint64 142 ptr := &x 143 b.RunParallel(func(pb *testing.PB) { 144 for pb.Next() { 145 atomic.Add64(ptr, 1) 146 } 147 }) 148 } 149 150 func BenchmarkCas(b *testing.B) { 151 var x uint32 152 x = 1 153 ptr := &x 154 b.RunParallel(func(pb *testing.PB) { 155 for pb.Next() { 156 atomic.Cas32(ptr, 1, 0) 157 atomic.Cas32(ptr, 0, 1) 158 } 159 }) 160 } 161 162 func BenchmarkCas64(b *testing.B) { 163 var x uint64 164 x = 1 165 ptr := &x 166 b.RunParallel(func(pb *testing.PB) { 167 for pb.Next() { 168 atomic.Cas64(ptr, 1, 0) 169 atomic.Cas64(ptr, 0, 1) 170 } 171 }) 172 } 173 func BenchmarkSwap32(b *testing.B) { 174 var x uint32 175 x = 1 176 ptr := &x 177 b.RunParallel(func(pb *testing.PB) { 178 var y uint32 179 y = 1 180 for pb.Next() { 181 y = atomic.Swap32(ptr, y) 182 y += 1 183 } 184 }) 185 } 186 187 func BenchmarkSwap64(b *testing.B) { 188 var x uint64 189 x = 1 190 ptr := &x 191 b.RunParallel(func(pb *testing.PB) { 192 var y uint64 193 y = 1 194 for pb.Next() { 195 y = atomic.Swap64(ptr, y) 196 y += 1 197 } 198 }) 199 }