rename function, return pctDone

This commit is contained in:
reesporte 2021-10-22 12:54:46 -05:00
parent 681ed9923d
commit bd0d68b2fd
3 changed files with 14 additions and 8 deletions

View file

@ -722,8 +722,8 @@ func (s *Server) Open() error {
}
if now := time.Now(); now.Sub(prevMsg) > time.Second {
pctDone := (float64(i+1) / float64(numMsgs)) * 100
s.logger.Printf("synced %d/%d messages (%.2f%% complete; %s remaining)", i+1, numMsgs, pctDone, EstTimeLeft(start, now, uint(i), numMsgs))
estimate, pctDone := GetLoopProgress(start, now, uint(i), numMsgs)
s.logger.Printf("synced %d/%d messages (%.2f%% complete; %s remaining)", i+1, numMsgs, pctDone, estimate)
prevMsg = now
}
}

10
util.go
View file

@ -345,10 +345,12 @@ func roaringFragmentHasData(path string, index, field, view string, shard uint64
return
}
// EstTimeLeft returns the estimated remaining time to iterate through some items
// given a start time, the current time, the iteration, and the number of items
func EstTimeLeft(start time.Time, now time.Time, i uint, total uint) time.Duration {
// GetLoopProgress returns the estimated remaining time to iterate through some items
// as well as the loop completion percentage with the following parameters:
// the start time, the current time, the iteration, and the number of items
func GetLoopProgress(start time.Time, now time.Time, i uint, total uint) (time.Duration, float64) {
msgsLeft := total - (i + 1)
avgMsgTime := float64(now.Sub(start)) / float64(i+1)
return time.Duration(avgMsgTime * float64(msgsLeft))
pctDone := (float64(i+1) / float64(total)) * 100
return time.Duration(avgMsgTime * float64(msgsLeft)), pctDone
}

View file

@ -21,7 +21,7 @@ import (
"time"
)
func TestEstTimeLeft(t *testing.T) {
func TestGetLoopProgress(t *testing.T) {
cases := []struct {
start time.Time
now time.Time
@ -64,10 +64,14 @@ func TestEstTimeLeft(t *testing.T) {
// we expect that it will be the avg time per message times
// the number of remaining messages
expected := time.Duration((float64(c.now.Sub(c.start)) / float64(c.i+1)) * float64(c.total-(c.i+1)))
expectedPct := 100 * (float64(c.i+1) / float64(c.total))
timeLeft := EstTimeLeft(c.start, c.now, c.i, c.total)
timeLeft, pctDone := GetLoopProgress(c.start, c.now, c.i, c.total)
if timeLeft != expected {
t.Errorf("Time left was incorrect, expected: %d, but got: %d", expected, timeLeft)
}
if pctDone != expectedPct {
t.Errorf("Percentage done was incorrect, expected: %f, but got: %f", expectedPct, pctDone)
}
}
}