Refactor statistics printing

This commit is contained in:
Jakob Borg
2014-01-05 16:16:37 +01:00
parent 7b6f43cbb5
commit 6679c84cfb
4 changed files with 115 additions and 71 deletions

34
main.go
View File

@@ -174,9 +174,43 @@ func main() {
}
}()
// Periodically print statistics
go printStatsLoop(m)
select {}
}
func printStatsLoop(m *Model) {
var lastUpdated int64
var lastStats = make(map[string]protocol.Statistics)
for {
time.Sleep(60 * time.Second)
for node, stats := range m.ConnectionStats() {
secs := time.Since(lastStats[node].At).Seconds()
inbps := 8 * int(float64(stats.InBytesTotal-lastStats[node].InBytesTotal)/secs)
outbps := 8 * int(float64(stats.OutBytesTotal-lastStats[node].OutBytesTotal)/secs)
if inbps+outbps > 0 {
infof("%s: %sb/s in, %sb/s out", node, MetricPrefix(inbps), MetricPrefix(outbps))
}
lastStats[node] = stats
}
if lu := m.Generation(); lu > lastUpdated {
lastUpdated = lu
files, bytes := m.GlobalSize()
infof("%6d files, %9sB in cluster", files, BinaryPrefix(bytes))
files, bytes = m.LocalSize()
infof("%6d files, %9sB in local repo", files, BinaryPrefix(bytes))
files, bytes = m.NeedSize()
infof("%6d files, %9sB in to synchronize", files, BinaryPrefix(bytes))
}
}
}
func listen(myID string, addr string, m *Model, cfg *tls.Config) {
l, err := tls.Listen("tcp", addr, cfg)
fatalErr(err)