-
Notifications
You must be signed in to change notification settings - Fork 22
/
stats.go
103 lines (89 loc) · 2.08 KB
/
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
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"time"
"github.com/logrusorgru/aurora"
)
func round(val float64) int {
// Go seemed a sweet language in the beginning...
if val < 0 {
return int(val - 0.5)
}
return int(val + 0.5)
}
type statsMessage struct {
sent int
err int
flush bool
elapsed time.Duration
maxElapsed time.Duration
}
func displayStats(channel chan statsMessage) {
// Displays every N seconds the number of sent requests, and the rate
start := time.Now()
sent := 0
var elapsed time.Duration
var maxElapsed time.Duration
errors := 0
totalSent := 0
totalReceived := 0
for {
// Read the channel and add the number of sent messages
added := <-channel
sent += added.sent
errors += added.err
elapsed += added.elapsed
if added.maxElapsed > maxElapsed {
maxElapsed = added.maxElapsed
}
if added.flush == true {
// Something has asked for a display flush
elapsedSeconds := time.Since(start).Seconds()
if sent > 0 {
fmt.Printf(
"%s %6.dr/s",
aurora.Faint("Requests sent:"),
round(float64(sent)/elapsedSeconds),
)
// Successful requests? (replies received)
fmt.Printf(
"\t%s %6.dr/s",
aurora.Faint("Replies received:"),
round(float64(sent-errors)/elapsedSeconds),
)
fmt.Printf(
" (mean=%.0fms / max=%.0fms)",
1000.*elapsed.Seconds()/float64(sent),
1000.*maxElapsed.Seconds(),
)
if errors > 0 {
fmt.Printf(
"\t %s",
aurora.Red(fmt.Sprintf("Errors: %d (%d%%)",
errors,
100*errors/sent,
)),
)
}
} else {
fmt.Printf("No requests were sent %s", aurora.Sprintf(aurora.Faint("(total responses received: %d)"), totalReceived))
}
fmt.Print("\n")
start = time.Now()
totalSent += sent
totalReceived += sent - errors
sent = 0
errors = 0
elapsed = 0
maxElapsed = 0
}
}
}
func timerStats(channel chan<- statsMessage) {
// Periodically triggers a display update for the stats
for {
timer := time.NewTimer(time.Duration(displayInterval) * time.Millisecond)
<-timer.C
channel <- statsMessage{flush: true}
}
}