-
Notifications
You must be signed in to change notification settings - Fork 25
/
tasks_benchmarks_test.go
59 lines (52 loc) · 1.2 KB
/
tasks_benchmarks_test.go
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
package tasks
import (
"testing"
"time"
)
func BenchmarkTasks(b *testing.B) {
// Create base scheduler to use
scheduler := New()
defer scheduler.Stop()
// Setup a single task for re-use
taskID, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Minute),
TaskFunc: func() error { return nil },
ErrFunc: func(_ error) {},
})
if err != nil {
b.Fatalf("Unable to schedule example task - %s", err)
}
defer scheduler.Del(taskID)
// Grab example for re-use
exampleTask, err := scheduler.Lookup(taskID)
if err != nil {
b.Fatalf("Unable to lookup newly added task - %s", err)
}
b.Run("Adding a scheduler", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := scheduler.Add(exampleTask)
if err != nil {
b.Fatalf("Unable to add new scheduled task - %s", err)
}
}
})
// Clear Excess Tasks
for id := range scheduler.Tasks() {
if id == taskID {
continue
}
scheduler.Del(id)
}
b.Run("Looking up a scheduled task", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := scheduler.Lookup(taskID)
if err != nil {
b.Fatalf("Unable to lookup scheduled tasks - %s", err)
}
}
})
}