Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Aug 16, 2021
1 parent af8c150 commit f7486b0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
11 changes: 9 additions & 2 deletions examples/bench/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"crypto/rand"
"encoding/base64"
"log"
"net"
"runtime"
Expand Down Expand Up @@ -38,6 +40,8 @@ func main() {
eachClientCoroutineNum = 10
)

arpc.EnablePool(true)

clients := make([]*arpc.Client, clientNum)

for i := 0; i < clientNum; i++ {
Expand All @@ -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)
}
}
Expand Down
2 changes: 2 additions & 0 deletions examples/bench/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 9 additions & 2 deletions examples/bench_nbio/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"crypto/rand"
"encoding/base64"
"log"
"net"
"runtime"
Expand Down Expand Up @@ -38,6 +40,8 @@ func main() {
eachClientCoroutineNum = 10
)

arpc.EnablePool(true)

clients := make([]*arpc.Client, clientNum)

for i := 0; i < clientNum; i++ {
Expand All @@ -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)
}
}
Expand Down
7 changes: 7 additions & 0 deletions examples/bench_nbio/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions examples/nbio/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 {
Expand Down

0 comments on commit f7486b0

Please sign in to comment.