Created
January 27, 2017 08:41
-
-
Save funny-falcon/4342be67d4eb385998d5f78e4f0a594e to your computer and use it in GitHub Desktop.
simple go-rpc bench
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"net/rpc" | |
"sync" | |
"time" | |
) | |
var ( | |
Num int | |
Conn int | |
Addr string | |
RS int | |
) | |
func init() { | |
flag.IntVar(&Num, "gor", 1, "goroutine concurrency") | |
flag.IntVar(&Conn, "conn", 1, "connection concurrency") | |
flag.IntVar(&RS, "req", 1000, "request per goroutine") | |
flag.StringVar(&Addr, "addr", "127.0.0.1", "") | |
flag.Parse() | |
} | |
func main() { | |
clients := make([]*rpc.Client, 0, 10) | |
for i := 0; i < Conn; i++ { | |
client, err := rpc.Dial("tcp", Addr+":8000") | |
if err != nil { | |
log.Fatalln(err) | |
} | |
clients = append(clients, client) | |
} | |
index := 0 | |
s1 := "aaaaaaaaaaaaaaaaa" | |
var wg sync.WaitGroup | |
start := time.Now() | |
for i := 0; i < Num; i++ { | |
wg.Add(1) | |
var c *rpc.Client | |
if index < len(clients) { | |
c = clients[index] | |
index++ | |
} else { | |
index = 0 | |
c = clients[index] | |
} | |
go func(cli *rpc.Client) { | |
defer wg.Done() | |
for n := 0; n < RS; n++ { | |
if err := cli.Call("EchoServer.Echo", &s1, nil); err != nil { | |
log.Println(err) | |
} | |
} | |
}(c) | |
} | |
wg.Wait() | |
total := RS * Num | |
secs := time.Now().Sub(start).Seconds() | |
fmt.Printf("goroutines: %d\n", Num) | |
fmt.Printf("connections: %d\n", Num) | |
fmt.Printf("total: %d\n", total) | |
fmt.Printf("seconds: %.3f\n", secs) | |
fmt.Printf("qps: %d\n", int(float64(total)/secs)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"net/rpc" | |
) | |
type EchoServer bool | |
func (s *EchoServer) Echo(req *string, res *string) error { | |
return nil | |
} | |
func main() { | |
echo := new(EchoServer) | |
if err := rpc.Register(echo); err != nil { | |
log.Println(err) | |
return | |
} | |
var listener *net.TCPListener | |
if tcpAddr, err := net.ResolveTCPAddr("tcp", ":8000"); err != nil { | |
log.Println(err) | |
return | |
} else { | |
if listener, err = net.ListenTCP("tcp", tcpAddr); err != nil { | |
log.Println(err) | |
return | |
} | |
} | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
log.Println(err) | |
continue | |
} | |
go rpc.ServeConn(conn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment