BenchmarkPubSubConcurrentCreation benchmarks concurrent PubSub creation
(b *testing.B)
| 238 | |
| 239 | // BenchmarkPubSubConcurrentCreation benchmarks concurrent PubSub creation |
| 240 | func BenchmarkPubSubConcurrentCreation(b *testing.B) { |
| 241 | ctx := context.Background() |
| 242 | client := benchmarkClient(32) |
| 243 | defer client.Close() |
| 244 | |
| 245 | concurrencyLevels := []int{1, 2, 4, 8, 16} |
| 246 | |
| 247 | for _, concurrency := range concurrencyLevels { |
| 248 | b.Run(fmt.Sprintf("Concurrency_%d", concurrency), func(b *testing.B) { |
| 249 | b.ResetTimer() |
| 250 | b.ReportAllocs() |
| 251 | |
| 252 | var wg sync.WaitGroup |
| 253 | semaphore := make(chan struct{}, concurrency) |
| 254 | |
| 255 | for i := 0; i < b.N; i++ { |
| 256 | wg.Add(1) |
| 257 | semaphore <- struct{}{} |
| 258 | |
| 259 | go func() { |
| 260 | defer wg.Done() |
| 261 | defer func() { <-semaphore }() |
| 262 | |
| 263 | pubsub := client.Subscribe(ctx, "test-channel") |
| 264 | pubsub.Close() |
| 265 | }() |
| 266 | } |
| 267 | |
| 268 | wg.Wait() |
| 269 | }) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // BenchmarkPubSubMultipleChannels benchmarks subscribing to multiple channels |
| 274 | func BenchmarkPubSubMultipleChannels(b *testing.B) { |