Add support for calculating the estimate time remaining (#71)

This commit is contained in:
Arturo Filastò 2019-11-14 12:01:40 +01:00 committed by GitHub
parent 0c6862bb87
commit bc1314ca61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 12 deletions

View File

@ -109,9 +109,11 @@ func (h *Handler) TypedLog(t string, e *log.Entry) error {
return nil
case "progress":
perc := e.Fields.Get("percentage").(float64) * 100
s := fmt.Sprintf(" %s %-25s",
eta := e.Fields.Get("eta").(float64)
s := fmt.Sprintf(" %s %-25s (%ss left)",
bold.Sprintf("%.2f%%", perc),
e.Message)
e.Message,
bold.Sprintf("%.2f", eta))
fmt.Fprint(h.Writer, s)
fmt.Fprintln(h.Writer)
return nil

View File

@ -14,17 +14,18 @@ import (
// MeasurementJSON prints the JSON of a measurement
func MeasurementJSON(j map[string]interface{}) {
log.WithFields(log.Fields{
"type": "measurement_json",
"type": "measurement_json",
"measurement_json": j,
}).Info("Measurement JSON")
}
// Progress logs a progress type event
func Progress(key string, perc float64, msg string) {
func Progress(key string, perc float64, eta float64, msg string) {
log.WithFields(log.Fields{
"type": "progress",
"key": key,
"percentage": perc,
"eta": eta,
}).Info(msg)
}
@ -42,13 +43,13 @@ type MeasurementSummaryData struct {
func MeasurementSummary(msmt MeasurementSummaryData) {
log.WithFields(log.Fields{
"type": "measurement_summary",
"total_runtime": msmt.TotalRuntime,
"total_count": msmt.TotalCount,
"anomaly_count": msmt.AnomalyCount,
"data_usage_down": msmt.DataUsageDown,
"data_usage_up": msmt.DataUsageUp,
"asn": msmt.ASN,
"type": "measurement_summary",
"total_runtime": msmt.TotalRuntime,
"total_count": msmt.TotalCount,
"anomaly_count": msmt.AnomalyCount,
"data_usage_down": msmt.DataUsageDown,
"data_usage_up": msmt.DataUsageUp,
"asn": msmt.ASN,
"network_country_code": msmt.NetworkCountryCode,
"network_name": msmt.NetworkName,
"start_time": msmt.StartTime,

View File

@ -44,6 +44,7 @@ type Controller struct {
nt Nettest
ntCount int
ntIndex int
ntStartTime time.Time // used to calculate the eta
msmts map[int64]*database.Measurement
msmtPath string // XXX maybe we can drop this and just use a temporary file
inputIdxMap map[int64]int64 // Used to map mk idx to database id
@ -105,6 +106,7 @@ func (c *Controller) Run(builder *engine.ExperimentBuilder, inputs []string) err
}
}
c.ntStartTime = time.Now()
for idx, input := range inputs {
c.curInputIdx = idx // allow for precise progress
idx64 := int64(idx)
@ -185,18 +187,21 @@ func (c *Controller) Run(builder *engine.ExperimentBuilder, inputs []string) err
// OnProgress should be called when a new progress event is available.
func (c *Controller) OnProgress(perc float64, msg string) {
log.Debugf("OnProgress: %f - %s", perc, msg)
var eta float64
eta = -1.0
if c.numInputs >= 1 {
// make the percentage relative to the current input over all inputs
floor := (float64(c.curInputIdx) / float64(c.numInputs))
step := 1.0 / float64(c.numInputs)
perc = floor + perc*step
eta = (float64(time.Now().Sub(c.ntStartTime).Seconds()) / float64(c.curInputIdx)) * float64(c.numInputs-c.curInputIdx)
}
if c.ntCount > 0 {
// make the percentage relative to the current nettest over all nettests
perc = float64(c.ntIndex)/float64(c.ntCount) + perc/float64(c.ntCount)
}
key := fmt.Sprintf("%T", c.nt)
output.Progress(key, perc, msg)
output.Progress(key, perc, eta, msg)
}
// OnDataUsage should be called when we have a data usage update.