MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / benchmarkWorkerPool

Function benchmarkWorkerPool

pkg/workerpool/workerpool_test.go:222–275  ·  view source on GitHub ↗
(b *testing.B, processingDelay time.Duration, publishingDelay time.Duration)

Source from the content-addressed store, hash-verified

220}
221
222func 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
277func BenchmarkWorkerPool(b *testing.B) {
278 delays := []time.Duration{5 * time.Millisecond, 10 * time.Millisecond, 50 * time.Millisecond}

Callers 1

BenchmarkWorkerPoolFunction · 0.85

Calls 9

PublishMethod · 0.95
WaitMethod · 0.95
NewFunction · 0.92
JitterFunction · 0.92
NewWorkerPoolFunction · 0.92
SubMethod · 0.80
DoneMethod · 0.80
NowMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected