-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdns_stats.go
90 lines (78 loc) · 1.92 KB
/
dns_stats.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
package main
import (
"fmt"
"os"
"strconv"
"sync"
"time"
"github.com/olekukonko/tablewriter"
)
// DNSStats represents the stats of the requests
type DNSStats struct {
DurationStats
sync.Mutex
nbOfRequests int
statusStats map[string]int
}
// newDNSStats will return an empty Stats object
func newDNSStats() Stats {
return &DNSStats{
DurationStats: DurationStats{},
statusStats: map[string]int{},
}
}
// AddRequest will add a request to the stats
func (s *DNSStats) AddRequest(req Request) {
s.Lock()
defer s.Unlock()
s.nbOfRequests++
s.addDuration(req)
if req.IsError() {
s.statusStats[req.Error()]++
return
}
s.statusStats[req.Status()]++
}
// addDuration will add the duration of a requests to the stats
func (s *DNSStats) addDuration(req Request) {
s.totalDuration += req.Duration()
if s.maxDuration < req.Duration() {
s.maxDuration = req.Duration()
}
if s.minDuration == 0 || s.minDuration > req.Duration() {
s.minDuration = req.Duration()
}
}
// Render renders the results
func (s *DNSStats) Render() {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.SetHeader([]string{
"Number of requests ",
"Min duration",
"Max duration",
"Average duration",
"Exec duration",
})
table.Append([]string{
strconv.Itoa(s.nbOfRequests),
s.minDuration.String(),
s.maxDuration.String(),
(s.totalDuration / time.Duration(s.nbOfRequests)).String(),
s.execDuration.String(),
})
fmt.Printf("\nStats :\n")
table.Render()
statusTable := tablewriter.NewWriter(os.Stdout)
statusTable.SetAlignment(tablewriter.ALIGN_CENTER)
statusTable.SetHeader([]string{"Result", "Count"})
for key, value := range s.statusStats {
statusTable.Append([]string{key, strconv.Itoa(value)})
}
fmt.Printf("\nStatuses :\n")
statusTable.Render()
}
// SetDuration will set the total duration of the simulation
func (s *DNSStats) SetDuration(t time.Duration) {
s.execDuration = t
}