(b *testing.B, f func() incrementUint64Map)
| 107 | } |
| 108 | |
| 109 | func benchmarkIncrementUint64Map(b *testing.B, f func() incrementUint64Map) { |
| 110 | const cat = "cat" |
| 111 | benches := []struct { |
| 112 | name string |
| 113 | goroutineCount int |
| 114 | }{ |
| 115 | { |
| 116 | name: " 1", |
| 117 | goroutineCount: 1, |
| 118 | }, |
| 119 | { |
| 120 | name: " 10", |
| 121 | goroutineCount: 10, |
| 122 | }, |
| 123 | { |
| 124 | name: " 100", |
| 125 | goroutineCount: 100, |
| 126 | }, |
| 127 | { |
| 128 | name: "1000", |
| 129 | goroutineCount: 1000, |
| 130 | }, |
| 131 | } |
| 132 | for _, bb := range benches { |
| 133 | b.Run(bb.name, func(b *testing.B) { |
| 134 | m := f() |
| 135 | var wg sync.WaitGroup |
| 136 | wg.Add(bb.goroutineCount) |
| 137 | b.ResetTimer() |
| 138 | for i := 0; i < bb.goroutineCount; i++ { |
| 139 | go func() { |
| 140 | for j := 0; j < b.N; j++ { |
| 141 | m.increment(cat) |
| 142 | } |
| 143 | wg.Done() |
| 144 | }() |
| 145 | } |
| 146 | wg.Wait() |
| 147 | b.StopTimer() |
| 148 | if m.result(cat) != uint64(bb.goroutineCount*b.N) { |
| 149 | b.Fatalf("result is %d, want %d", m.result(cat), b.N) |
| 150 | } |
| 151 | }) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func BenchmarkMapWithSyncMutexContention(b *testing.B) { |
| 156 | benchmarkIncrementUint64Map(b, newMapWithLock) |
no test coverage detected