forked from moby/swarmkit
/
collector.go
76 lines (67 loc) · 2.17 KB
/
collector.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
package main
import (
"fmt"
"io"
"net"
"strconv"
"time"
"github.com/docker/swarmkit/log"
"github.com/rcrowley/go-metrics"
"golang.org/x/net/context"
)
// Collector waits for tasks to phone home while collecting statistics.
type Collector struct {
t metrics.Timer
ln net.Listener
}
// Listen starts listening on a TCP port. Tasks have to connect to this address
// once they come online.
func (c *Collector) Listen(port int) error {
var err error
c.ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
return err
}
return nil
}
// Collect blocks until `count` tasks phoned home.
func (c *Collector) Collect(count uint64) {
start := time.Now()
for i := uint64(0); i < count; i++ {
conn, err := c.ln.Accept()
if err != nil {
log.G(context.Background()).WithError(err).Error("failure accepting connection")
continue
}
c.t.UpdateSince(start)
conn.Close()
}
}
// Stats prints various statistics related to the collection.
func (c *Collector) Stats(w io.Writer, unit time.Duration) {
du := float64(unit)
duSuffix := unit.String()[1:]
t := c.t.Snapshot()
ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
fmt.Fprintf(w, "stats:\n")
fmt.Fprintf(w, " count: %9d\n", t.Count())
fmt.Fprintf(w, " min: %12.2f%s\n", float64(t.Min())/du, duSuffix)
fmt.Fprintf(w, " max: %12.2f%s\n", float64(t.Max())/du, duSuffix)
fmt.Fprintf(w, " mean: %12.2f%s\n", t.Mean()/du, duSuffix)
fmt.Fprintf(w, " stddev: %12.2f%s\n", t.StdDev()/du, duSuffix)
fmt.Fprintf(w, " median: %12.2f%s\n", ps[0]/du, duSuffix)
fmt.Fprintf(w, " 75%%: %12.2f%s\n", ps[1]/du, duSuffix)
fmt.Fprintf(w, " 95%%: %12.2f%s\n", ps[2]/du, duSuffix)
fmt.Fprintf(w, " 99%%: %12.2f%s\n", ps[3]/du, duSuffix)
fmt.Fprintf(w, " 99.9%%: %12.2f%s\n", ps[4]/du, duSuffix)
fmt.Fprintf(w, " 1-min rate: %12.2f\n", t.Rate1())
fmt.Fprintf(w, " 5-min rate: %12.2f\n", t.Rate5())
fmt.Fprintf(w, " 15-min rate: %12.2f\n", t.Rate15())
fmt.Fprintf(w, " mean rate: %12.2f\n", t.RateMean())
}
// NewCollector creates and returns a collector.
func NewCollector() *Collector {
return &Collector{
t: metrics.NewTimer(),
}
}