nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/test/benchmark_test.go (about) 1 // Copyright 2018 The Mangos Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use file except in compliance with the License. 5 // You may obtain a copy of the license at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "strings" 19 "testing" 20 "time" 21 22 "nanomsg.org/go/mangos/v2" 23 "nanomsg.org/go/mangos/v2/protocol/pair" 24 "nanomsg.org/go/mangos/v2/protocol/rep" 25 "nanomsg.org/go/mangos/v2/protocol/req" 26 "nanomsg.org/go/mangos/v2/transport/all" 27 ) 28 29 func benchmarkReq(t *testing.B, url string, size int) { 30 31 srvopts := make(map[string]interface{}) 32 cliopts := make(map[string]interface{}) 33 34 if strings.HasPrefix(url, "wss://") || strings.HasPrefix(url, "tls+tcp://") { 35 srvopts[mangos.OptionTLSConfig] = srvCfg 36 cliopts[mangos.OptionTLSConfig] = cliCfg 37 } 38 srvrdy := make(chan struct{}) 39 srvsock, err := rep.NewSocket() 40 if err != nil || srvsock == nil { 41 t.Errorf("Failed creating server socket: %v", err) 42 return 43 } 44 defer srvsock.Close() 45 46 all.AddTransports(srvsock) 47 48 clisock, err := req.NewSocket() 49 if err != nil || clisock == nil { 50 t.Errorf("Failed creating client socket: %v", err) 51 return 52 } 53 defer clisock.Close() 54 all.AddTransports(clisock) 55 56 go func() { 57 var err error 58 var msg *mangos.Message 59 60 if err = srvsock.ListenOptions(url, srvopts); err != nil { 61 t.Errorf("Server listen failed: %v", err) 62 return 63 } 64 65 close(srvrdy) 66 // echo server 67 68 for { 69 if msg, err = srvsock.RecvMsg(); err != nil { 70 return 71 } 72 if err = srvsock.SendMsg(msg); err != nil { 73 t.Errorf("Server send failed: %v", err) 74 return 75 } 76 } 77 78 }() 79 80 <-srvrdy 81 82 if err = clisock.DialOptions(url, cliopts); err != nil { 83 t.Errorf("Client dial failed: %v", err) 84 return 85 } 86 87 time.Sleep(time.Millisecond * 1000) 88 t.ResetTimer() 89 msg := make([]byte, size) 90 91 for i := 0; i < t.N; i++ { 92 if err = clisock.Send(msg); err != nil { 93 t.Errorf("Client send failed: %v", err) 94 return 95 } 96 if msg, err = clisock.Recv(); err != nil { 97 t.Errorf("Client receive failed: %v", err) 98 return 99 } 100 } 101 if size > 128 { 102 t.SetBytes(int64(size)) 103 } 104 t.StopTimer() 105 } 106 107 func benchmarkPair(t *testing.B, url string, size int) { 108 109 srvopts := make(map[string]interface{}) 110 cliopts := make(map[string]interface{}) 111 112 if strings.HasPrefix(url, "wss://") || strings.HasPrefix(url, "tls+tcp://") { 113 srvopts[mangos.OptionTLSConfig] = srvCfg 114 cliopts[mangos.OptionTLSConfig] = cliCfg 115 } 116 117 finish := make(chan struct{}) 118 ready := make(chan struct{}) 119 srvsock, err := pair.NewSocket() 120 if err != nil || srvsock == nil { 121 t.Errorf("Failed creating server socket: %v", err) 122 return 123 } 124 all.AddTransports(srvsock) 125 defer srvsock.Close() 126 127 clisock, err := pair.NewSocket() 128 if err != nil || clisock == nil { 129 t.Errorf("Failed creating client socket: %v", err) 130 return 131 } 132 all.AddTransports(clisock) 133 defer clisock.Close() 134 135 go func() { 136 var err error 137 var m *mangos.Message 138 139 if err = srvsock.ListenOptions(url, srvopts); err != nil { 140 t.Errorf("Server listen failed: %v", err) 141 return 142 } 143 close(ready) 144 for i := 0; i < t.N; i++ { 145 if m, err = srvsock.RecvMsg(); err != nil { 146 t.Errorf("Error receiving %d: %v", i, err) 147 return 148 } 149 m.Free() 150 } 151 close(finish) 152 153 }() 154 <-ready 155 156 if err = clisock.DialOptions(url, cliopts); err != nil { 157 t.Errorf("Client dial failed: %v", err) 158 return 159 } 160 161 time.Sleep(700 * time.Millisecond) 162 t.ResetTimer() 163 164 for i := 0; i < t.N; i++ { 165 msg := mangos.NewMessage(size) 166 if err = clisock.SendMsg(msg); err != nil { 167 t.Errorf("Client send failed: %v", err) 168 return 169 } 170 } 171 <-finish 172 t.StopTimer() 173 if size > 128 { 174 t.SetBytes(int64(size)) 175 } 176 } 177 178 var benchInpAddr = "inproc://benchmark_test" 179 var benchTCPAddr = "tcp://127.0.0.1:33833" 180 var benchIPCAddr = "ipc://benchmark_test_sock" 181 var benchTLSAddr = "tls+tcp://127.0.0.1:44844" 182 var benchWSAddr = "ws://127.0.0.1:55855/BENCHMARK" 183 var benchWSSAddr = "wss://127.0.0.1:55856/BENCHMARK" 184 185 func BenchmarkLatencyInp(t *testing.B) { 186 benchmarkReq(t, benchInpAddr, 0) 187 } 188 func BenchmarkLatencyIPC(t *testing.B) { 189 benchmarkReq(t, benchIPCAddr, 0) 190 } 191 func BenchmarkLatencyTCP(t *testing.B) { 192 benchmarkReq(t, benchTCPAddr, 0) 193 } 194 func BenchmarkLatencyTLS(t *testing.B) { 195 benchmarkReq(t, benchTLSAddr, 0) 196 } 197 func BenchmarkLatencyWS(t *testing.B) { 198 benchmarkReq(t, benchWSAddr, 0) 199 } 200 func BenchmarkLatencyWSS(t *testing.B) { 201 benchmarkReq(t, benchWSSAddr, 0) 202 } 203 204 func BenchmarkTPut4kInp(t *testing.B) { 205 benchmarkPair(t, benchInpAddr, 4096) 206 } 207 func BenchmarkTPut4kIPC(t *testing.B) { 208 benchmarkPair(t, benchIPCAddr, 4096) 209 } 210 func BenchmarkTPut4kTCP(t *testing.B) { 211 benchmarkPair(t, benchTCPAddr, 4096) 212 } 213 func BenchmarkTPut4kTLS(t *testing.B) { 214 benchmarkPair(t, benchTLSAddr, 4096) 215 } 216 func BenchmarkTPut4kWS(t *testing.B) { 217 benchmarkPair(t, benchWSAddr, 4096) 218 } 219 func BenchmarkTPut4kWSS(t *testing.B) { 220 benchmarkPair(t, benchWSSAddr, 4096) 221 } 222 223 func BenchmarkTPut64kInp(t *testing.B) { 224 benchmarkPair(t, benchInpAddr, 65536) 225 } 226 func BenchmarkTPut64kIPC(t *testing.B) { 227 benchmarkPair(t, benchIPCAddr, 65536) 228 } 229 func BenchmarkTPut64kTCP(t *testing.B) { 230 benchmarkPair(t, benchTCPAddr, 65536) 231 } 232 func BenchmarkTPut64kTLS(t *testing.B) { 233 benchmarkPair(t, benchTLSAddr, 65536) 234 } 235 func BenchmarkTPut64kWS(t *testing.B) { 236 benchmarkPair(t, benchWSAddr, 65536) 237 } 238 func BenchmarkTPut64kWSS(t *testing.B) { 239 benchmarkPair(t, benchWSSAddr, 65536) 240 }