-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_test.go
57 lines (47 loc) · 1.2 KB
/
benchmark_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
package dataloader
import (
"context"
"fmt"
"testing"
"time"
)
func BenchmarkDataLoader(b *testing.B) {
batchFunc := func(ctx context.Context, keys []int) []Result[string] {
results := make([]Result[string], len(keys))
for i, key := range keys {
results[i] = Result[string]{data: fmt.Sprintf("Result for %d", key)}
}
time.Sleep(1 * time.Millisecond)
return results
}
loader := New(batchFunc, WithBatchSize(10))
b.Run("direct.Batch", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = batchFunc(context.Background(), []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
}
})
b.Run("dataloader.Load", func(b *testing.B) {
loader.ClearAll()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 10; j++ {
_ = loader.Load(context.Background(), j)
}
}
})
b.Run("dataloader.LoadMany", func(b *testing.B) {
loader.ClearAll()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = loader.LoadMany(context.Background(), []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
}
})
b.Run("dataloader.LoadMap", func(b *testing.B) {
loader.ClearAll()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = loader.LoadMap(context.Background(), []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
}
})
}