-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
80 lines (67 loc) · 1.81 KB
/
Copy pathexample_test.go
File metadata and controls
80 lines (67 loc) · 1.81 KB
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
package bytebufferpool_test
import (
"fmt"
bytebufferpool "github.com/ymj4023/bytebufferpool"
)
func ExamplePool_Acquire() {
pool, _ := bytebufferpool.New(bytebufferpool.DefaultConfig(bytebufferpool.Fast))
lease := pool.Acquire(5)
copy(lease.Bytes(), "hello")
fmt.Println(string(lease.Bytes()))
fmt.Println(lease.Release())
// Output:
// hello
// Retained
}
func ExamplePool_AcquireSlice() {
pool, _ := bytebufferpool.New(bytebufferpool.DefaultConfig(bytebufferpool.Fast))
buffer := pool.AcquireSlice(5)
copy(buffer, "hello")
fmt.Println(string(buffer))
fmt.Println(pool.ReleaseSlice(buffer))
// Output:
// hello
// Retained
}
func ExamplePool_Buffer() {
pool, _ := bytebufferpool.New(bytebufferpool.DefaultConfig(bytebufferpool.Fast))
buffer := pool.Buffer(64)
_, _ = buffer.WriteString("hello")
_ = buffer.WriteByte(' ')
_, _ = buffer.Write([]byte("world"))
fmt.Println(string(buffer.Bytes()))
fmt.Println(buffer.Release())
// Output:
// hello world
// Retained
}
func ExamplePool_bounded() {
config := bytebufferpool.DefaultConfig(bytebufferpool.Bounded)
config.Classes = []int{64}
config.MaxPooledCapacity = 64
config.MaxRetainedCapacity = 64
pool, _ := bytebufferpool.New(config)
first := pool.Acquire(64)
second := pool.Acquire(64)
fmt.Println(first.Release())
fmt.Println(second.Release())
stats := pool.Stats()
fmt.Println(stats.RetainedStorageCount, stats.RetainedCapacity)
// Output:
// Retained
// DroppedFull
// 1 64
}
func ExamplePool_unpooled() {
config := bytebufferpool.DefaultConfig(bytebufferpool.Fast)
config.Classes = []int{64}
config.MaxPooledCapacity = 128
pool, _ := bytebufferpool.New(config)
gap := pool.Acquire(100)
oversize := pool.Acquire(129)
fmt.Println(gap.Release())
fmt.Println(oversize.Release())
// Output:
// DroppedUnpooled
// DroppedOversize
}