From 0f108a612dc5ff23b11d6248b543795fa0e74239 Mon Sep 17 00:00:00 2001 From: reesporte Date: Fri, 22 Oct 2021 11:25:03 -0500 Subject: [PATCH] refactor and add unit tests --- server.go | 8 +++---- util.go | 9 ++++++++ util_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 util_test.go diff --git a/server.go b/server.go index 44e9da6f2..f6fd1ef5b 100644 --- a/server.go +++ b/server.go @@ -703,6 +703,7 @@ func (s *Server) Open() error { start := time.Now() prevMsg := start + numMsgs := uint(len(toSend)) s.logger.Printf("start initial cluster state sync") for i := range toSend { for { @@ -721,11 +722,8 @@ func (s *Server) Open() error { } if now := time.Now(); now.Sub(prevMsg) > time.Second { - progressRatio := float64(i+1) / float64(len(toSend)) - messagesLeft := len(toSend) - (i + 1) - avgTimePerMessage := float64(now.Sub(start)) / float64(i+1) - timeRemaining := time.Duration(avgTimePerMessage * float64(messagesLeft)) - s.logger.Printf("synced %d/%d messages (%.2f%% complete; %s remaining)", i+1, len(toSend), 100*progressRatio, timeRemaining) + 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)) prevMsg = now } } diff --git a/util.go b/util.go index 3bbeff8e3..93f5e9878 100644 --- a/util.go +++ b/util.go @@ -25,6 +25,7 @@ import ( "sort" "strings" "syscall" + "time" "unsafe" "github.com/molecula/featurebase/v2/roaring" @@ -343,3 +344,11 @@ 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 { + msgsLeft := total - (i + 1) + avgMsgTime := float64(now.Sub(start)) / float64(i+1) + return time.Duration(avgMsgTime * float64(msgsLeft)) +} diff --git a/util_test.go b/util_test.go new file mode 100644 index 000000000..824035744 --- /dev/null +++ b/util_test.go @@ -0,0 +1,59 @@ +package pilosa + +// util_test.go has unit tests for utility functions from util.go + +import ( + "testing" + "time" +) + +func TestEstTimeLeft(t *testing.T) { + cases := []struct { + start time.Time + now time.Time + i uint + total uint + }{ + { + time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), + time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC), + 10, + 20, + }, + { + time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), + time.Date(1969, time.June, 9, 4, 20, 4, 0, time.UTC), + 10, + 20, + }, + { + time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), + time.Date(1969, time.June, 9, 4, 21, 1, 5, time.UTC), + 10, + 20, + }, + { + time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), + time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC), + 1, + 20, + }, + { + time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), + time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC), + 10, + 50, + }, + } + + for _, c := range cases { + // 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))) + + timeLeft := EstTimeLeft(c.start, c.now, c.i, c.total) + if timeLeft != expected { + t.Errorf("Time left was incorrect, expected: %d, but got: %d", expected, timeLeft) + } + } +}