| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
"math/rand" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
func Test_AppendDuration(t *testing.T) { |
| 12 |
t.Parallel() |
| 13 |
check := func(d time.Duration) { |
| 14 |
t.Helper() |
| 15 |
want := d.String() |
| 16 |
if got := string(AppendDuration(nil, d)); got != want { |
| 17 |
t.Fatalf("AppendDuration(%d) = %q, want %q", int64(d), got, want) |
| 18 |
} |
| 19 |
if got := string(AppendDuration([]byte("x"), d)); got != "x"+want { |
| 20 |
t.Fatalf("AppendDuration(%d) with prefix = %q, want %q", int64(d), got, "x"+want) |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
// Every unit boundary, the values just around it, and the extremes. |
| 25 |
for _, base := range []time.Duration{ |
| 26 |
0, 1, 999, 1000, 1001, 999999, time.Millisecond, time.Millisecond + 1, |
| 27 |
999999999, time.Second, time.Second + 1, 1500 * time.Millisecond, |
| 28 |
59 * time.Second, time.Minute, time.Minute + time.Second, 59*time.Minute + 59*time.Second, |
| 29 |
time.Hour, time.Hour + time.Nanosecond, 100 * time.Hour, 2540400 * time.Hour, |
| 30 |
math.MaxInt64, math.MinInt64, math.MinInt64 + 1, |
| 31 |
} { |
| 32 |
check(base) |
| 33 |
check(-base) |
| 34 |
check(base + 1) |
| 35 |
check(base - 1) |
| 36 |
} |
| 37 |
// Every count of trailing zeros in the fraction, in every unit. |
| 38 |
for _, unit := range []time.Duration{time.Microsecond, time.Millisecond, time.Second} { |
| 39 |
for f := time.Duration(1); f < unit; f *= 10 { |
| 40 |
check(unit + f) |
| 41 |
check(unit + 7*f) |
| 42 |
check(unit - f) |
| 43 |
} |
| 44 |
} |
| 45 |
// Dense sweeps through the small units. |
| 46 |
for d := range 3000 { |
| 47 |
check(time.Duration(d)) |
| 48 |
} |
| 49 |
for d := time.Duration(0); d < 3*time.Second; d += 1234567 { |
| 50 |
check(d) |
| 51 |
check(-d) |
| 52 |
} |
| 53 |
// A deterministic pseudo-random spread across every magnitude. |
| 54 |
rng := rand.New(rand.NewSource(1)) //nolint:gosec // deterministic test data |
| 55 |
for range 200000 { |
| 56 |
x := rng.Uint64() |
| 57 |
check(time.Duration(x >> (x % 64))) |
| 58 |
check(-time.Duration(x >> (x % 64))) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func Benchmark_AppendDuration(b *testing.B) { |
| 63 |
inputs := []struct { |
| 64 |
name string |
| 65 |
value time.Duration |
| 66 |
}{ |
| 67 |
{"micros", 823 * time.Microsecond}, |
| 68 |
{"millis-frac", 12345678 * time.Nanosecond}, |
| 69 |
{"hours", 3*time.Hour + 4*time.Minute + 5*time.Second}, |
| 70 |
} |
| 71 |
dst := make([]byte, 0, 32) |
| 72 |
for _, input := range inputs { |
| 73 |
b.Run(input.name+"/fiber", func(b *testing.B) { |
| 74 |
b.ReportAllocs() |
| 75 |
for b.Loop() { |
| 76 |
AppendDuration(dst, input.value) |
| 77 |
} |
| 78 |
}) |
| 79 |
b.Run(input.name+"/default", func(b *testing.B) { |
| 80 |
b.ReportAllocs() |
| 81 |
var s string |
| 82 |
for b.Loop() { |
| 83 |
s = input.value.String() |
| 84 |
} |
| 85 |
_ = s |
| 86 |
}) |
| 87 |
b.Run(input.name+"/fmt", func(b *testing.B) { |
| 88 |
b.ReportAllocs() |
| 89 |
var out []byte |
| 90 |
for b.Loop() { |
| 91 |
out = fmt.Appendf(dst, "%v", input.value) |
| 92 |
} |
| 93 |
_ = out |
| 94 |
}) |
| 95 |
} |
| 96 |
} |