-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
84 lines (75 loc) · 2.47 KB
/
bench_test.go
File metadata and controls
84 lines (75 loc) · 2.47 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
package nowandlater
import (
"testing"
"time"
. "github.com/client9/nowandlater/languages"
)
// fixedBenchNow is a pre-built time value used as the reference clock in all
// Parser benchmarks, avoiding time.Now() overhead in the measurement.
var fixedBenchNow = time.Date(2026, 3, 22, 10, 0, 0, 0, time.UTC)
var benchParser = Parser{
Lang: &LangEn,
Now: func() time.Time { return fixedBenchNow },
}
// BenchmarkStdlibRFC3339 is the baseline: Go's own time.Parse on a well-formed
// RFC3339 string. Use this as the lower-bound reference.
func BenchmarkStdlibRFC3339(b *testing.B) {
b.ReportAllocs()
const input = "2026-03-22T09:30:00Z"
b.ResetTimer()
for b.Loop() {
_, _ = time.Parse(time.RFC3339, input)
}
}
// BenchmarkParserRFC3339 measures our full pipeline (preprocess → tokenize →
// dispatch → resolve) on the same RFC3339 string that BenchmarkStdlibRFC3339
// uses. This shows the overhead of natural-language support on a structured input.
func BenchmarkParserRFC3339(b *testing.B) {
b.ReportAllocs()
const input = "2026-03-22T09:30:00Z"
b.ResetTimer()
for b.Loop() {
_, _ = benchParser.Parse(input)
}
}
// BenchmarkParserNaturalAbsolute measures parsing of a natural-language absolute
// date: a form that time.Parse cannot handle at all.
func BenchmarkParserNaturalAbsolute(b *testing.B) {
b.ReportAllocs()
const input = "March 22, 2026 at 9:30am"
b.ResetTimer()
for b.Loop() {
_, _ = benchParser.Parse(input)
}
}
// BenchmarkParserRelative measures parsing of a purely relative expression,
// which requires resolution against the reference clock.
func BenchmarkParserRelative(b *testing.B) {
b.ReportAllocs()
const input = "next Monday at 9:30"
b.ResetTimer()
for b.Loop() {
_, _ = benchParser.Parse(input)
}
}
// BenchmarkParserSuite runs a representative cross-section of input types so a
// single run gives a broad picture of average throughput.
func BenchmarkParserSuite(b *testing.B) {
cases := []string{
"2026-03-22T09:30:00Z", // RFC3339 (structured)
"March 22, 2026", // natural absolute date
"March 22, 2026 at 9:30am", // natural absolute date+time
"next Monday", // relative weekday
"next Monday at 9:30", // relative weekday + time
"in 3 days", // relative delta
"tomorrow at noon", // anchor + time-of-day
"next week", // direction + unit
}
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
for _, input := range cases {
_, _ = benchParser.Parse(input)
}
}
}