-
Notifications
You must be signed in to change notification settings - Fork 151
/
pool_perf_test.go
216 lines (191 loc) · 5.7 KB
/
pool_perf_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
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
package pool
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
"github.com/jolestar/go-commons-pool/v2/concurrent"
)
type BenchObject struct {
Num int32
}
func BenchmarkPoolBorrowReturn(b *testing.B) {
ctx := context.Background()
pool := NewObjectPoolWithDefaultConfig(ctx, NewPooledObjectFactorySimple(func(context.Context) (interface{}, error) {
return &BenchObject{Num: rand.Int31()}, nil
}))
defer pool.Close(ctx)
for i := 0; i < b.N; i++ {
o, err := pool.BorrowObject(ctx)
if err != nil {
b.Fail()
}
err = pool.ReturnObject(ctx, o)
if err != nil {
b.Fail()
}
}
}
func BenchmarkPoolBorrowReturnParallel(b *testing.B) {
ctx := context.Background()
pool := NewObjectPoolWithDefaultConfig(ctx, NewPooledObjectFactorySimple(func(context.Context) (interface{}, error) {
return &BenchObject{}, nil
}))
pool.Config.MaxTotal = 100
defer pool.Close(ctx)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
o, err := pool.BorrowObject(ctx)
//fmt.Println("borrow:",reflect.ValueOf(o).Pointer())
if err != nil {
fmt.Println(err)
b.Fail()
}
err = pool.ReturnObject(ctx, o)
//fmt.Println("return:",reflect.ValueOf(o).Pointer())
if err != nil {
fmt.Println(err)
b.Fail()
}
}
})
}
type SleepingObjectFactory struct {
counter concurrent.AtomicInteger
}
func NewSleepingObjectFactory() *SleepingObjectFactory {
return &SleepingObjectFactory{counter: concurrent.AtomicInteger(0)}
}
func (f *SleepingObjectFactory) MakeObject(context.Context) (*PooledObject, error) {
if debugTest {
fmt.Println("factory MakeObject", f.counter.Get())
}
time.Sleep(500 * time.Millisecond)
return NewPooledObject(getNthObject(int(f.counter.Get()))), nil
}
func (f *SleepingObjectFactory) DestroyObject(ctx context.Context, object *PooledObject) error {
if debugTest {
fmt.Println("factory DestroyObject", object)
}
time.Sleep(250 * time.Millisecond)
return nil
}
func (f *SleepingObjectFactory) ValidateObject(ctx context.Context, object *PooledObject) bool {
if debugTest {
fmt.Println("factory ValidateObject", object)
}
time.Sleep(30 * time.Millisecond)
return true
}
func (f *SleepingObjectFactory) ActivateObject(ctx context.Context, object *PooledObject) error {
if debugTest {
fmt.Println("factory ActivateObject", object)
defer fmt.Println("factory ActivateObject end")
}
time.Sleep(10 * time.Millisecond)
return nil
}
func (f *SleepingObjectFactory) PassivateObject(ctx context.Context, object *PooledObject) error {
if debugTest {
fmt.Println("factory PassivateObject", object)
}
time.Sleep(10 * time.Millisecond)
return nil
}
type TaskStats struct {
waiting int
complete int
totalBorrowTime time.Duration
totalReturnTime time.Duration
nrSamples int
}
func runOnce(ctx context.Context, pool *ObjectPool, taskStats *TaskStats) (time.Duration, time.Duration) {
taskStats.waiting++
if debugTest {
fmt.Println(" waiting: ", taskStats.waiting, " complete: ", taskStats.complete)
}
begin := time.Now()
o, _ := pool.BorrowObject(ctx)
borrowTime := time.Since(begin)
taskStats.waiting--
if debugTest {
fmt.Println(
" waiting: ", taskStats.waiting,
" complete: ", taskStats.complete)
}
begin = time.Now()
pool.ReturnObject(ctx, o)
returnTime := time.Since(begin)
taskStats.complete++
return borrowTime, returnTime
}
func prefTask(ctx context.Context, pool *ObjectPool, nrIterations int) chan TaskStats {
ch := make(chan TaskStats, 1)
go func() {
taskStats := TaskStats{}
runOnce(ctx, pool, &taskStats) // warmup
for i := 0; i < nrIterations; i++ {
borrowTime, returnTime := runOnce(ctx, pool, &taskStats)
taskStats.totalBorrowTime += borrowTime
taskStats.totalReturnTime += returnTime
taskStats.nrSamples++
if debugTest {
fmt.Println("result ", taskStats.nrSamples, "borrow time: ", borrowTime, "\t"+
"return time: ", returnTime, "\t", "waiting: ",
taskStats.waiting, "\t", "complete: ",
taskStats.complete)
}
}
ch <- taskStats
}()
return ch
}
func perfRun(ctx context.Context, iterations int, nrThreads int, maxTotal int, maxIdle int) {
factory := NewSleepingObjectFactory()
pool := NewObjectPoolWithDefaultConfig(ctx, factory)
pool.Config.MaxTotal = maxTotal
pool.Config.MaxIdle = maxIdle
pool.Config.TestOnBorrow = true
chs := make([]chan TaskStats, nrThreads)
for i := 0; i < nrThreads; i++ {
chs[i] = prefTask(ctx, pool, iterations)
}
aggregate := TaskStats{}
for i := 0; i < nrThreads; i++ {
taskStats := <-chs[i]
close(chs[i])
aggregate.complete += taskStats.complete
aggregate.nrSamples += taskStats.nrSamples
aggregate.totalBorrowTime += taskStats.totalBorrowTime
aggregate.totalReturnTime += taskStats.totalReturnTime
aggregate.waiting += taskStats.waiting
}
fmt.Println("-----------------------------------------")
fmt.Println("nrIterations: ", iterations)
fmt.Println("nrThreads: ", nrThreads)
fmt.Println("maxTotal: ", maxTotal)
fmt.Println("maxIdle: ", maxIdle)
fmt.Println("nrSamples: ", aggregate.nrSamples)
fmt.Println("totalBorrowTime: ", aggregate.totalBorrowTime)
fmt.Println("totalReturnTime: ", aggregate.totalReturnTime)
fmt.Println("avg BorrowTime: ",
aggregate.totalBorrowTime/time.Duration(aggregate.nrSamples))
fmt.Println("avg ReturnTime: ",
aggregate.totalReturnTime/time.Duration(aggregate.nrSamples))
}
func perfMain(ctx context.Context) {
fmt.Println("Increase threads")
perfRun(ctx, 1, 50, 5, 5)
perfRun(ctx, 1, 100, 5, 5)
perfRun(ctx, 1, 200, 5, 5)
perfRun(ctx, 1, 400, 5, 5)
fmt.Println("Increase threads & poolsize")
perfRun(ctx, 1, 50, 5, 5)
perfRun(ctx, 1, 100, 10, 10)
perfRun(ctx, 1, 200, 20, 20)
perfRun(ctx, 1, 400, 40, 40)
fmt.Println("Increase maxIdle")
perfRun(ctx, 1, 400, 40, 5)
perfRun(ctx, 1, 400, 40, 40)
}