bench_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package yamux
  2. import (
  3. "testing"
  4. )
  5. func BenchmarkPing(b *testing.B) {
  6. client, server := testClientServer()
  7. defer client.Close()
  8. defer server.Close()
  9. for i := 0; i < b.N; i++ {
  10. rtt, err := client.Ping()
  11. if err != nil {
  12. b.Fatalf("err: %v", err)
  13. }
  14. if rtt == 0 {
  15. b.Fatalf("bad: %v", rtt)
  16. }
  17. }
  18. }
  19. func BenchmarkAccept(b *testing.B) {
  20. client, server := testClientServer()
  21. defer client.Close()
  22. defer server.Close()
  23. go func() {
  24. for i := 0; i < b.N; i++ {
  25. stream, err := server.AcceptStream()
  26. if err != nil {
  27. return
  28. }
  29. stream.Close()
  30. }
  31. }()
  32. for i := 0; i < b.N; i++ {
  33. stream, err := client.Open()
  34. if err != nil {
  35. b.Fatalf("err: %v", err)
  36. }
  37. stream.Close()
  38. }
  39. }
  40. func BenchmarkSendRecv(b *testing.B) {
  41. client, server := testClientServer()
  42. defer client.Close()
  43. defer server.Close()
  44. sendBuf := make([]byte, 512)
  45. recvBuf := make([]byte, 512)
  46. doneCh := make(chan struct{})
  47. go func() {
  48. stream, err := server.AcceptStream()
  49. if err != nil {
  50. return
  51. }
  52. defer stream.Close()
  53. for i := 0; i < b.N; i++ {
  54. if _, err := stream.Read(recvBuf); err != nil {
  55. b.Fatalf("err: %v", err)
  56. }
  57. }
  58. close(doneCh)
  59. }()
  60. stream, err := client.Open()
  61. if err != nil {
  62. b.Fatalf("err: %v", err)
  63. }
  64. defer stream.Close()
  65. for i := 0; i < b.N; i++ {
  66. if _, err := stream.Write(sendBuf); err != nil {
  67. b.Fatalf("err: %v", err)
  68. }
  69. }
  70. <-doneCh
  71. }
  72. func BenchmarkSendRecvLarge(b *testing.B) {
  73. client, server := testClientServer()
  74. defer client.Close()
  75. defer server.Close()
  76. const sendSize = 512 * 1024 * 1024
  77. const recvSize = 4 * 1024
  78. sendBuf := make([]byte, sendSize)
  79. recvBuf := make([]byte, recvSize)
  80. b.ResetTimer()
  81. recvDone := make(chan struct{})
  82. go func() {
  83. stream, err := server.AcceptStream()
  84. if err != nil {
  85. return
  86. }
  87. defer stream.Close()
  88. for i := 0; i < b.N; i++ {
  89. for j := 0; j < sendSize/recvSize; j++ {
  90. if _, err := stream.Read(recvBuf); err != nil {
  91. b.Fatalf("err: %v", err)
  92. }
  93. }
  94. }
  95. close(recvDone)
  96. }()
  97. stream, err := client.Open()
  98. if err != nil {
  99. b.Fatalf("err: %v", err)
  100. }
  101. defer stream.Close()
  102. for i := 0; i < b.N; i++ {
  103. if _, err := stream.Write(sendBuf); err != nil {
  104. b.Fatalf("err: %v", err)
  105. }
  106. }
  107. <-recvDone
  108. }