-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
183 lines (154 loc) · 4.37 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bufio"
"bytes"
"container/heap"
"fmt"
"io"
"net"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/pborman/getopt"
myheap "github.com/rahulnakre/systems_assignment/heap"
)
func main() {
// NOTE: For a faster median calulation, it's better to do the 2 heap approach
// however this week was a bit hectic so I can't get the time to implement this
minHeap := &myheap.MinHeap{}
heap.Init(minHeap)
maxHeap := &myheap.MaxHeap{}
heap.Init(maxHeap)
urlFlag := getopt.StringLong("url", 0, "", "url of a site")
helpFlag := getopt.BoolLong("help", 0, "Help")
profileFlag := getopt.Int64Long("profile", 0, -1, "Profile: an integer >= 1")
showStats := true
getopt.Parse()
if *helpFlag {
getopt.Usage()
os.Exit(0)
}
u, err := url.Parse(*urlFlag)
checkError(err)
if len(u.Path) == 0 {
u.Path = "/"
}
if *profileFlag < 1 {
*profileFlag = 1
showStats = false
}
var slowestResTime, fastestResTime float64 = -1, -1
var largestResSize, smallestResSize int64 = -1, -1
var totalTime float64 = 0
var successCount int64 = 0
var errorCodesArr []int
var resTimeArr []float64
var printCount int = 0
for i := int64(0); i < *profileFlag; i++ {
startTime := time.Now()
conn, err := net.Dial("tcp", u.Host+":http")
checkError(err)
defer conn.Close()
_, err = fmt.Fprintf(conn, "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", u.Path, u.Host)
checkError(err)
res, totalBytesRead, statusCode, err := Read(conn)
checkError(err)
endTime := time.Now()
// only print the body once
if printCount == 0 {
fmt.Printf(string(res))
printCount++
}
if statusCode >= 200 && statusCode <= 299 {
successCount++
}
if statusCode >= 400 && statusCode <= 599 {
errorCodesArr = append(errorCodesArr, statusCode)
}
resTime := endTime.Sub(startTime).Seconds()
resTimeArr = append(resTimeArr, resTime)
if i == 0 || resTime < fastestResTime {
fastestResTime = resTime
}
if i == 0 || resTime > slowestResTime {
slowestResTime = resTime
}
totalTime += resTime
if i == 0 || totalBytesRead < smallestResSize {
smallestResSize = totalBytesRead
}
if i == 0 || totalBytesRead > largestResSize {
largestResSize = totalBytesRead
}
}
if !showStats {
return
}
sort.Float64s(resTimeArr)
var median float64
if len(resTimeArr)%2 == 0 {
median = float64(resTimeArr[len(resTimeArr)/2]+resTimeArr[(len(resTimeArr)/2)-1]) / float64(2)
} else {
median = resTimeArr[len(resTimeArr)/2]
}
fmt.Printf("Number of requests: %d\n", *profileFlag)
fmt.Printf("Fastest Response Time: %f\n", fastestResTime)
fmt.Printf("Slowest Response Time: %f\n", slowestResTime)
fmt.Printf("Mean Response Time: %f\n", float64(totalTime)/float64(*profileFlag))
fmt.Printf("Median Response Time: %f\n", median)
fmt.Printf("Percentage of Successful Requests: %f%%\n", float64(successCount)/float64(*profileFlag)*100)
fmt.Printf("Error response codes: %v\n", errorCodesArr)
fmt.Printf("Size of smallest response (in bytes): %d\n", smallestResSize)
fmt.Printf("Size of largest response (in bytes): %d\n", largestResSize)
}
// Read reads from a connection. Avoided io.ReadAll() because I don't want the entire response
// in memory. NOTE: some responses have a larger byte size than one reported by curl - %{size_download}
// And the same can be reproduced using io.ReadAll(). Interestingly, http.Get{} does not seem to suffer
// from this issue. Next time, I would fix this
func Read(conn net.Conn) (string, int64, int, error) {
reader := bufio.NewReader(conn)
var buffer bytes.Buffer
var i int = -1
var statusCode int
var totalBytesRead int64 = 0
var headerDone bool = false
var isPrevDelim bool = false
for {
i++
bytesArr, err := reader.ReadBytes('\n')
if headerDone {
totalBytesRead += int64(len(bytesArr))
}
if !headerDone {
if strings.EqualFold(string(bytesArr), "\r\n") && isPrevDelim {
totalBytesRead = 0
headerDone = true
continue
}
if strings.Contains(string(bytesArr), "\r\n") {
isPrevDelim = true
}
if i == 0 {
statusCode, err = strconv.Atoi(strings.Split(string(bytesArr), " ")[1])
}
} else {
buffer.Write(bytesArr)
}
if err != nil {
if err == io.EOF {
break
}
return "", totalBytesRead, -1, err
}
}
return buffer.String(), totalBytesRead, statusCode, nil
}
func checkError(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(0)
}
}