Skip to content

Commit

Permalink
[feat] Better number formatting all around
Browse files Browse the repository at this point in the history
uppfinnarn committed Dec 2, 2016

Unverified

This user has not yet uploaded their public signing key.
1 parent 9a585a7 commit 02044a2
Showing 4 changed files with 126 additions and 5 deletions.
8 changes: 4 additions & 4 deletions run.go
Original file line number Diff line number Diff line change
@@ -338,8 +338,8 @@ loop:
fmt.Printf("%10s %s %10s / %s\r",
statusString,
progressBar.String(),
atTime-(atTime%(100*time.Millisecond)),
totalTime-(totalTime%(100*time.Millisecond)),
roundDuration(atTime, 100*time.Millisecond),
roundDuration(totalTime, 100*time.Millisecond),
)
case <-srvC.Done():
log.Debug("API server terminated; shutting down...")
@@ -363,8 +363,8 @@ loop:
progressBar.Progress = 1.0
fmt.Printf(" done %s %10s / %s\n",
progressBar.String(),
atTime-(atTime%(100*time.Millisecond)),
atTime-(atTime%(100*time.Millisecond)),
roundDuration(atTime, 100*time.Millisecond),
roundDuration(atTime, 100*time.Millisecond),
)
fmt.Printf("\n")

13 changes: 12 additions & 1 deletion stats/stats.go
Original file line number Diff line number Diff line change
@@ -186,7 +186,18 @@ func (m Metric) HumanizeValue(v float64) string {
default:
switch m.Contains {
case Time:
return time.Duration(v).String()
d := time.Duration(v)
switch {
case d > time.Minute:
d -= d % (1 * time.Second)
case d > time.Second:
d -= d % (10 * time.Millisecond)
case d > time.Millisecond:
d -= d % (10 * time.Microsecond)
case d > time.Microsecond:
d -= d % (10 * time.Nanosecond)
}
return d.String()
default:
return strconv.FormatFloat(v, 'f', -1, 64)
}
105 changes: 105 additions & 0 deletions stats/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package stats

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestMetricHumanizeValue(t *testing.T) {
data := map[*Metric]map[float64]string{
&Metric{Type: Counter, Contains: Default}: map[float64]string{
1.0: "1",
1.5: "1.5",
1.54321: "1.54321",
},
&Metric{Type: Gauge, Contains: Default}: map[float64]string{
1.0: "1",
1.5: "1.5",
1.54321: "1.54321",
},
&Metric{Type: Trend, Contains: Default}: map[float64]string{
1.0: "1",
1.5: "1.5",
1.54321: "1.54321",
},
&Metric{Type: Counter, Contains: Time}: map[float64]string{
float64(1): "1ns",
float64(12): "12ns",
float64(123): "123ns",
float64(1234): "1.23µs",
float64(12345): "12.34µs",
float64(123456): "123.45µs",
float64(1234567): "1.23ms",
float64(12345678): "12.34ms",
float64(123456789): "123.45ms",
float64(1234567890): "1.23s",
float64(12345678901): "12.34s",
float64(123456789012): "2m3s",
float64(1234567890123): "20m34s",
float64(12345678901234): "3h25m45s",
float64(123456789012345): "34h17m36s",
},
&Metric{Type: Gauge, Contains: Time}: map[float64]string{
float64(1): "1ns",
float64(12): "12ns",
float64(123): "123ns",
float64(1234): "1.23µs",
float64(12345): "12.34µs",
float64(123456): "123.45µs",
float64(1234567): "1.23ms",
float64(12345678): "12.34ms",
float64(123456789): "123.45ms",
float64(1234567890): "1.23s",
float64(12345678901): "12.34s",
float64(123456789012): "2m3s",
float64(1234567890123): "20m34s",
float64(12345678901234): "3h25m45s",
float64(123456789012345): "34h17m36s",
},
&Metric{Type: Trend, Contains: Time}: map[float64]string{
float64(1): "1ns",
float64(12): "12ns",
float64(123): "123ns",
float64(1234): "1.23µs",
float64(12345): "12.34µs",
float64(123456): "123.45µs",
float64(1234567): "1.23ms",
float64(12345678): "12.34ms",
float64(123456789): "123.45ms",
float64(1234567890): "1.23s",
float64(12345678901): "12.34s",
float64(123456789012): "2m3s",
float64(1234567890123): "20m34s",
float64(12345678901234): "3h25m45s",
float64(123456789012345): "34h17m36s",
},
&Metric{Type: Rate, Contains: Default}: map[float64]string{
0.0: "0.00%",
0.01: "1.00%",
0.02: "2.00%",
0.022: "2.20%",
0.0222: "2.22%",
0.02222: "2.22%",
0.022222: "2.22%",
0.5: "50.00%",
0.55: "55.00%",
0.555: "55.50%",
0.5555: "55.55%",
0.55555: "55.55%",
0.75: "75.00%",
1.0: "100.00%",
1.5: "150.00%",
},
}

for m, values := range data {
t.Run(fmt.Sprintf("type=%s,contains=%s", m.Type.String(), m.Contains.String()), func(t *testing.T) {
for v, s := range values {
t.Run(fmt.Sprintf("v=%f", v), func(t *testing.T) {
assert.Equal(t, s, m.HumanizeValue(v))
})
}
})
}
}
5 changes: 5 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package main
import (
"gopkg.in/guregu/null.v3"
"gopkg.in/urfave/cli.v1"
"time"
)

// cliBool returns a CLI argument as a bool, which is invalid if not given.
@@ -24,3 +25,7 @@ func cliFloat64(cc *cli.Context, name string) null.Float {
func cliDuration(cc *cli.Context, name string) null.String {
return null.NewString(cc.Duration(name).String(), cc.IsSet(name))
}

func roundDuration(d, to time.Duration) time.Duration {
return d - (d % to)
}

0 comments on commit 02044a2

Please sign in to comment.