-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
108 lines (103 loc) · 2.7 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
package main
import (
"fmt"
"github.com/someshkoli/httpreq/pkg/http"
"github.com/spf13/pflag"
)
type reqPair struct {
request http.Request
response *http.Response
}
func run(url string, count int64, full bool) {
var fastest, slowest, timesum int64
fastest = -9223372036854775808
slowest = 9223372036854775807
timesum = 0
smallest := 4294967295
biggest := 0
var failed []reqPair
var success []reqPair
var total []reqPair
if count == 1 {
req, err := http.MakeRequest(url)
if err != nil {
fmt.Println(err)
}
res := req.Call()
if res.Err != nil {
fmt.Println(res.Err)
fmt.Println("Use --help for more options")
return
}
if full {
fmt.Println(res.Data)
return
}
fmt.Println(res.Body)
return
}
for i := 0; i < int(count); i++ {
req, err := http.MakeRequest(url)
if err != nil {
fmt.Println(err)
}
res := req.Call()
if res.Err != nil {
fmt.Println(res.Err)
fmt.Println("Use --help for more options")
return
}
pair := reqPair{request: req, response: res}
total = append(total, pair)
if res.Status != 200 {
failed = append(failed, pair)
} else {
success = append(success, pair)
}
if res.Time.Milliseconds() < slowest {
slowest = res.Time.Milliseconds()
}
if res.Time.Milliseconds() > fastest {
fastest = res.Time.Milliseconds()
}
if len(res.Body) < smallest {
smallest = len(pair.response.Body)
}
if len(res.Body) > biggest {
biggest = len(pair.response.Body)
}
timesum += res.Time.Milliseconds()
}
mean := float64(timesum) / float64(count)
percentage := float64(len(success)) / float64(count)
median := total[(count+1)/2].response.Time
fmt.Println("+++++++++++++++++++++++++")
fmt.Printf("URL: %s\n", url)
fmt.Printf("Profile: %d\n", count)
fmt.Println("+++++++++++++++++++++++++")
fmt.Printf("Mean of all response time: %f ms\n", mean)
fmt.Printf("Median of all response time: %d ms\n", median)
fmt.Printf("Percentage of successfull response: %d%%\n", int(percentage*100))
fmt.Printf("Request with fastest response: %d \n", fastest)
fmt.Printf("Request with slowest response: %d \n", slowest)
fmt.Printf("Request with biggest size: %d \n", biggest)
fmt.Printf("Request with smallest size: %d \n", smallest)
if len(failed) != 0 {
fmt.Printf("Failed Responses: ")
for _, f := range failed {
fmt.Printf("%d ", f.response.Status)
}
fmt.Print("\n")
}
fmt.Println("+++++++++++++++++++++++++")
}
func main() {
var url string
var full bool
var profile int64
pflag.StringVar(&url, "url", "", "URL to make HTTP request to.")
pflag.Int64Var(&profile, "profile", 1, "Number of requests to make.")
pflag.BoolVar(&full, "full", false, "If mentioned prints full response.")
pflag.Parse()
run(url, profile, full)
}