From f7486b0ccb30ec30631932ede003f2e730c5ec7b Mon Sep 17 00:00:00 2001 From: lesismal Date: Mon, 16 Aug 2021 17:05:13 +0800 Subject: [PATCH] update examples --- examples/bench/client/client.go | 11 +++++++++-- examples/bench/server/server.go | 2 ++ examples/bench_nbio/client/client.go | 11 +++++++++-- examples/bench_nbio/server/server.go | 7 +++++++ examples/nbio/server/server.go | 5 +++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/examples/bench/client/client.go b/examples/bench/client/client.go index 4301105..1a492cb 100644 --- a/examples/bench/client/client.go +++ b/examples/bench/client/client.go @@ -1,6 +1,8 @@ package main import ( + "crypto/rand" + "encoding/base64" "log" "net" "runtime" @@ -38,6 +40,8 @@ func main() { eachClientCoroutineNum = 10 ) + arpc.EnablePool(true) + clients := make([]*arpc.Client, clientNum) for i := 0; i < clientNum; i++ { @@ -55,14 +59,17 @@ func main() { for j := 0; j < eachClientCoroutineNum; j++ { go func() { var err error + var data = make([]byte, 512) for k := 0; true; k++ { - req := &HelloReq{Msg: "hello from client.Call"} + rand.Read(data) + req := &HelloReq{Msg: base64.RawStdEncoding.EncodeToString(data)} rsp := &HelloRsp{} err = client.Call(method, req, rsp, time.Second*5) if err != nil { log.Printf("Call failed: %v", err) + } else if rsp.Msg != req.Msg { + log.Fatal("Call failed: not equal") } else { - //log.Printf("Call Response: \"%v\"", rsp.Msg) atomic.AddUint64(&qpsSec, 1) } } diff --git a/examples/bench/server/server.go b/examples/bench/server/server.go index 9817317..c340dff 100644 --- a/examples/bench/server/server.go +++ b/examples/bench/server/server.go @@ -35,6 +35,8 @@ func main() { } svr := arpc.NewServer() + svr.Handler.EnablePool(true) + svr.Handler.SetAsyncResponse(true) svr.Handler.Handle("Hello", OnHello) svr.Serve(ln) } diff --git a/examples/bench_nbio/client/client.go b/examples/bench_nbio/client/client.go index 4301105..1a492cb 100644 --- a/examples/bench_nbio/client/client.go +++ b/examples/bench_nbio/client/client.go @@ -1,6 +1,8 @@ package main import ( + "crypto/rand" + "encoding/base64" "log" "net" "runtime" @@ -38,6 +40,8 @@ func main() { eachClientCoroutineNum = 10 ) + arpc.EnablePool(true) + clients := make([]*arpc.Client, clientNum) for i := 0; i < clientNum; i++ { @@ -55,14 +59,17 @@ func main() { for j := 0; j < eachClientCoroutineNum; j++ { go func() { var err error + var data = make([]byte, 512) for k := 0; true; k++ { - req := &HelloReq{Msg: "hello from client.Call"} + rand.Read(data) + req := &HelloReq{Msg: base64.RawStdEncoding.EncodeToString(data)} rsp := &HelloRsp{} err = client.Call(method, req, rsp, time.Second*5) if err != nil { log.Printf("Call failed: %v", err) + } else if rsp.Msg != req.Msg { + log.Fatal("Call failed: not equal") } else { - //log.Printf("Call Response: \"%v\"", rsp.Msg) atomic.AddUint64(&qpsSec, 1) } } diff --git a/examples/bench_nbio/server/server.go b/examples/bench_nbio/server/server.go index 8212d6b..3322e6c 100644 --- a/examples/bench_nbio/server/server.go +++ b/examples/bench_nbio/server/server.go @@ -70,7 +70,11 @@ func onData(c *nbio.Conn, data []byte) { func main() { nlog.SetLogger(log.DefaultLogger) + arpc.BufferPool = mempool.DefaultMemPool + + handler.EnablePool(true) handler.SetAsyncWrite(false) + handler.SetAsyncResponse(true) // register router handler.Handle("Hello", func(ctx *arpc.Context) { @@ -86,6 +90,9 @@ func main() { g.OnOpen(onOpen) g.OnData(onData) + g.OnWriteBufferRelease(func(c *nbio.Conn, b []byte) { + mempool.Free(b) + }) err := g.Start() if err != nil { diff --git a/examples/nbio/server/server.go b/examples/nbio/server/server.go index 960d545..60b60eb 100644 --- a/examples/nbio/server/server.go +++ b/examples/nbio/server/server.go @@ -60,6 +60,8 @@ func onData(c *nbio.Conn, data []byte) { func main() { nlog.SetLogger(log.DefaultLogger) + arpc.BufferPool = mempool.DefaultMemPool + handler.EnablePool(true) handler.SetAsyncWrite(false) @@ -83,6 +85,9 @@ func main() { g.OnOpen(onOpen) g.OnData(onData) + g.OnWriteBufferRelease(func(c *nbio.Conn, b []byte) { + mempool.Free(b) + }) err := g.Start() if err != nil {