| 117 | } |
| 118 | |
| 119 | func BenchmarkSyncPool(b *testing.B) { |
| 120 | var wg sync.WaitGroup |
| 121 | var trials atomic.Int32 |
| 122 | trials.Store(int32(b.N)) |
| 123 | workers := runtime.NumCPU() + 2 |
| 124 | if workers-4 <= 0 { |
| 125 | b.Skip("Not enough cores") |
| 126 | } |
| 127 | p := sync.Pool{New: func() any { return make([]byte, 16) }} |
| 128 | wg.Add(workers) |
| 129 | b.ResetTimer() |
| 130 | for i := 0; i < workers; i++ { |
| 131 | go func() { |
| 132 | defer wg.Done() |
| 133 | for trials.Add(-1) > 0 { |
| 134 | x := p.Get() |
| 135 | time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond) |
| 136 | p.Put(x) |
| 137 | } |
| 138 | }() |
| 139 | } |
| 140 | wg.Wait() |
| 141 | } |