-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathcarbon_bench_test.go
More file actions
142 lines (125 loc) · 2.53 KB
/
carbon_bench_test.go
File metadata and controls
142 lines (125 loc) · 2.53 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package carbon
import (
"sync"
"testing"
"time"
)
func BenchmarkNewCarbon(b *testing.B) {
loc, _ := time.LoadLocation(PRC)
tt, _ := time.ParseInLocation(DateTimeLayout, "2020-08-05 13:14:15", loc)
b.Run("sequential", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N/10; i++ {
NewCarbon(tt)
}
})
b.Run("concurrent", func(b *testing.B) {
var wg sync.WaitGroup
b.ResetTimer()
for i := 0; i < b.N/10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
NewCarbon(tt)
}()
}
wg.Wait()
})
b.Run("parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
NewCarbon(tt)
}
})
})
}
func BenchmarkCopy(b *testing.B) {
b.Run("sequential", func(b *testing.B) {
c := Parse("2020-08-05").SetLocale("zh-CN")
b.ResetTimer()
for i := 0; i < b.N/10; i++ {
c.Copy()
}
})
b.Run("concurrent", func(b *testing.B) {
var wg sync.WaitGroup
c := Parse("2020-08-05").SetLocale("zh-CN")
b.ResetTimer()
for i := 0; i < b.N/10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
c.Copy()
}()
}
wg.Wait()
})
b.Run("parallel", func(b *testing.B) {
c := Parse("2020-08-05").SetLocale("zh-CN")
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
c.Copy()
}
})
})
}
func BenchmarkSleep(b *testing.B) {
b.Run("sequential", func(b *testing.B) {
testNow := Parse("2020-08-05 13:14:15")
SetTestNow(testNow)
defer ClearTestNow()
b.ResetTimer()
for i := 0; i < b.N/10; i++ {
Sleep(1 * time.Hour)
}
})
b.Run("concurrent", func(b *testing.B) {
var wg sync.WaitGroup
testNow := Parse("2020-08-05 13:14:15")
SetTestNow(testNow)
defer ClearTestNow()
b.ResetTimer()
for i := 0; i < b.N/10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
Sleep(1 * time.Hour)
}()
}
wg.Wait()
})
b.Run("parallel", func(b *testing.B) {
testNow := Parse("2020-08-05 13:14:15")
SetTestNow(testNow)
defer ClearTestNow()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Sleep(1 * time.Hour)
}
})
})
}
func BenchmarkSleep_DifferentDurations(b *testing.B) {
durations := []time.Duration{
1 * time.Nanosecond,
1 * time.Microsecond,
1 * time.Millisecond,
1 * time.Second,
1 * time.Minute,
1 * time.Hour,
}
for _, duration := range durations {
b.Run(duration.String(), func(b *testing.B) {
testNow := Parse("2020-08-05 13:14:15")
SetTestNow(testNow)
defer ClearTestNow()
b.ResetTimer()
for i := 0; i < b.N/10; i++ {
Sleep(duration)
}
})
}
}