-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
135 lines (119 loc) · 3.55 KB
/
benchmark_test.go
File metadata and controls
135 lines (119 loc) · 3.55 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
package ixdtf_test
import (
"sort"
"testing"
"time"
"github.com/8beeeaaat/ixdtf"
)
// Benchmark targets focus on core hot paths:
// 1. Format / FormatNano with different extension complexities
// 2. Parse / Validate for typical, extended, and error cases
// Goal: Provide ns/op & allocs metrics for public API.
func benchmarkTime() time.Time { return time.Date(2025, 1, 2, 3, 4, 5, 123456789, time.UTC) }
func BenchmarkFormat(b *testing.B) {
base := benchmarkTime()
benchTokyo := time.FixedZone("Asia/Tokyo", 9*3600)
benchParis := time.FixedZone("Europe/Paris", 1*3600)
cases := []struct {
name string
t time.Time
ext *ixdtf.IXDTFExtensions
}{
{"noext", base, ixdtf.NewIXDTFExtensions(nil)},
{"tz", base.In(benchTokyo), ixdtf.NewIXDTFExtensions(nil)},
{"tz_specified", base, ixdtf.NewIXDTFExtensions(&ixdtf.NewIXDTFExtensionsArgs{Location: benchParis})},
{
"tags",
base,
ixdtf.NewIXDTFExtensions(
&ixdtf.NewIXDTFExtensionsArgs{Tags: map[string]string{"u-ca": "gregory", "a": "1", "b": "2"}},
),
},
}
sort.Slice(cases, func(i, j int) bool { return cases[i].name < cases[j].name })
b.ReportAllocs()
for _, c := range cases {
b.Run(c.name, func(sb *testing.B) {
sb.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = ixdtf.Format(c.t, c.ext)
}
})
})
}
}
func BenchmarkFormatNano(b *testing.B) {
base := benchmarkTime()
benchTokyo := time.FixedZone("Asia/Tokyo", 9*3600)
cases := []struct {
name string
t time.Time
ext *ixdtf.IXDTFExtensions
}{
{"noext", base, ixdtf.NewIXDTFExtensions(nil)},
{
"tz_tags",
base.In(benchTokyo),
ixdtf.NewIXDTFExtensions(&ixdtf.NewIXDTFExtensionsArgs{Tags: map[string]string{"u-ca": "gregory"}}),
},
}
sort.Slice(cases, func(i, j int) bool { return cases[i].name < cases[j].name })
b.ReportAllocs()
for _, c := range cases {
b.Run(c.name, func(sb *testing.B) {
sb.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = ixdtf.FormatNano(c.t, c.ext)
}
})
})
}
}
func BenchmarkParse(b *testing.B) {
cases := []struct {
name, input string
strict bool
}{
{"rfc3339", "2025-01-02T03:04:05Z", false},
{"rfc3339_nano", "2025-01-02T03:04:05.123456789Z", false},
{"extended_tz", "2025-06-07T08:09:10+09:00[Asia/Tokyo]", false},
{"extended_tz_tags", "2025-06-07T08:09:10+01:00[Europe/Paris][u-ca=gregory]", false},
{"mismatch_non_strict", "2025-06-01T12:00:00+09:00[America/New_York]", false},
{"mismatch_strict", "2025-06-01T12:00:00+09:00[America/New_York]", true},
{"invalid_suffix", "2025-01-01T00:00:00Z[unclosed", false},
}
sort.Slice(cases, func(i, j int) bool { return cases[i].name < cases[j].name })
b.ReportAllocs()
for _, c := range cases {
b.Run(c.name, func(sb *testing.B) {
sb.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _, _ = ixdtf.Parse(c.input, c.strict)
}
})
})
}
}
func BenchmarkValidate(b *testing.B) {
cases := []struct {
name, input string
strict bool
}{
{"rfc3339", "2025-01-02T03:04:05Z", false},
{"extended", "2025-06-07T08:09:10+09:00[Asia/Tokyo]", false},
{"extended_tags", "2025-06-07T08:09:10Z[u-ca=gregory]", false},
{"mismatch_strict", "2025-06-01T12:00:00+09:00[America/New_York]", true},
{"invalid_ext", "2025-01-01T00:00:00Z[INVALID=val]", false},
}
sort.Slice(cases, func(i, j int) bool { return cases[i].name < cases[j].name })
b.ReportAllocs()
for _, c := range cases {
b.Run(c.name, func(sb *testing.B) {
sb.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = ixdtf.Validate(c.input, c.strict)
}
})
})
}
}