-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnettest.go
More file actions
48 lines (41 loc) · 942 Bytes
/
nettest.go
File metadata and controls
48 lines (41 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"time"
)
var mode = flag.String("mode", "client", "mode为server或者client")
var host = flag.String("host", "localhost", "服务器ip")
func main() {
flag.Parse()
if *mode == "server" {
server()
} else {
client(*host)
}
}
func server() {
log.Println("start server :8000")
http.HandleFunc("/", indexHandler)
http.ListenAndServe(":8000", nil)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("%s\n", r.RemoteAddr)
w.Write([]byte("hello world"))
}
func client(host string) {
start := now()
resp, err := http.Get("http://" + host + ":8000/")
end := now()
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
log.Printf("耗时毫秒:%v,微秒:%v,纳秒:%v\n响应:%s\n", (end-start)/1000000, (end-start)/1000, (end - start), body)
}
func now() int64 {
return time.Now().UnixNano()
}