generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
time_test.go
97 lines (93 loc) 路 2.46 KB
/
time_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package u_test
import (
"fmt"
"testing"
"time"
"moul.io/u"
)
func ExampleShortDuration() {
fmt.Println(u.ShortDuration(time.Nanosecond * 0))
fmt.Println(u.ShortDuration(time.Nanosecond))
fmt.Println(u.ShortDuration(time.Nanosecond * 12))
fmt.Println(u.ShortDuration(time.Nanosecond * 123))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345678))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456789))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567891))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345678912))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456789123))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567891234))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345678912345))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456789123456))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567891234567))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345678912345678))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456789123456789))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567891234567891))
fmt.Println("-------")
fmt.Println(u.ShortDuration(time.Nanosecond))
fmt.Println(u.ShortDuration(time.Microsecond))
fmt.Println(u.ShortDuration(time.Millisecond))
fmt.Println(u.ShortDuration(time.Second))
fmt.Println(u.ShortDuration(time.Minute))
fmt.Println(u.ShortDuration(time.Hour))
fmt.Println(u.ShortDuration(time.Hour * 24))
fmt.Println(u.ShortDuration(time.Hour + time.Second))
// Output:
// 0s
// 1ns
// 12ns
// 123ns
// 1.2碌s
// 12.3碌s
// 123.5碌s
// 1.2ms
// 12.3ms
// 123.5ms
// 1.2s
// 12.3s
// 2m3s
// 20m35s
// 3h25m46s
// 1d10h18m
// 14d6h56m
// 142d21h21m
// 1428d21h33m
// 14288d23h32m
// -------
// 1ns
// 1碌s
// 1ms
// 1s
// 1m
// 1h
// 1d
// 1h0m1s
}
func BenchmarkShortDuration(b *testing.B) {
cases := []struct {
Name string
Data time.Duration
}{
{"Simple", time.Second * 5},
{"Complex", time.Nanosecond * 123456789101112},
}
for _, bc := range cases {
b.Run(bc.Name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.ShortDuration(bc.Data)
}
})
b.Run(bc.Name+"-parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u.ShortDuration(bc.Data)
}
})
})
}
}