Created
September 11, 2015 04:22
-
-
Save simpx/3999157a7d52ce39d85f to your computer and use it in GitHub Desktop.
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 ( | |
"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)) | |
} |
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 ( | |
"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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
1.5 server & 1.4 client
1.4 server & 1.5 client
1.4 server & 1.4 client