forked from arran4/dateparse
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbench_test.go
More file actions
224 lines (198 loc) · 6.1 KB
/
bench_test.go
File metadata and controls
224 lines (198 loc) · 6.1 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package dateparse
import (
"fmt"
"testing"
"time"
)
/*
go test -bench Parse
// Aarons Laptop Lenovo 900 Feb 2018
BenchmarkShotgunParse-4 50000 30045 ns/op 13136 B/op 169 allocs/op
BenchmarkParseAny-4 200000 8627 ns/op 144 B/op 3 allocs/op
// ifreddyrondon Laptop MacBook Pro (Retina, Mid 2012) March 2018
BenchmarkShotgunParse-8 50000 33940 ns/op 13136 B/op 169 allocs/op
BenchmarkParseAny-8 200000 10146 ns/op 912 B/op 29 allocs/op
BenchmarkParseDateString-8 10000 123077 ns/op 208 B/op 13 allocs/op
// Klondike Dragon Dec 2023
cpu: 12th Gen Intel(R) Core(TM) i7-1255U
BenchmarkShotgunParse-12 62788 18113 ns/op 19448 B/op 474 allocs/op
BenchmarkParseAny-12 347020 3455 ns/op 48 B/op 2 allocs/op
BenchmarkBigShotgunParse-12 1226 951271 ns/op 1214937 B/op 27245 allocs/op
BenchmarkBigParseAny-12 4234 267893 ns/op 27492 B/op 961 allocs/op
BenchmarkBigParseIn-12 4032 280900 ns/op 30422 B/op 1033 allocs/op
BenchmarkBigParseRetryAmbiguous-12 4453 282475 ns/op 29558 B/op 1030 allocs/op
BenchmarkShotgunParseErrors-12 19240 62641 ns/op 67080 B/op 1679 allocs/op
BenchmarkParseAnyErrors-12 185677 6179 ns/op 752 B/op 23 allocs/op
BenchmarkBigParseAnyErrors-12 26688 44885 ns/op 480 B/op 94 allocs/op
BenchmarkParseAmbiguous-12 1590302 752.9 ns/op 296 B/op 7 allocs/op
BenchmarkParseWeekdayAndFullMonth-12 2141109 555.0 ns/op 16 B/op 2 allocs/op
*/
func BenchmarkShotgunParse(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, dateStr := range testDates {
// This is the non dateparse traditional approach
_, _ = parseShotgunStyle(dateStr)
}
}
}
func BenchmarkParseAny(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, dateStr := range testDates {
_, _ = ParseAny(dateStr)
}
}
}
func BenchmarkBigShotgunParse(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, t := range testInputs {
// This is the non dateparse traditional approach
_, _ = parseShotgunStyle(t.in)
}
}
}
func BenchmarkBigParseAny(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, t := range testInputs {
_, _ = ParseAny(t.in)
}
}
}
func BenchmarkBigParseIn(b *testing.B) {
b.ReportAllocs()
loc, _ := time.LoadLocation("America/New_York")
for i := 0; i < b.N; i++ {
for _, t := range testInputs {
_, _ = ParseIn(t.in, loc)
}
}
}
func BenchmarkBigParseRetryAmbiguous(b *testing.B) {
b.ReportAllocs()
opts := []ParserOption{RetryAmbiguousDateWithSwap(true)}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, t := range testInputs {
_, _ = ParseAny(t.in, opts...)
}
}
}
func BenchmarkShotgunParseErrors(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, t := range testParseErrors {
// This is the non dateparse traditional approach
_, _ = parseShotgunStyle(t.in)
}
}
}
func BenchmarkParseAnyErrors(b *testing.B) {
b.ReportAllocs()
opts := []ParserOption{SimpleErrorMessages(true)}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, t := range testParseErrors {
_, _ = ParseAny(t.in, opts...)
}
}
}
func BenchmarkBigParseAnyErrors(b *testing.B) {
b.ReportAllocs()
opts := []ParserOption{SimpleErrorMessages(true)}
// manufacture a bunch of different tests with random errors put in them
var testBigErrorInputs []string
for index, t := range testInputs {
b := []byte(t.in)
spread := 4 + (index % 4)
startingIndex := spread % len(b)
for i := startingIndex; i < len(b); i += spread {
b[i] = '?'
}
testBigErrorInputs = append(testBigErrorInputs, string(b))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, in := range testBigErrorInputs {
_, err := ParseAny(in, opts...)
if err == nil {
panic(fmt.Sprintf("expected parsing to fail: %s", in))
}
}
}
}
func BenchmarkParseAmbiguous(b *testing.B) {
b.ReportAllocs()
opts := []ParserOption{RetryAmbiguousDateWithSwap(true)}
b.ResetTimer()
for i := 0; i < b.N; i++ {
MustParse("13/02/2014 04:08:09 +0000 UTC", opts...)
}
}
func BenchmarkParseWeekdayAndFullMonth(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
MustParse("Monday 02 December 2006 03:04:05 PM UTC")
}
}
/*
func BenchmarkParseDateString(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, dateStr := range testDates {
timeutils.ParseDateString(dateStr)
}
}
}
*/
var (
testDates = []string{
"2012/03/19 10:11:59",
"2012/03/19 10:11:59.3186369",
"2009-08-12T22:15:09-07:00",
"2014-04-26 17:24:37.3186369",
"2012-08-03 18:31:59.257000000",
"2013-04-01 22:43:22",
"2014-04-26 17:24:37.123",
"2014-12-16 06:20:00 UTC",
"1384216367189",
"1332151919",
"2014-05-11 08:20:13,787",
"2014-04-26 05:24:37 PM",
"2014-04-26",
}
ErrDateFormat = fmt.Errorf("invalid date format")
timeFormats = []string{
// ISO 8601ish formats
time.RFC3339Nano,
time.RFC3339,
// Unusual formats, prefer formats with timezones
time.RFC1123Z,
time.RFC1123,
time.RFC822Z,
time.RFC822,
time.UnixDate,
time.RubyDate,
time.ANSIC,
// Hilariously, Go doesn't have a const for it's own time layout.
// See: https://code.google.com/p/go/issues/detail?id=6587
"2006-01-02 15:04:05.999999999 -0700 MST",
// No timezone information
"2006-01-02T15:04:05.999999999",
"2006-01-02T15:04:05",
"2006-01-02 15:04:05.999999999",
"2006-01-02 15:04:05",
}
)
func parseShotgunStyle(raw string) (time.Time, error) {
for _, format := range timeFormats {
t, err := time.Parse(format, raw)
if err == nil {
// Parsed successfully
return t, nil
}
}
return time.Time{}, ErrDateFormat
}