1
0

udp_coalesce_bench_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package batch
  2. import (
  3. "testing"
  4. )
  5. // buildUDPv4BulkFlow returns n equal-size datagrams on one flow — the
  6. // steady state for single-flow QUIC bulk, the workload USO exists for.
  7. func buildUDPv4BulkFlow(n, payloadLen int) [][]byte {
  8. pay := make([]byte, payloadLen)
  9. pkts := make([][]byte, n)
  10. for i := range pkts {
  11. pkts[i] = buildUDPv4(40000, 443, pay)
  12. }
  13. return pkts
  14. }
  15. // buildUDPv4RunInterleaved mirrors buildTCPv4RunInterleaved: nFlows*perFlow
  16. // datagrams arriving in GRO-burst runs of runLen per flow.
  17. func buildUDPv4RunInterleaved(nFlows, perFlow, runLen, payloadLen int) [][]byte {
  18. pay := make([]byte, payloadLen)
  19. pkts := make([][]byte, 0, nFlows*perFlow)
  20. for done := 0; done < perFlow; done += runLen {
  21. for f := range nFlows {
  22. sport := uint16(40000 + f)
  23. for range runLen {
  24. pkts = append(pkts, buildUDPv4(sport, 443, pay))
  25. }
  26. }
  27. }
  28. return pkts
  29. }
  30. // runUDPCommitBench drives UDPCoalescer.Commit over pkts batchSize at a
  31. // time, flushing between batches, and reports per-packet cost.
  32. func runUDPCommitBench(b *testing.B, pkts [][]byte, batchSize int) {
  33. b.Helper()
  34. c := newTestUDPCoalescer(b, nopTunWriter{})
  35. b.ReportAllocs()
  36. b.SetBytes(int64(len(pkts[0])))
  37. b.ResetTimer()
  38. for i := 0; i < b.N; i++ {
  39. pkt := pkts[i%len(pkts)]
  40. if err := c.Commit(pkt); err != nil {
  41. b.Fatal(err)
  42. }
  43. if (i+1)%batchSize == 0 {
  44. if err := c.Flush(); err != nil {
  45. b.Fatal(err)
  46. }
  47. }
  48. }
  49. _ = c.Flush()
  50. }
  51. // BenchmarkUDPCommitSingleFlow is the single-flow bulk steady state.
  52. func BenchmarkUDPCommitSingleFlow(b *testing.B) {
  53. pkts := buildUDPv4BulkFlow(udpCoalesceMaxSegs, 1200)
  54. runUDPCommitBench(b, pkts, udpCoalesceMaxSegs)
  55. }
  56. // BenchmarkUDPCommitInterleaved4 is the adversarial per-packet round-robin.
  57. func BenchmarkUDPCommitInterleaved4(b *testing.B) {
  58. pkts := buildUDPv4RunInterleaved(4, udpCoalesceMaxSegs, 1, 1200)
  59. runUDPCommitBench(b, pkts, len(pkts))
  60. }
  61. // BenchmarkUDPCommitRunInterleaved4 is 4 flows in GRO-burst runs of 16.
  62. func BenchmarkUDPCommitRunInterleaved4(b *testing.B) {
  63. pkts := buildUDPv4RunInterleaved(4, udpCoalesceMaxSegs, 16, 1200)
  64. runUDPCommitBench(b, pkts, len(pkts))
  65. }