diff --git a/server.go b/server.go index f6fd1ef5b..8912d38bb 100644 --- a/server.go +++ b/server.go @@ -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 } } diff --git a/util.go b/util.go index 93f5e9878..d165f634e 100644 --- a/util.go +++ b/util.go @@ -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 } diff --git a/util_test.go b/util_test.go index 9aa336f31..ea27f1c5b 100644 --- a/util_test.go +++ b/util_test.go @@ -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) + } } }