-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
126 lines (114 loc) · 2.75 KB
/
Copy pathbench_test.go
File metadata and controls
126 lines (114 loc) · 2.75 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
package cadence
import (
"testing"
"time"
)
// BenchmarkParsing benchmarks parsing a standard 5-field cron spec.
func BenchmarkParsing(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := StandardParser.Parse("30 4 1 * *")
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkParsingDescriptor benchmarks parsing a descriptor spec.
func BenchmarkParsingDescriptor(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := StandardParser.Parse("@every 5m")
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkParsingWithSeconds benchmarks parsing a 6-field spec.
func BenchmarkParsingWithSeconds(b *testing.B) {
p := NewParser(Second | Minute | Hour | Dom | Month | Dow | Descriptor)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := p.Parse("0 30 4 1 * *")
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkNext benchmarks computing the next schedule time for a SpecSchedule.
func BenchmarkNext(b *testing.B) {
sched, err := StandardParser.Parse("30 4 1 * *")
if err != nil {
b.Fatal(err)
}
t := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = sched.Next(t)
}
}
// BenchmarkNextEvery benchmarks computing next for a ConstantDelaySchedule.
func BenchmarkNextEvery(b *testing.B) {
sched := Every(5 * time.Minute)
t := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = sched.Next(t)
}
}
// BenchmarkAddFunc benchmarks adding a function to a stopped Cron.
func BenchmarkAddFunc(b *testing.B) {
c := New()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := c.AddFunc("* * * * *", func() {})
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkAddFuncWhileRunning benchmarks adding entries while the scheduler
// is running (exercises the channel path).
func BenchmarkAddFuncWhileRunning(b *testing.B) {
c := New()
c.Start()
b.Cleanup(func() { c.Stop() })
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := c.AddFunc("@every 1h", func() {})
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkEntries benchmarks snapshotting entries from a stopped Cron.
func BenchmarkEntries(b *testing.B) {
c := New()
for i := 0; i < 100; i++ {
c.AddFunc("* * * * *", func() {}) //nolint:errcheck
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.Entries()
}
}
// BenchmarkEntriesWhileRunning benchmarks the snapshot path through the
// run-loop channel.
func BenchmarkEntriesWhileRunning(b *testing.B) {
c := New()
for i := 0; i < 100; i++ {
c.AddFunc("@every 1h", func() {}) //nolint:errcheck
}
c.Start()
b.Cleanup(func() { c.Stop() })
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.Entries()
}
}