diff --git a/decor/speed.go b/decor/speed.go index dd0ad70..448aac7 100644 --- a/decor/speed.go +++ b/decor/speed.go @@ -4,6 +4,7 @@ "fmt" "io" "math" + "strings" "time" "github.com/VividCortex/ewma" @@ -39,7 +40,7 @@ // EwmaSpeed exponential-weighted-moving-average based speed decorator. // For this decorator to work correctly you have to measure each iteration's // duration and pass it to one of the (*Bar).EwmaIncr... family methods. -func EwmaSpeed(unit int, format string, age float64, wcc ...WC) Decorator { +func EwmaSpeed(unit interface{}, format string, age float64, wcc ...WC) Decorator { var average ewma.MovingAverage if age == 0 { average = ewma.NewMovingAverage() @@ -52,7 +53,7 @@ // MovingAverageSpeed decorator relies on MovingAverage implementation // to calculate its average. // -// `unit` one of [0|UnitKiB|UnitKB] zero for no unit +// `unit` one of [0|SizeB1024(0)|SizeB1000(0)] // // `format` printf compatible verb for value, like "%f" or "%d" // @@ -62,13 +63,15 @@ // // format examples: // -// unit=UnitKiB, format="%.1f" output: "1.0MiB/s" -// unit=UnitKiB, format="% .1f" output: "1.0 MiB/s" -// unit=UnitKB, format="%.1f" output: "1.0MB/s" -// unit=UnitKB, format="% .1f" output: "1.0 MB/s" -func MovingAverageSpeed(unit int, format string, average ewma.MovingAverage, wcc ...WC) Decorator { +// unit=SizeB1024(0), format="%.1f" output: "1.0MiB/s" +// unit=SizeB1024(0), format="% .1f" output: "1.0 MiB/s" +// unit=SizeB1000(0), format="%.1f" output: "1.0MB/s" +// unit=SizeB1000(0), format="% .1f" output: "1.0 MB/s" +func MovingAverageSpeed(unit interface{}, format string, average ewma.MovingAverage, wcc ...WC) Decorator { if format == "" { format = "%.0f" + } else if strings.Count(format, "%") != 1 { + panic("expected format with exactly 1 verb") } d := &movingAverageSpeed{ WC: initWC(wcc...), @@ -113,7 +116,7 @@ // NewAverageSpeed decorator with dynamic unit measure adjustment and // user provided start time. // -// `unit` one of [0|UnitKiB|UnitKB] zero for no unit +// `unit` one of [0|SizeB1024(0)|SizeB1000(0)] // // `format` printf compatible verb for value, like "%f" or "%d" // @@ -123,13 +126,15 @@ // // format examples: // -// unit=UnitKiB, format="%.1f" output: "1.0MiB/s" -// unit=UnitKiB, format="% .1f" output: "1.0 MiB/s" -// unit=UnitKB, format="%.1f" output: "1.0MB/s" -// unit=UnitKB, format="% .1f" output: "1.0 MB/s" -func NewAverageSpeed(unit int, format string, startTime time.Time, wcc ...WC) Decorator { +// unit=SizeB1024(0), format="%.1f" output: "1.0MiB/s" +// unit=SizeB1024(0), format="% .1f" output: "1.0 MiB/s" +// unit=SizeB1000(0), format="%.1f" output: "1.0MB/s" +// unit=SizeB1000(0), format="% .1f" output: "1.0 MB/s" +func NewAverageSpeed(unit interface{}, format string, startTime time.Time, wcc ...WC) Decorator { if format == "" { format = "%.0f" + } else if strings.Count(format, "%") != 1 { + panic("expected format with exactly 1 verb") } d := &averageSpeed{ WC: initWC(wcc...), @@ -159,13 +164,13 @@ d.startTime = startTime } -func chooseSpeedProducer(unit int, format string) func(float64) string { - switch unit { - case UnitKiB: +func chooseSpeedProducer(unit interface{}, format string) func(float64) string { + switch unit.(type) { + case SizeB1024: return func(speed float64) string { return fmt.Sprintf(format, FmtAsSpeed(SizeB1024(math.Round(speed)))) } - case UnitKB: + case SizeB1000: return func(speed float64) string { return fmt.Sprintf(format, FmtAsSpeed(SizeB1000(math.Round(speed)))) } diff --git a/decor/speed_test.go b/decor/speed_test.go index 7f7d09d..a004da3 100644 --- a/decor/speed_test.go +++ b/decor/speed_test.go @@ -9,110 +9,110 @@ cases := []struct { name string fmt string - unit int + unit interface{} current int64 elapsed time.Duration expected string }{ { name: "empty fmt", - unit: UnitKiB, + unit: SizeB1024(0), fmt: "", current: 0, elapsed: time.Second, expected: "0b/s", }, { - name: "UnitKiB:%d:0b", - unit: UnitKiB, - fmt: "%d", - current: 0, - elapsed: time.Second, - expected: "0b/s", - }, - { - name: "UnitKiB:% .2f:0b", - unit: UnitKiB, + name: "SizeB1024(0):%d:0b", + unit: SizeB1024(0), + fmt: "%d", + current: 0, + elapsed: time.Second, + expected: "0b/s", + }, + { + name: "SizeB1024(0):% .2f:0b", + unit: SizeB1024(0), fmt: "% .2f", current: 0, elapsed: time.Second, expected: "0.00 b/s", }, { - name: "UnitKiB:%d:1b", - unit: UnitKiB, + name: "SizeB1024(0):%d:1b", + unit: SizeB1024(0), fmt: "%d", current: 1, elapsed: time.Second, expected: "1b/s", }, { - name: "UnitKiB:% .2f:1b", - unit: UnitKiB, + name: "SizeB1024(0):% .2f:1b", + unit: SizeB1024(0), fmt: "% .2f", current: 1, elapsed: time.Second, expected: "1.00 b/s", }, { - name: "UnitKiB:%d:KiB", - unit: UnitKiB, + name: "SizeB1024(0):%d:KiB", + unit: SizeB1024(0), fmt: "%d", current: 2 * int64(_iKiB), elapsed: 1 * time.Second, expected: "2KiB/s", }, { - name: "UnitKiB:% .f:KiB", - unit: UnitKiB, + name: "SizeB1024(0):% .f:KiB", + unit: SizeB1024(0), fmt: "% .2f", current: 2 * int64(_iKiB), elapsed: 1 * time.Second, expected: "2.00 KiB/s", }, { - name: "UnitKiB:%d:MiB", - unit: UnitKiB, + name: "SizeB1024(0):%d:MiB", + unit: SizeB1024(0), fmt: "%d", current: 2 * int64(_iMiB), elapsed: 1 * time.Second, expected: "2MiB/s", }, { - name: "UnitKiB:% .2f:MiB", - unit: UnitKiB, + name: "SizeB1024(0):% .2f:MiB", + unit: SizeB1024(0), fmt: "% .2f", current: 2 * int64(_iMiB), elapsed: 1 * time.Second, expected: "2.00 MiB/s", }, { - name: "UnitKiB:%d:GiB", - unit: UnitKiB, + name: "SizeB1024(0):%d:GiB", + unit: SizeB1024(0), fmt: "%d", current: 2 * int64(_iGiB), elapsed: 1 * time.Second, expected: "2GiB/s", }, { - name: "UnitKiB:% .2f:GiB", - unit: UnitKiB, + name: "SizeB1024(0):% .2f:GiB", + unit: SizeB1024(0), fmt: "% .2f", current: 2 * int64(_iGiB), elapsed: 1 * time.Second, expected: "2.00 GiB/s", }, { - name: "UnitKiB:%d:TiB", - unit: UnitKiB, + name: "SizeB1024(0):%d:TiB", + unit: SizeB1024(0), fmt: "%d", current: 2 * int64(_iTiB), elapsed: 1 * time.Second, expected: "2TiB/s", }, { - name: "UnitKiB:% .2f:TiB", - unit: UnitKiB, + name: "SizeB1024(0):% .2f:TiB", + unit: SizeB1024(0), fmt: "% .2f", current: 2 * int64(_iTiB), elapsed: 1 * time.Second, @@ -137,110 +137,110 @@ cases := []struct { name string fmt string - unit int + unit interface{} current int64 elapsed time.Duration expected string }{ { name: "empty fmt", - unit: UnitKB, + unit: SizeB1000(0), fmt: "", current: 0, elapsed: time.Second, expected: "0b/s", }, { - name: "UnitKB:%d:0b", - unit: UnitKB, - fmt: "%d", - current: 0, - elapsed: time.Second, - expected: "0b/s", - }, - { - name: "UnitKB:% .2f:0b", - unit: UnitKB, + name: "SizeB1000(0):%d:0b", + unit: SizeB1000(0), + fmt: "%d", + current: 0, + elapsed: time.Second, + expected: "0b/s", + }, + { + name: "SizeB1000(0):% .2f:0b", + unit: SizeB1000(0), fmt: "% .2f", current: 0, elapsed: time.Second, expected: "0.00 b/s", }, { - name: "UnitKB:%d:1b", - unit: UnitKB, + name: "SizeB1000(0):%d:1b", + unit: SizeB1000(0), fmt: "%d", current: 1, elapsed: time.Second, expected: "1b/s", }, { - name: "UnitKB:% .2f:1b", - unit: UnitKB, + name: "SizeB1000(0):% .2f:1b", + unit: SizeB1000(0), fmt: "% .2f", current: 1, elapsed: time.Second, expected: "1.00 b/s", }, { - name: "UnitKB:%d:KB", - unit: UnitKB, + name: "SizeB1000(0):%d:KB", + unit: SizeB1000(0), fmt: "%d", current: 2 * int64(_KB), elapsed: 1 * time.Second, expected: "2KB/s", }, { - name: "UnitKB:% .f:KB", - unit: UnitKB, + name: "SizeB1000(0):% .f:KB", + unit: SizeB1000(0), fmt: "% .2f", current: 2 * int64(_KB), elapsed: 1 * time.Second, expected: "2.00 KB/s", }, { - name: "UnitKB:%d:MB", - unit: UnitKB, + name: "SizeB1000(0):%d:MB", + unit: SizeB1000(0), fmt: "%d", current: 2 * int64(_MB), elapsed: 1 * time.Second, expected: "2MB/s", }, { - name: "UnitKB:% .2f:MB", - unit: UnitKB, + name: "SizeB1000(0):% .2f:MB", + unit: SizeB1000(0), fmt: "% .2f", current: 2 * int64(_MB), elapsed: 1 * time.Second, expected: "2.00 MB/s", }, { - name: "UnitKB:%d:GB", - unit: UnitKB, + name: "SizeB1000(0):%d:GB", + unit: SizeB1000(0), fmt: "%d", current: 2 * int64(_GB), elapsed: 1 * time.Second, expected: "2GB/s", }, { - name: "UnitKB:% .2f:GB", - unit: UnitKB, + name: "SizeB1000(0):% .2f:GB", + unit: SizeB1000(0), fmt: "% .2f", current: 2 * int64(_GB), elapsed: 1 * time.Second, expected: "2.00 GB/s", }, { - name: "UnitKB:%d:TB", - unit: UnitKB, + name: "SizeB1000(0):%d:TB", + unit: SizeB1000(0), fmt: "%d", current: 2 * int64(_TB), elapsed: 1 * time.Second, expected: "2TB/s", }, { - name: "UnitKB:% .2f:TB", - unit: UnitKB, + name: "SizeB1000(0):% .2f:TB", + unit: SizeB1000(0), fmt: "% .2f", current: 2 * int64(_TB), elapsed: 1 * time.Second,