github.com/criyle/go-sandbox@v0.10.3/pkg/unixsocket/benchmark_linux_test.go (about) 1 package unixsocket 2 3 import "testing" 4 5 func BenchmarkBaseline(b *testing.B) { 6 s, t, err := NewSocketPair() 7 if err != nil { 8 b.Fatal(err) 9 } 10 m := make([]byte, 1024) 11 b.ResetTimer() 12 go func() { 13 msg := []byte("message") 14 for i := 0; i < b.N; i++ { 15 s.SendMsg(msg, Msg{}) 16 } 17 }() 18 19 for i := 0; i < b.N; i++ { 20 t.RecvMsg(m) 21 } 22 } 23 24 func BenchmarkGoroutine(b *testing.B) { 25 s, t, err := NewSocketPair() 26 if err != nil { 27 b.Fatal(err) 28 } 29 m := make([]byte, 1024) 30 b.ResetTimer() 31 go func() { 32 msg := []byte("message") 33 for i := 0; i < b.N; i++ { 34 s.SendMsg(msg, Msg{}) 35 } 36 }() 37 38 for i := 0; i < b.N; i++ { 39 c := make(chan struct{}) 40 go func() { 41 defer close(c) 42 t.RecvMsg(m) 43 }() 44 <-c 45 } 46 } 47 48 func BenchmarkChannel(b *testing.B) { 49 c := make(chan []byte) 50 benchGoroutine(b, c) 51 } 52 53 func BenchmarkChannelBuffed(b *testing.B) { 54 c := make(chan []byte, 1) 55 benchGoroutine(b, c) 56 } 57 58 func BenchmarkChannelBuffed4(b *testing.B) { 59 c := make(chan []byte, 4) 60 benchGoroutine(b, c) 61 } 62 63 func BenchmarkEmptyGoroutine(b *testing.B) { 64 for i := 0; i < b.N; i++ { 65 c := make(chan struct{}) 66 go func() { 67 close(c) 68 }() 69 <-c 70 } 71 } 72 73 func benchGoroutine(b *testing.B, c chan []byte) { 74 s, t, err := NewSocketPair() 75 if err != nil { 76 b.Fatal(err) 77 } 78 79 go func() { 80 msg := []byte("message") 81 for i := 0; i < b.N; i++ { 82 s.SendMsg(msg, Msg{}) 83 } 84 }() 85 86 b.ResetTimer() 87 go func() { 88 m := make([]byte, 1024) 89 for i := 0; i < b.N; i++ { 90 t.RecvMsg(m) 91 c <- m 92 } 93 }() 94 95 for i := 0; i < b.N; i++ { 96 <-c 97 } 98 }