-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
53 lines (43 loc) · 1.18 KB
/
util.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
package httpbench
import (
"regexp"
"sort"
"time"
)
func IsValidURL(url string) bool {
var urlRegex = regexp.MustCompile(`^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$`)
return urlRegex.MatchString(url)
}
func CalculateStatistics(responses []HTTPResponse) Statistics {
stats := Statistics{}
stats.TotalCalls = len(responses)
var totalLatency int64 = 0
twoHundreds := 0
threeHundreds := 0
fourHundreds := 0
fiveHundreds := 0
sort.Slice(responses, func(i, j int) bool { return responses[i].Latency < responses[j].Latency })
stats.FastestRequest = responses[0].Latency
stats.SlowestRequest = responses[len(responses)-1].Latency
for _, v := range responses {
totalLatency = totalLatency + int64(v.Latency)
switch v.Status {
case 200:
twoHundreds++
case 300, 302:
threeHundreds++
case 400:
fourHundreds++
case 500:
fiveHundreds++
}
}
avgLatency := totalLatency / int64(len(responses))
stats.AvgTimePerRequest = time.Duration(avgLatency)
stats.TotalTime = time.Duration(totalLatency)
stats.TwoHundredResponses = twoHundreds
stats.ThreeHundredResponses = threeHundreds
stats.FourHundredResponses = fourHundreds
stats.FiveHundredResponses = fiveHundreds
return stats
}