-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmempool_test.go
107 lines (100 loc) · 2.38 KB
/
mempool_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
package mempool
import "testing"
// func TestPool(t *testing.T) {
// poolSize := 5
// bufferCap := 1024
// pool := NewPool(poolSize, bufferCap)
//
// // Test Get and Put
// for i := 0; i < 100000; i++ {
// go func() {
// buffer := pool.Get()
// if buffer == nil {
// t.Errorf("Expected buffer to be not nil")
// }
// if buffer.Cap() != bufferCap {
// t.Errorf("Expected buffer capacity to be %d, but got %d", bufferCap, buffer.Cap())
// }
//
// pool.Put(buffer)
// }()
// }
//
// time.Sleep(5 * time.Second)
//
// // Test Resize
// newSize := 10
// pool.Resize(newSize)
// if pool.GetPoolSize() != newSize {
// t.Errorf("Expected pool size to be %d, but got %d", newSize, pool.GetPoolSize())
// }
// }
// benchmark pool and sync.Pool concurrency performance
// func BenchmarkPool(b *testing.B) {
// // mempool
// poolSize := 1000
// bufferCap := 1024
// pool := NewPool(poolSize, bufferCap)
//
// b.RunParallel(func(pb *testing.PB) {
// for pb.Next() {
// buffer := pool.Get()
// defer func() {
// pool.Put(buffer)
// }()
// if buffer == nil {
// b.Errorf("Expected buffer to be not nil")
// }
// if buffer.Cap() != bufferCap {
// b.Errorf("Expected buffer capacity to be %d, but got %d", bufferCap, buffer.Cap())
// }
// }
// })
//
// }
// benchmark pool and sync.Pool concurrency performance
func BenchmarkPool(b *testing.B) {
// mempool
poolSize := 1000
bufferCap := 1024
pool := NewPool(poolSize, bufferCap)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
func() {
buffer := pool.Get()
defer func() {
pool.Put(buffer)
}()
if buffer == nil {
b.Errorf("Expected buffer to be not nil")
}
if buffer.Cap() != bufferCap {
b.Errorf("Expected buffer capacity to be %d, but got %d", bufferCap, buffer.Cap())
}
}()
}
})
}
// func BenchmarkSyncPool(b *testing.B) {
// bufferCap := 1024
// // sync.Pool
// syncPool := &sync.Pool{
// New: func() interface{} {
// return make([]byte, bufferCap)
// },
// }
//
// b.RunParallel(func(pb *testing.PB) {
// for pb.Next() {
// buffer := syncPool.Get().([]byte)
// if buffer == nil {
// b.Errorf("Expected buffer to be not nil")
// }
// if cap(buffer) != bufferCap {
// b.Errorf("Expected buffer capacity to be %d, but got %d", bufferCap, cap(buffer))
// }
// syncPool.Put(buffer)
// }
// })
//
// }