MCPcopy
hub / github.com/WireGuard/wireguard-go / BenchmarkThroughput

Function BenchmarkThroughput

device/device_test.go:340–382  ·  view source on GitHub ↗
(b *testing.B)

Source from the content-addressed store, hash-verified

338}
339
340func BenchmarkThroughput(b *testing.B) {
341 pair := genTestPair(b, true)
342
343 // Establish a connection.
344 pair.Send(b, Ping, nil)
345 pair.Send(b, Pong, nil)
346
347 // Measure how long it takes to receive b.N packets,
348 // starting when we receive the first packet.
349 var recv atomic.Uint64
350 var elapsed time.Duration
351 var wg sync.WaitGroup
352 wg.Add(1)
353 go func() {
354 defer wg.Done()
355 var start time.Time
356 for {
357 <-pair[0].tun.Inbound
358 new := recv.Add(1)
359 if new == 1 {
360 start = time.Now()
361 }
362 // Careful! Don't change this to else if; b.N can be equal to 1.
363 if new == uint64(b.N) {
364 elapsed = time.Since(start)
365 return
366 }
367 }
368 }()
369
370 // Send packets as fast as we can until we've received enough.
371 ping := tuntest.Ping(pair[0].ip, pair[1].ip)
372 pingc := pair[1].tun.Outbound
373 var sent uint64
374 for recv.Load() != uint64(b.N) {
375 sent++
376 pingc <- ping
377 }
378 wg.Wait()
379
380 b.ReportMetric(float64(elapsed)/float64(b.N), "ns/op")
381 b.ReportMetric(1-float64(b.N)/float64(sent), "packet-loss")
382}
383
384func BenchmarkUAPIGet(b *testing.B) {
385 pair := genTestPair(b, true)

Callers

nothing calls this directly

Calls 4

PingFunction · 0.92
genTestPairFunction · 0.85
WaitMethod · 0.80
SendMethod · 0.65

Tested by

no test coverage detected