Skip to content

Instantly share code, notes, and snippets.

@simpx
Created September 11, 2015 04:22
Show Gist options
  • Save simpx/3999157a7d52ce39d85f to your computer and use it in GitHub Desktop.
Save simpx/3999157a7d52ce39d85f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"flag"
"log"
"net"
"io"
"time"
)
var (
Host = flag.String("h", "127.0.0.1", "server host")
Port = flag.Int("p", 9876, "server port")
RequestSize = flag.Int("r", 4096, "request size")
ResponseSize = flag.Int("s", 4096, "response size")
RequestNum = flag.Int("n", 1000, "request num")
)
func init() {
flag.Parse()
log.SetFlags(log.Flags() | log.Lshortfile)
}
func main() {
log.Printf("host %s port %d, request size %d, response size %d, request num %d\n", *Host, *Port, *RequestSize, *ResponseSize, *RequestNum)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", *Host, *Port))
if err != nil {
log.Fatal("connect fail ", err)
}
request := make([]byte, *RequestSize)
response := make([]byte, *ResponseSize)
var rtt int64 = 0
i := 0
for i < *RequestNum {
i += 1
_, err := conn.Write(request)
var start int64 = time.Now().UnixNano()
if err != nil {
log.Println("write fail ", err)
break
}
_, err = io.ReadFull(conn, response)
rtt += (time.Now().UnixNano() - start)
if err != nil {
log.Println("read fail ", err)
break
}
}
log.Printf("%d request done, avg rtt %f ms\n", *RequestNum, float64(rtt) / 1000 / float64(*RequestNum))
}
package main
import (
"fmt"
"flag"
"log"
"net"
"io"
)
var (
Port = flag.Int("p", 9876, "server port")
RequestSize = flag.Int("r", 4096, "request size")
ResponseSize = flag.Int("s", 4096, "response size")
)
func handleConn(conn net.Conn) {
defer conn.Close()
request := make([]byte, *RequestSize)
response := make([]byte, *ResponseSize)
for {
_, err := io.ReadFull(conn, request)
if (err != nil) {
log.Println("read fail: ", err)
break;
}
conn.Write(response)
}
}
func main() {
flag.Parse()
log.SetFlags(log.Flags() | log.Lshortfile)
log.Printf("port %d, request size %d, response size %d\n", *Port, *RequestSize, *ResponseSize)
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", *Port))
if err != nil {
log.Fatal("listen fail ", err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal("accept fail ", err)
}
go handleConn(conn)
}
}
@simpx
Copy link
Author

simpx commented Sep 11, 2015

env

redhat 5, kernel 2.6.32
go version go1.4.2 linux/amd64
go version go1.5.1 linux/amd64
binary *_1.5 is build by golang 1.5.1
binary *_1.4 is build by golang 1.4.2

1.5 server & 1.5 client

./server_1.5 -r 1024 -s 1024
./client_1.5 -r 1024 -s 1024 -n 10000
2015/09/11 12:30:02 client.go:30: host 127.0.0.1 port 9876, request size 1024, response size 1024, request num 10000
2015/09/11 12:30:03 client.go:59: 10000 request done, avg rtt 32.984351 ms

1.5 server & 1.4 client

./server_1.5 -r 1024 -s 1024
./client_1.4 -r 1024 -s 1024 -n 10000
2015/09/11 12:31:40 client.go:30: host 127.0.0.1 port 9876, request size 1024, response size 1024, request num 10000
2015/09/11 12:31:40 client.go:59: 10000 request done, avg rtt 30.749519 ms

1.4 server & 1.5 client

./server_1.4 -r 1024 -s 1024
./client_1.5 -r 1024 -s 1024 -n 10000
2015/09/11 12:33:44 client.go:30: host 127.0.0.1 port 9876, request size 1024, response size 1024, request num 10000
2015/09/11 12:33:44 client.go:59: 10000 request done, avg rtt 27.982606 ms

1.4 server & 1.4 client

./server_1.4 -r 1024 -s 1024
./client_1.4 -r 1024 -s 1024 -n 10000
2015/09/11 12:34:20 client.go:30: host 127.0.0.1 port 9876, request size 1024, response size 1024, request num 10000
2015/09/11 12:34:20 client.go:59: 10000 request done, avg rtt 25.945010 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment