-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
252 lines (216 loc) · 4.79 KB
/
benchmark_test.go
File metadata and controls
252 lines (216 loc) · 4.79 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package cron
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
)
// BenchmarkAddJob 测试添加任务的性能
func BenchmarkAddJob(b *testing.B) {
benchmarks := []struct {
name string
jobCount int
}{
{"10jobs", 10},
{"50jobs", 50},
{"100jobs", 100},
{"500jobs", 500},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c := New()
for j := 0; j < bm.jobCount; j++ {
c.AddFunc("* * * * * *", func() {}, fmt.Sprintf("job-%d", j))
}
}
})
}
}
// BenchmarkRemoveJob 测试删除任务的性能
func BenchmarkRemoveJob(b *testing.B) {
benchmarks := []struct {
name string
jobCount int
}{
{"10jobs", 10},
{"50jobs", 50},
{"100jobs", 100},
{"500jobs", 500},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
// 准备测试数据
c := New()
for j := 0; j < bm.jobCount; j++ {
c.AddFunc("* * * * * *", func() {}, fmt.Sprintf("job-%d", j))
}
b.StartTimer()
for i := 0; i < b.N; i++ {
// 删除中间的任务
jobName := fmt.Sprintf("job-%d", bm.jobCount/2)
c.RemoveJob(jobName)
// 重新添加以便下次测试
c.AddFunc("* * * * * *", func() {}, jobName)
}
})
}
}
// BenchmarkSchedulerLoop 测试调度循环的性能(模拟运行)
func BenchmarkSchedulerLoop(b *testing.B) {
benchmarks := []struct {
name string
jobCount int
}{
{"10jobs", 10},
{"50jobs", 50},
{"100jobs", 100},
{"500jobs", 500},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
c := New()
var counter int64
// 添加任务
for j := 0; j < bm.jobCount; j++ {
c.AddFunc("* * * * * *", func() {
atomic.AddInt64(&counter, 1)
}, fmt.Sprintf("job-%d", j))
}
c.Start()
defer c.Stop()
b.ResetTimer()
// 让调度器运行 N 次循环
time.Sleep(time.Duration(b.N) * 100 * time.Millisecond)
})
}
}
// BenchmarkConcurrentAdd 测试并发添加任务
func BenchmarkConcurrentAdd(b *testing.B) {
benchmarks := []struct {
name string
goroutines int
jobsPerGo int
}{
{"10goroutines_10jobs", 10, 10},
{"50goroutines_10jobs", 50, 10},
{"100goroutines_10jobs", 100, 10},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c := New()
c.Start()
var wg sync.WaitGroup
wg.Add(bm.goroutines)
for g := 0; g < bm.goroutines; g++ {
go func(gid int) {
defer wg.Done()
for j := 0; j < bm.jobsPerGo; j++ {
c.AddFunc("* * * * * *", func() {},
fmt.Sprintf("job-g%d-j%d", gid, j))
}
}(g)
}
wg.Wait()
c.Stop()
}
})
}
}
// BenchmarkEntries 测试获取任务列表的性能
func BenchmarkEntries(b *testing.B) {
benchmarks := []struct {
name string
jobCount int
}{
{"10jobs", 10},
{"50jobs", 50},
{"100jobs", 100},
{"500jobs", 500},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
c := New()
for j := 0; j < bm.jobCount; j++ {
c.AddFunc("* * * * * *", func() {}, fmt.Sprintf("job-%d", j))
}
c.Start()
defer c.Stop()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.Entries()
}
})
}
}
// BenchmarkJobExecution 测试任务执行的吞吐量
func BenchmarkJobExecution(b *testing.B) {
benchmarks := []struct {
name string
jobCount int
workerPool int
}{
{"10jobs_noLimit", 10, 0},
{"10jobs_5workers", 10, 5},
{"100jobs_noLimit", 100, 0},
{"100jobs_50workers", 100, 50},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
var c *Cron
if bm.workerPool > 0 {
c = NewWithWorkerLimit(bm.workerPool)
} else {
c = New()
}
var counter int64
// 添加每秒执行的任务
for j := 0; j < bm.jobCount; j++ {
c.AddFunc("* * * * * *", func() {
atomic.AddInt64(&counter, 1)
time.Sleep(10 * time.Millisecond) // 模拟工作
}, fmt.Sprintf("job-%d", j))
}
c.Start()
defer c.Stop()
b.ResetTimer()
// 运行 N 秒
time.Sleep(time.Duration(b.N) * time.Second)
b.ReportMetric(float64(atomic.LoadInt64(&counter))/float64(b.N), "jobs/sec")
})
}
}
// BenchmarkMemoryUsage 测试内存使用情况
func BenchmarkMemoryUsage(b *testing.B) {
benchmarks := []struct {
name string
jobCount int
}{
{"10jobs", 10},
{"100jobs", 100},
{"1000jobs", 1000},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c := New()
for j := 0; j < bm.jobCount; j++ {
c.AddFunc("* * * * * *", func() {}, fmt.Sprintf("job-%d", j))
}
c.Start()
time.Sleep(100 * time.Millisecond)
c.Stop()
}
})
}
}