(b *testing.B, processingDelay time.Duration, publishingDelay time.Duration)
| 220 | } |
| 221 | |
| 222 | func benchmarkWorkerPool(b *testing.B, processingDelay time.Duration, publishingDelay time.Duration) { |
| 223 | _, ctx := test.New(b) |
| 224 | |
| 225 | var totalQueueDelayMS int64 |
| 226 | var totalHandled int64 |
| 227 | var published, dropped int64 |
| 228 | |
| 229 | for r := 0; r < b.N; r++ { |
| 230 | ctx, cancel := context.WithCancel(ctx) |
| 231 | defer cancel() |
| 232 | |
| 233 | handler := func(ctx context.Context, tm time.Time) { |
| 234 | delay := time.Now().Sub(tm).Milliseconds() |
| 235 | atomic.AddInt64(&totalQueueDelayMS, delay) |
| 236 | atomic.AddInt64(&totalHandled, 1) |
| 237 | |
| 238 | time.Sleep(random.Jitter(processingDelay, 0.15)) |
| 239 | } |
| 240 | |
| 241 | wp := workerpool.NewWorkerPool(workerpool.Config[time.Time]{ |
| 242 | Component: &mockComponent{}, |
| 243 | Context: ctx, |
| 244 | Handler: handler, |
| 245 | }) |
| 246 | |
| 247 | var wg sync.WaitGroup |
| 248 | publisher := func() { |
| 249 | defer wg.Done() |
| 250 | for range 1_000 { |
| 251 | if err := wp.Publish(ctx, time.Now()); err != nil { |
| 252 | atomic.AddInt64(&dropped, 1) |
| 253 | } else { |
| 254 | atomic.AddInt64(&published, 1) |
| 255 | } |
| 256 | time.Sleep(random.Jitter(publishingDelay, 0.15)) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | for i := 0; i < workerpool.DefaultMaxWorkers; i++ { |
| 261 | wg.Add(1) |
| 262 | go publisher() |
| 263 | } |
| 264 | |
| 265 | wg.Wait() |
| 266 | |
| 267 | time.Sleep(testTimeout) |
| 268 | cancel() |
| 269 | wp.Wait() |
| 270 | } |
| 271 | |
| 272 | b.ReportMetric(float64(totalQueueDelayMS)/float64(totalHandled), "queueDelayMS") |
| 273 | b.ReportMetric(float64(published), "published") |
| 274 | b.ReportMetric(float64(dropped), "dropped") |
| 275 | } |
| 276 | |
| 277 | func BenchmarkWorkerPool(b *testing.B) { |
| 278 | delays := []time.Duration{5 * time.Millisecond, 10 * time.Millisecond, 50 * time.Millisecond} |
no test coverage detected