-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpPerf.go
62 lines (57 loc) · 1.57 KB
/
httpPerf.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bytes"
// "fmt"
"io"
"net"
"net/http"
"log"
"time"
)
func main() {
hostname := "vps.monkey.id.au"
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
request, err := http.NewRequest("GET", "http://"+hostname+"/info.php", nil)
request.Header.Set("Connection", "close")
if err != nil {
log.Fatal(err.Error())
}
log.Printf("dialing host %s", hostname)
start := time.Now()
conn, err := net.Dial("tcp", hostname + ":80")
defer conn.Close()
if err != nil {
log.Fatal(err.Error())
}
connect := time.Since(start)
log.Printf("connection established")
log.Printf("sending request")
step := time.Now()
if err := request.Write(conn); err != nil {
log.Fatal(err.Error())
}
send := time.Since(step)
step = time.Now()
log.Printf("request sent")
log.Printf("waiting for first byte")
firstByte := make([]byte, 1)
bytesRead, err := conn.Read(firstByte)
if err != nil {
log.Fatal(err.Error())
}
ttfb := time.Since(step)
step = time.Now()
log.Printf("first byte read. byte_length: %d", bytesRead)
log.Printf("reading rest of payload")
var buf bytes.Buffer
buf.Write(firstByte)
_, err = io.Copy(&buf, conn)
if err != nil {
log.Fatal(err.Error())
}
ttlb := time.Since(step)
total := time.Since(start)
log.Printf("all bytes read. byte_length: %d", buf.Len())
log.Printf("\nConnect: %s\nSend: %s\nWait: %s\nRecv: %s\nTotal: %s\n",
connect, send, ttfb, ttlb, total)
}