-
Notifications
You must be signed in to change notification settings - Fork 4
/
batch_test.go
129 lines (102 loc) · 2.04 KB
/
batch_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
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
package batch
import (
"fmt"
//"time"
"testing"
)
// Resources Structure
type Resources struct {
id int
name string
flag bool
}
func TestBatch(t *testing.T) {
t.Run("Batch-1", func(t *testing.T) {
t.Parallel()
mFlag := 10
rFlag := 15
b := NewBatch(WithMaxItems(uint64(mFlag)))
b.StartBatchProcessing()
for i := 1; i <= rFlag; i++ {
b.Item <- &Resources{
id: i,
name: fmt.Sprintf("%s%d", "R-", i),
flag: false,
}
}
b.Close()
})
t.Run("Batch-2", func(t *testing.T) {
t.Parallel()
mFlag := 10
rFlag := 100
b := NewBatch(WithMaxItems(uint64(mFlag)))
b.StartBatchProcessing()
for i := 1; i <= rFlag; i++ {
b.Item <- &Resources{
id: i,
name: fmt.Sprintf("%s%d", "R-", i),
flag: false,
}
}
var panics bool
func() {
defer func() {
if r := recover(); r != nil {
t.Log("Recover--", r)
panics = true
}
}()
b.StartBatchProcessing()
for i := 1; i <= rFlag; i++ {
b.Item <- &Resources{
id: i,
name: fmt.Sprintf("%s%d", "R-", i),
flag: false,
}
}
}()
if !panics {
t.Error("Testing Concurrent batch processing should fail")
}
b.Close()
})
}
func benchmarkBatch(numResources, maxItems int, b *testing.B) {
for n := 0; n < b.N; n++ {
b := NewBatch(WithMaxItems(uint64(maxItems)))
b.StartBatchProcessing()
for i := 1; i <= numResources; i++ {
b.Item <- &Resources{
id: i,
name: fmt.Sprintf("%s%d", "R-", i),
flag: false,
}
}
b.Close()
}
}
func BenchmarkBatchR100M5(b *testing.B) {
benchmarkBatch(100, 5, b)
}
func BenchmarkBatchR10M5(b *testing.B) {
benchmarkBatch(10, 5, b)
}
func BenchmarkBatchR100M10(b *testing.B) {
benchmarkBatch(100, 10, b)
}
func BenchmarkBatch(b *testing.B) {
b.ResetTimer()
b.Run("Bench-1", func(b *testing.B) {
benchmarkBatch(100, 5, b)
})
b.Run("Bench-2", func(b *testing.B) {
benchmarkBatch(500, 50, b)
})
b.Run("Bench-3", func(b *testing.B) {
benchmarkBatch(3000, 300, b)
})
b.Run("Bench-4", func(b *testing.B) {
benchmarkBatch(10000, 100, b)
})
}