forked from omniscale/imposm3
/
stats.go
166 lines (146 loc) · 3.91 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package stats
import (
"fmt"
"imposm3/logging"
"time"
)
type Counter struct {
start time.Time
Coords *RpsCounter
Nodes *RpsCounter
Ways *RpsCounter
Relations *RpsCounter
}
func (c *Counter) Tick() {
c.Coords.Tick()
c.Nodes.Tick()
c.Ways.Tick()
c.Relations.Tick()
}
func NewCounter() *Counter {
return &Counter{
start: time.Now(),
Coords: NewRpsCounter(),
Nodes: NewRpsCounter(),
Ways: NewRpsCounter(),
Relations: NewRpsCounter(),
}
}
func NewCounterWithEstimate(counts ElementCounts) *Counter {
counter := NewCounter()
counter.Coords.total = counts.Coords.Current
counter.Nodes.total = counts.Nodes.Current
counter.Ways.total = counts.Ways.Current
counter.Relations.total = counts.Relations.Current
return counter
}
type ElementCounts struct {
Coords, Nodes, Ways, Relations ElementCount
}
// Duration returns the duration since start with seconds precission.
func (c *Counter) CurrentCount() *ElementCounts {
return &ElementCounts{
Coords: c.Coords.Count(),
Nodes: c.Nodes.Count(),
Ways: c.Ways.Count(),
Relations: c.Relations.Count(),
}
}
// Duration returns the duration since start with seconds precission.
func (c *Counter) Duration() time.Duration {
return time.Duration(int64(time.Since(c.start).Seconds()) * 1000 * 1000 * 1000)
}
type Statistics struct {
counter *Counter
done chan bool
}
const (
RESET = iota
START
STOP
QUIT
)
func (s *Statistics) AddCoords(n int) { s.counter.Coords.Add(n) }
func (s *Statistics) AddNodes(n int) { s.counter.Nodes.Add(n) }
func (s *Statistics) AddWays(n int) { s.counter.Ways.Add(n) }
func (s *Statistics) AddRelations(n int) { s.counter.Relations.Add(n) }
func (s *Statistics) Stop() *ElementCounts {
s.done <- true
return s.counter.CurrentCount()
}
func NewStatsReporter() *Statistics {
s := Statistics{}
s.counter = NewCounter()
s.done = make(chan bool)
go s.loop()
return &s
}
func NewStatsReporterWithEstimate(counts *ElementCounts) *Statistics {
s := Statistics{}
if counts != nil {
s.counter = NewCounterWithEstimate(*counts)
} else {
s.counter = NewCounter()
}
s.done = make(chan bool)
go s.loop()
return &s
}
func (s *Statistics) loop() {
tick := time.NewTicker(500 * time.Millisecond)
tock := time.NewTicker(time.Minute)
for {
select {
case <-s.done:
tick.Stop()
tock.Stop()
s.counter.PrintStats()
return
case <-tock.C:
s.counter.PrintStats()
case <-tick.C:
s.counter.PrintTick()
s.counter.Tick()
}
}
}
func fmtPercentOrVal(progress float64, value int64) string {
if progress == -1.0 {
return fmt.Sprintf("%d", value)
}
return fmt.Sprintf("%4.1f%%", progress*100)
}
func roundInt(val float64, round int) int64 {
return int64(val/float64(round)) * int64(round)
}
func (c *Counter) PrintTick() {
logging.Progress(
fmt.Sprintf("[%6s] C: %7d/s %7d/s (%s) N: %7d/s %7d/s (%s) W: %7d/s %7d/s (%s) R: %6d/s %6d/s (%s)",
c.Duration(),
roundInt(c.Coords.Rps(), 1000),
roundInt(c.Coords.LastRps(), 1000),
fmtPercentOrVal(c.Coords.Progress(), c.Coords.Value()),
roundInt(c.Nodes.Rps(), 100),
roundInt(c.Nodes.LastRps(), 100),
fmtPercentOrVal(c.Nodes.Progress(), c.Nodes.Value()),
roundInt(c.Ways.Rps(), 100),
roundInt(c.Ways.LastRps(), 100),
fmtPercentOrVal(c.Ways.Progress(), c.Ways.Value()),
roundInt(c.Relations.Rps(), 10),
roundInt(c.Relations.LastRps(), 10),
fmtPercentOrVal(c.Relations.Progress(), c.Relations.Value()),
))
}
func (c *Counter) PrintStats() {
logging.Infof("[%6s] C: %7d/s (%s) N: %7d/s (%s) W: %7d/s (%s) R: %6d/s (%s)",
c.Duration(),
roundInt(c.Coords.Rps(), 1000),
fmtPercentOrVal(c.Coords.Progress(), c.Coords.Value()),
roundInt(c.Nodes.Rps(), 100),
fmtPercentOrVal(c.Nodes.Progress(), c.Nodes.Value()),
roundInt(c.Ways.Rps(), 100),
fmtPercentOrVal(c.Ways.Progress(), c.Ways.Value()),
roundInt(c.Relations.Rps(), 10),
fmtPercentOrVal(c.Relations.Progress(), c.Relations.Value()),
)
}