Skip to content

Commit

Permalink
Improve stats dump
Browse files Browse the repository at this point in the history
  • Loading branch information
fkollmann committed Dec 21, 2019
1 parent 163c910 commit a24a2c1
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package xdelta

import (
"fmt"
"github.com/dustin/go-humanize"
lib "github.com/konsorten/go-xdelta/xdelta-lib"
"sync"
"time"
)

type Stats struct {
states map[lib.XdeltaState]time.Duration
dataBytes map[lib.XdeltaState]int64
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]int64),
dataBytes: make(map[lib.XdeltaState]uint64),
}
}

Expand All @@ -28,7 +29,7 @@ func (s *Stats) DumpToStdout() {
for k, v := range s.states {
dataBytes, _ := s.dataBytes[k]

fmt.Printf(" State %10v lastet %v and processed %v bytes\n", k, v, dataBytes)
fmt.Printf(" State %10v lastet %v and processed %v bytes (%v)\n", k, v, dataBytes, humanize.Bytes(dataBytes))
}

s.lock.Unlock()
Expand All @@ -45,11 +46,15 @@ func (s *Stats) addStateTime(state lib.XdeltaState, duration time.Duration) {
}

func (s *Stats) addDataBytes(state lib.XdeltaState, amount int) {
if amount <= 0 {
return
}

s.lock.Lock()

prev, _ := s.dataBytes[state]

s.dataBytes[state] = prev + int64(amount)
s.dataBytes[state] = prev + uint64(amount)

s.lock.Unlock()
}

0 comments on commit a24a2c1

Please sign in to comment.