|
| 1 | +// Copyright 2014 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package prometheus |
| 15 | + |
| 16 | +import ( |
| 17 | + "math" |
| 18 | + "sync" |
| 19 | + "sync/atomic" |
| 20 | + "testing" |
| 21 | + "unsafe" |
| 22 | +) |
| 23 | + |
| 24 | +var output float64 |
| 25 | + |
| 26 | +func TestAtomicUpdateFloat(t *testing.T) { |
| 27 | + var val float64 = 0.0 |
| 28 | + bits := (*uint64)(unsafe.Pointer(&val)) |
| 29 | + var wg sync.WaitGroup |
| 30 | + numGoroutines := 100000 |
| 31 | + increment := 1.0 |
| 32 | + |
| 33 | + for i := 0; i < numGoroutines; i++ { |
| 34 | + wg.Add(1) |
| 35 | + go func() { |
| 36 | + defer wg.Done() |
| 37 | + atomicUpdateFloat(bits, func(f float64) float64 { |
| 38 | + return f + increment |
| 39 | + }) |
| 40 | + }() |
| 41 | + } |
| 42 | + |
| 43 | + wg.Wait() |
| 44 | + expected := float64(numGoroutines) * increment |
| 45 | + if val != expected { |
| 46 | + t.Errorf("Expected %f, got %f", expected, val) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Benchmark for atomicUpdateFloat with single goroutine (no contention). |
| 51 | +func BenchmarkAtomicUpdateFloat_SingleGoroutine(b *testing.B) { |
| 52 | + var val float64 = 0.0 |
| 53 | + bits := (*uint64)(unsafe.Pointer(&val)) |
| 54 | + |
| 55 | + for i := 0; i < b.N; i++ { |
| 56 | + atomicUpdateFloat(bits, func(f float64) float64 { |
| 57 | + return f + 1.0 |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + output = val |
| 62 | +} |
| 63 | + |
| 64 | +// Benchmark for old implementation with single goroutine (no contention) -> to check overhead of backoff |
| 65 | +func BenchmarkAtomicNoBackoff_SingleGoroutine(b *testing.B) { |
| 66 | + var val float64 = 0.0 |
| 67 | + bits := (*uint64)(unsafe.Pointer(&val)) |
| 68 | + |
| 69 | + for i := 0; i < b.N; i++ { |
| 70 | + for { |
| 71 | + loadedBits := atomic.LoadUint64(bits) |
| 72 | + newBits := math.Float64bits(math.Float64frombits(loadedBits) + 1.0) |
| 73 | + if atomic.CompareAndSwapUint64(bits, loadedBits, newBits) { |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + output = val |
| 80 | +} |
| 81 | + |
| 82 | +// Benchmark varying the number of goroutines. |
| 83 | +func benchmarkAtomicUpdateFloatConcurrency(b *testing.B, numGoroutines int) { |
| 84 | + var val float64 = 0.0 |
| 85 | + bits := (*uint64)(unsafe.Pointer(&val)) |
| 86 | + b.SetParallelism(numGoroutines) |
| 87 | + |
| 88 | + b.ResetTimer() |
| 89 | + b.RunParallel(func(pb *testing.PB) { |
| 90 | + for pb.Next() { |
| 91 | + atomicUpdateFloat(bits, func(f float64) float64 { |
| 92 | + return f + 1.0 |
| 93 | + }) |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + output = val |
| 98 | +} |
| 99 | + |
| 100 | +func benchmarkAtomicNoBackoffFloatConcurrency(b *testing.B, numGoroutines int) { |
| 101 | + var val float64 = 0.0 |
| 102 | + bits := (*uint64)(unsafe.Pointer(&val)) |
| 103 | + b.SetParallelism(numGoroutines) |
| 104 | + |
| 105 | + b.ResetTimer() |
| 106 | + b.RunParallel(func(pb *testing.PB) { |
| 107 | + for pb.Next() { |
| 108 | + for { |
| 109 | + loadedBits := atomic.LoadUint64(bits) |
| 110 | + newBits := math.Float64bits(math.Float64frombits(loadedBits) + 1.0) |
| 111 | + if atomic.CompareAndSwapUint64(bits, loadedBits, newBits) { |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + output = val |
| 119 | +} |
| 120 | + |
| 121 | +func BenchmarkAtomicUpdateFloat_1Goroutine(b *testing.B) { |
| 122 | + benchmarkAtomicUpdateFloatConcurrency(b, 1) |
| 123 | +} |
| 124 | + |
| 125 | +func BenchmarkAtomicNoBackoff_1Goroutine(b *testing.B) { |
| 126 | + benchmarkAtomicNoBackoffFloatConcurrency(b, 1) |
| 127 | +} |
| 128 | + |
| 129 | +func BenchmarkAtomicUpdateFloat_2Goroutines(b *testing.B) { |
| 130 | + benchmarkAtomicUpdateFloatConcurrency(b, 2) |
| 131 | +} |
| 132 | + |
| 133 | +func BenchmarkAtomicNoBackoff_2Goroutines(b *testing.B) { |
| 134 | + benchmarkAtomicNoBackoffFloatConcurrency(b, 2) |
| 135 | +} |
| 136 | + |
| 137 | +func BenchmarkAtomicUpdateFloat_4Goroutines(b *testing.B) { |
| 138 | + benchmarkAtomicUpdateFloatConcurrency(b, 4) |
| 139 | +} |
| 140 | + |
| 141 | +func BenchmarkAtomicNoBackoff_4Goroutines(b *testing.B) { |
| 142 | + benchmarkAtomicNoBackoffFloatConcurrency(b, 4) |
| 143 | +} |
| 144 | + |
| 145 | +func BenchmarkAtomicUpdateFloat_8Goroutines(b *testing.B) { |
| 146 | + benchmarkAtomicUpdateFloatConcurrency(b, 8) |
| 147 | +} |
| 148 | + |
| 149 | +func BenchmarkAtomicNoBackoff_8Goroutines(b *testing.B) { |
| 150 | + benchmarkAtomicNoBackoffFloatConcurrency(b, 8) |
| 151 | +} |
| 152 | + |
| 153 | +func BenchmarkAtomicUpdateFloat_16Goroutines(b *testing.B) { |
| 154 | + benchmarkAtomicUpdateFloatConcurrency(b, 16) |
| 155 | +} |
| 156 | + |
| 157 | +func BenchmarkAtomicNoBackoff_16Goroutines(b *testing.B) { |
| 158 | + benchmarkAtomicNoBackoffFloatConcurrency(b, 16) |
| 159 | +} |
| 160 | + |
| 161 | +func BenchmarkAtomicUpdateFloat_32Goroutines(b *testing.B) { |
| 162 | + benchmarkAtomicUpdateFloatConcurrency(b, 32) |
| 163 | +} |
| 164 | + |
| 165 | +func BenchmarkAtomicNoBackoff_32Goroutines(b *testing.B) { |
| 166 | + benchmarkAtomicNoBackoffFloatConcurrency(b, 32) |
| 167 | +} |
0 commit comments