-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.go
122 lines (107 loc) · 2.08 KB
/
results.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
package main
import (
"fmt"
"sort"
"strings"
"time"
)
func showResults(results []update) int {
successful := 0
nonOk := 0
skipped := 0
errors := 0
var totalTime time.Duration
codes := make(map[int]int)
codes[200] = 0
types := make(map[string]int)
for _, v := range results {
if v.Skipped {
skipped++
continue
}
totalTime += v.ResponseTime
if v.Error != nil {
errors++
continue
}
successful++
codes[v.Status]++
if !v.IsOK() {
nonOk++
}
if len(v.ContentType) > 0 {
types[v.ContentType]++
} else {
types["Unknown"]++
}
}
fmt.Println("\nResults:")
fmt.Printf(" %5d total\n", len(results))
fmt.Printf(" %5d successful\n", successful)
fmt.Printf(" %5d errors\n", errors)
fmt.Printf(" %5d skipped\n", skipped)
fmt.Printf("Total time: %s\n", totalTime)
if len(codes) > 1 {
showStatusCodes(codes)
}
if len(types) > 0 {
showContentTypes(types)
}
return nonOk + errors
}
func showStatusCodes(codes map[int]int) {
fmt.Println("\nStatus Codes:")
sortCodes := []struct {
status int
count int
}{}
for s, c := range codes {
sortCodes = append(sortCodes, struct {
status int
count int
}{
status: s,
count: c,
})
}
sort.Slice(sortCodes, func(i int, j int) bool {
a := sortCodes[i].count
b := sortCodes[j].count
if a == b {
return sortCodes[i].status < sortCodes[j].status
}
return a > b
})
for _, v := range sortCodes {
if v.count > 0 {
fmt.Printf(" %5d %d\n", v.count, v.status)
}
}
}
func showContentTypes(types map[string]int) {
fmt.Println("\nContent Types:")
sortTypes := []struct {
contentType string
count int
}{}
for t, c := range types {
sortTypes = append(sortTypes, struct {
contentType string
count int
}{
contentType: t,
count: c,
})
}
sort.Slice(sortTypes, func(i int, j int) bool {
a := sortTypes[i].count
b := sortTypes[j].count
if a == b {
return strings.Compare(sortTypes[i].contentType, sortTypes[j].contentType) < 0
}
return a > b
})
for _, v := range sortTypes {
fmt.Printf(" %5d %s\n", v.count, v.contentType)
}
}