Skip to content

Commit 15f269e

Browse files
committed
refactor(calendar): Add Gregorian calendar benchmark test
1 parent b91b55a commit 15f269e

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

calendar/gregorian_bench_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package calendar
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func BenchmarkString(b *testing.B) {
9+
testCases := []*Gregorian{
10+
{Time: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)},
11+
{Time: time.Date(2024, 2, 29, 12, 30, 45, 123456789, time.UTC)}, // 闰年闰日
12+
{Time: time.Date(2023, 12, 31, 23, 59, 59, 999999999, time.UTC)},
13+
{Time: time.Date(2020, 8, 5, 0, 0, 0, 0, time.UTC)}, // 闰年
14+
{Time: time.Date(2021, 6, 15, 15, 30, 0, 0, time.UTC)},
15+
}
16+
17+
b.ResetTimer()
18+
for i := 0; i < b.N; i++ {
19+
g := testCases[i%len(testCases)]
20+
g.String()
21+
}
22+
}
23+
24+
func BenchmarkStringNil(b *testing.B) {
25+
var g *Gregorian
26+
27+
b.ResetTimer()
28+
for i := 0; i < b.N; i++ {
29+
g.String()
30+
}
31+
}
32+
33+
func BenchmarkStringZeroTime(b *testing.B) {
34+
g := &Gregorian{}
35+
36+
b.ResetTimer()
37+
for i := 0; i < b.N; i++ {
38+
g.String()
39+
}
40+
}
41+
42+
func BenchmarkIsLeapYear(b *testing.B) {
43+
testCases := []*Gregorian{
44+
{Time: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)}, // 闰年
45+
{Time: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC)}, // 闰年闰日
46+
{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪闰年
47+
{Time: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)}, // 非闰年
48+
{Time: time.Date(2023, 12, 31, 0, 0, 0, 0, time.UTC)}, // 非闰年
49+
{Time: time.Date(2100, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪非闰年
50+
}
51+
52+
b.ResetTimer()
53+
for i := 0; i < b.N; i++ {
54+
g := testCases[i%len(testCases)]
55+
g.IsLeapYear()
56+
}
57+
}
58+
59+
func BenchmarkIsLeapYearNil(b *testing.B) {
60+
var g *Gregorian
61+
62+
b.ResetTimer()
63+
for i := 0; i < b.N; i++ {
64+
g.IsLeapYear()
65+
}
66+
}
67+
68+
func BenchmarkIsLeapYearWithError(b *testing.B) {
69+
g := &Gregorian{Error: &time.ParseError{}}
70+
71+
b.ResetTimer()
72+
for i := 0; i < b.N; i++ {
73+
g.IsLeapYear()
74+
}
75+
}
76+
77+
func BenchmarkIsLeapYearZeroTime(b *testing.B) {
78+
g := &Gregorian{}
79+
80+
b.ResetTimer()
81+
for i := 0; i < b.N; i++ {
82+
g.IsLeapYear()
83+
}
84+
}
85+
86+
func BenchmarkLeapYearEdgeCases(b *testing.B) {
87+
testCases := []*Gregorian{
88+
{Time: time.Date(1600, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪闰年
89+
{Time: time.Date(1700, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪非闰年
90+
{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪非闰年
91+
{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪非闰年
92+
{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪闰年
93+
{Time: time.Date(2100, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪非闰年
94+
{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪非闰年
95+
{Time: time.Date(2300, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪非闰年
96+
{Time: time.Date(2400, 1, 1, 0, 0, 0, 0, time.UTC)}, // 世纪闰年
97+
}
98+
99+
b.ResetTimer()
100+
for i := 0; i < b.N; i++ {
101+
g := testCases[i%len(testCases)]
102+
g.IsLeapYear()
103+
}
104+
}
105+
106+
func BenchmarkLeapYearRegularYears(b *testing.B) {
107+
testCases := []*Gregorian{
108+
{Time: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)}, // 闰年
109+
{Time: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)}, // 非闰年
110+
{Time: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)}, // 非闰年
111+
{Time: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)}, // 非闰年
112+
{Time: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)}, // 闰年
113+
{Time: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)}, // 非闰年
114+
{Time: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)}, // 非闰年
115+
{Time: time.Date(2027, 1, 1, 0, 0, 0, 0, time.UTC)}, // 非闰年
116+
{Time: time.Date(2028, 1, 1, 0, 0, 0, 0, time.UTC)}, // 闰年
117+
{Time: time.Date(2029, 1, 1, 0, 0, 0, 0, time.UTC)}, // 非闰年
118+
}
119+
120+
b.ResetTimer()
121+
for i := 0; i < b.N; i++ {
122+
g := testCases[i%len(testCases)]
123+
g.IsLeapYear()
124+
}
125+
}

0 commit comments

Comments
 (0)