summaryrefslogtreecommitdiff
path: root/internal/clients/stats.go
blob: ec6adfe5e0e70d01eb41fdab8632164ac52f2ef6 (plain)
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
package clients

import (
	"context"
	"fmt"
	"runtime"
	"sync"
	"time"

	"github.com/mimecast/dtail/internal/io/logger"
)

// Used to collect and display various client stats.
type stats struct {
	// Total amount servers to connect to.
	connectionsTotal int
	// To keep track of what connected and disconnected
	connectionsEstCh chan struct{}
	// Amount of servers connections are established.
	connected int
	// To synchronize concurrent access.
	mutex sync.Mutex
}

func newTailStats(connectionsTotal int) *stats {
	return &stats{
		connectionsTotal: connectionsTotal,
		connectionsEstCh: make(chan struct{}, connectionsTotal),
		connected:        0,
	}
}

func (s *stats) periodicLogStats(ctx context.Context, throttleCh chan struct{}) {
	connectedLast := 0
	statsInterval := 5

	for {
		select {
		case <-time.After(time.Second * time.Duration(statsInterval)):
		case <-ctx.Done():
			return
		}

		connected := len(s.connectionsEstCh)
		throttle := len(throttleCh)

		newConnections := connected - connectedLast
		connectionsPerSecond := float64(newConnections) / float64(statsInterval)
		s.log(connected, newConnections, connectionsPerSecond, throttle)

		connectedLast = connected

		s.mutex.Lock()
		s.connected = connected
		s.mutex.Unlock()
	}
}

func (s *stats) numConnected() int {
	s.mutex.Lock()
	defer s.mutex.Unlock()

	return s.connected
}

func (s *stats) log(connected, newConnections int, connectionsPerSecond float64, throttle int) {
	percConnected := percentOf(float64(s.connectionsTotal), float64(connected))

	connectedStr := fmt.Sprintf("connected=%d/%d(%d%%)", connected, s.connectionsTotal, int(percConnected))
	newConnStr := fmt.Sprintf("new=%d", newConnections)
	rateStr := fmt.Sprintf("rate=%2.2f/s", connectionsPerSecond)
	throttleStr := fmt.Sprintf("throttle=%d", throttle)
	cpusGoroutinesStr := fmt.Sprintf("cpus/goroutines=%d/%d", runtime.NumCPU(), runtime.NumGoroutine())

	logger.Info("stats", connectedStr, newConnStr, rateStr, throttleStr, cpusGoroutinesStr)
}

func percentOf(total float64, value float64) float64 {
	if total == 0 || total == value {
		return 100
	}
	return value / (total / 100.0)
}