-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
stats.go
60 lines (43 loc) · 1.05 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
package xdelta
import (
"fmt"
"github.com/dustin/go-humanize"
lib "github.com/nine-lives-later/go-xdelta/xdelta-lib"
"sync"
"time"
)
type Stats struct {
states map[lib.XdeltaState]time.Duration
dataBytes map[lib.XdeltaState]uint64
lock sync.Mutex
}
func newStats() *Stats {
return &Stats{
states: make(map[lib.XdeltaState]time.Duration),
dataBytes: make(map[lib.XdeltaState]uint64),
}
}
func (s *Stats) DumpToStdout() {
fmt.Println("STATS:")
s.lock.Lock()
for k, v := range s.states {
dataBytes, _ := s.dataBytes[k]
fmt.Printf(" State %10v lastet %v and processed %v bytes (%v)\n", k, v, dataBytes, humanize.Bytes(dataBytes))
}
s.lock.Unlock()
}
func (s *Stats) addStateTime(state lib.XdeltaState, duration time.Duration) {
s.lock.Lock()
prev, _ := s.states[state]
s.states[state] = prev + duration
s.lock.Unlock()
}
func (s *Stats) addDataBytes(state lib.XdeltaState, amount int) {
if amount <= 0 {
return
}
s.lock.Lock()
prev, _ := s.dataBytes[state]
s.dataBytes[state] = prev + uint64(amount)
s.lock.Unlock()
}