refactor and add unit tests

This commit is contained in:
reesporte 2021-10-22 11:25:03 -05:00
parent 45e36600f3
commit 0f108a612d
3 changed files with 71 additions and 5 deletions

View file

@ -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
}
}

View file

@ -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))
}

59
util_test.go Normal file
View file

@ -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)
}
}
}