github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/testEnv/benchs/edge_test.go (about) 1 package benchs_test 2 3 import ( 4 "context" 5 "flag" 6 "os" 7 "sync/atomic" 8 "testing" 9 "time" 10 11 "github.com/ronaksoft/rony" 12 "github.com/ronaksoft/rony/edge" 13 "github.com/ronaksoft/rony/edgec" 14 "github.com/ronaksoft/rony/internal/testEnv" 15 "github.com/ronaksoft/rony/internal/testEnv/pb/service" 16 "github.com/ronaksoft/rony/log" 17 "github.com/ronaksoft/rony/pools" 18 "github.com/valyala/fasthttp" 19 "google.golang.org/protobuf/proto" 20 ) 21 22 /* 23 Creation Time: 2020 - Dec - 28 24 Created by: (ehsan) 25 Maintainers: 26 1. Ehsan N. Moosa (E2) 27 Auditor: Ehsan N. Moosa (E2) 28 Copyright Ronak Software Group 2020 29 */ 30 31 var ( 32 edgeServer *edge.Server 33 ) 34 35 func TestMain(m *testing.M) { 36 edgeServer = testEnv.EdgeServer("Adam", 8080, 100000) 37 rony.SetLogLevel(log.WarnLevel) 38 service.RegisterSample( 39 &service.Sample{ 40 ServerID: edgeServer.GetServerID(), 41 }, 42 edgeServer, 43 ) 44 45 edgeServer.Start() 46 47 flag.Parse() 48 code := m.Run() 49 edgeServer.Shutdown() 50 os.Exit(code) 51 } 52 53 func BenchmarkEdge(b *testing.B) { 54 benches := map[string]func(*testing.B){ 55 "Http-Client": benchHttpClient, 56 "Http-Single-EdgeClient": benchHttpSingleEdgeClient, 57 "SingleWebsocket": benchSingleWebsocketClient, 58 "MultiWebsocket": benchMultiWebsocketClient, 59 } 60 for k, f := range benches { 61 b.Run(k, f) 62 } 63 } 64 func benchHttpClient(b *testing.B) { 65 edgeClient := &fasthttp.HostClient{ 66 Addr: "127.0.0.1:8080", 67 } 68 echoRequest := service.EchoRequest{ 69 Int: 100, 70 Timestamp: 32809238402, 71 } 72 reqID := uint64(1) 73 cntErr := int64(0) 74 cntOK := int64(0) 75 76 b.ResetTimer() 77 b.ReportAllocs() 78 b.SetParallelism(10) 79 b.RunParallel(func(p *testing.PB) { 80 for p.Next() { 81 rid := atomic.AddUint64(&reqID, 1) 82 req := rony.PoolMessageEnvelope.Get() 83 res := rony.PoolMessageEnvelope.Get() 84 req.Fill(rid, service.C_SampleEcho, &echoRequest) 85 err := sendHttp(edgeClient, req, res) 86 if err != nil { 87 atomic.AddInt64(&cntErr, 1) 88 } else { 89 atomic.AddInt64(&cntOK, 1) 90 } 91 92 rony.PoolMessageEnvelope.Put(req) 93 rony.PoolMessageEnvelope.Put(res) 94 } 95 }) 96 b.Log("Failed / OK:", cntErr, "/", cntOK) 97 } 98 func benchHttpSingleEdgeClient(b *testing.B) { 99 edgeClient := edgec.NewHttp(edgec.HttpConfig{ 100 Name: "Benchmark", 101 SeedHostPort: "127.0.0.1:8080", 102 ReadTimeout: time.Second, 103 WriteTimeout: time.Second, 104 ContextTimeout: time.Second, 105 RequestMaxRetry: 10, 106 }) 107 err := edgeClient.Start() 108 if err != nil { 109 b.Fatal(err) 110 } 111 echoRequest := service.EchoRequest{ 112 Int: 100, 113 Timestamp: 32809238402, 114 } 115 cntErr := int64(0) 116 cntOK := int64(0) 117 118 b.ResetTimer() 119 b.ReportAllocs() 120 b.SetParallelism(10) 121 b.RunParallel(func(p *testing.PB) { 122 for p.Next() { 123 req := rony.PoolMessageEnvelope.Get() 124 res := rony.PoolMessageEnvelope.Get() 125 req.Fill(edgeClient.GetRequestID(), service.C_SampleEcho, &echoRequest) 126 err = edgeClient.Send(context.TODO(), req, res) 127 if err != nil { 128 atomic.AddInt64(&cntErr, 1) 129 } else { 130 atomic.AddInt64(&cntOK, 1) 131 } 132 rony.PoolMessageEnvelope.Put(req) 133 rony.PoolMessageEnvelope.Put(res) 134 } 135 }) 136 b.Log("Failed / OK:", cntErr, "/", cntOK) 137 _ = edgeClient.Close() 138 } 139 func benchMultiWebsocketClient(b *testing.B) { 140 echoRequest := service.EchoRequest{ 141 Int: 100, 142 Timestamp: 32809238402, 143 } 144 b.ResetTimer() 145 b.ReportAllocs() 146 // b.SetParallelism(5) 147 b.RunParallel(func(p *testing.PB) { 148 edgeClient := edgec.NewWebsocket(edgec.WebsocketConfig{ 149 SeedHostPort: "127.0.0.1:8080", 150 IdleTimeout: time.Second, 151 DialTimeout: time.Second, 152 Handler: func(m *rony.MessageEnvelope) {}, 153 RequestMaxRetry: 10, 154 RequestTimeout: time.Second, 155 // ContextTimeout: time.Second, 156 }) 157 err := edgeClient.Start() 158 if err != nil { 159 b.Fatal(err) 160 } 161 for p.Next() { 162 req := rony.PoolMessageEnvelope.Get() 163 res := rony.PoolMessageEnvelope.Get() 164 req.Fill(edgeClient.GetRequestID(), service.C_SampleEcho, &echoRequest) 165 _ = edgeClient.Send(context.TODO(), req, res) 166 rony.PoolMessageEnvelope.Put(req) 167 rony.PoolMessageEnvelope.Put(res) 168 } 169 _ = edgeClient.Close() 170 }) 171 } 172 func benchSingleWebsocketClient(b *testing.B) { 173 edgeClient := edgec.NewWebsocket(edgec.WebsocketConfig{ 174 SeedHostPort: "127.0.0.1:8080", 175 IdleTimeout: time.Second, 176 DialTimeout: time.Second, 177 Handler: func(m *rony.MessageEnvelope) {}, 178 RequestMaxRetry: 10, 179 RequestTimeout: time.Second, 180 // ContextTimeout: time.Second, 181 }) 182 183 if err := edgeClient.Start(); err != nil { 184 b.Fatal(err) 185 } 186 echoRequest := service.EchoRequest{ 187 Int: 100, 188 Timestamp: 32809238402, 189 } 190 191 b.ResetTimer() 192 b.ReportAllocs() 193 b.SetParallelism(10) 194 b.RunParallel(func(p *testing.PB) { 195 for p.Next() { 196 req := rony.PoolMessageEnvelope.Get() 197 res := rony.PoolMessageEnvelope.Get() 198 req.Fill(edgeClient.GetRequestID(), service.C_SampleEcho, &echoRequest) 199 _ = edgeClient.Send(context.TODO(), req, res) 200 rony.PoolMessageEnvelope.Put(req) 201 rony.PoolMessageEnvelope.Put(res) 202 } 203 }) 204 _ = edgeClient.Close() 205 } 206 207 func sendHttp(c *fasthttp.HostClient, req, res *rony.MessageEnvelope) error { 208 mo := proto.MarshalOptions{UseCachedSize: true} 209 b := pools.Buffer.GetCap(mo.Size(req)) 210 *b.Bytes(), _ = mo.MarshalAppend(*b.Bytes(), req) 211 httpReq := fasthttp.AcquireRequest() 212 httpRes := fasthttp.AcquireResponse() 213 httpReq.Header.SetHost("127.0.0.1:8080") 214 httpReq.Header.SetMethod(fasthttp.MethodPost) 215 httpReq.SetBody(*b.Bytes()) 216 err := c.Do(httpReq, httpRes) 217 if err != nil { 218 return err 219 } 220 err = res.Unmarshal(httpRes.Body()) 221 222 return err 223 }