-
Notifications
You must be signed in to change notification settings - Fork 5
/
duration_test.go
54 lines (47 loc) · 1.27 KB
/
duration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package console
import (
"bytes"
"testing"
"time"
)
func TestDuration(t *testing.T) {
times := []time.Duration{
2*time.Hour + 3*time.Minute + 4*time.Second + 5*time.Millisecond + 6*time.Microsecond + 7*time.Nanosecond,
3*time.Minute + 4*time.Second + 5*time.Millisecond + 6*time.Microsecond + 7*time.Nanosecond,
4*time.Second + 5*time.Millisecond + 6*time.Microsecond + 7*time.Nanosecond,
5*time.Millisecond + 6*time.Microsecond + 7*time.Nanosecond,
6*time.Microsecond + 7*time.Nanosecond,
7 * time.Nanosecond,
time.Duration(0),
2*time.Hour + 7*time.Nanosecond,
-2*time.Hour + 7*time.Nanosecond,
}
b := [4096]byte{}
for _, tm := range times {
bd := appendDuration(b[:0], tm)
AssertEqual(t, tm.String(), string(bd))
}
bd := appendDuration(b[:0], 49*time.Hour+1*time.Second)
AssertEqual(t, "2d1h0m1s", string(bd))
}
func BenchmarkDuration(b *testing.B) {
d := 12*time.Hour + 13*time.Minute + 43*time.Second + 12*time.Millisecond
b.Run("std", func(b *testing.B) {
w := new(bytes.Buffer)
w.Grow(2048)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.WriteString(d.String())
w.Reset()
}
})
b.Run("append", func(b *testing.B) {
w := new(buffer)
w.Grow(2048)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.AppendDuration(d)
w.Reset()
}
})
}