-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_example_test.go
More file actions
188 lines (161 loc) · 5.5 KB
/
Copy pathpool_example_test.go
File metadata and controls
188 lines (161 loc) · 5.5 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
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
package bytepool
import (
"encoding/json"
"fmt"
)
// Example_recentLengthsTracking demonstrates the recent 256 get operation length tracking feature
func Example_recentLengthsTracking() {
// create a BytePool
pool := NewPools([]int{128, 256, 512, 1024})
// simulate some get operations
lengths := []int{100, 200, 50, 300, 150, 400, 80}
fmt.Println("Simulating get operations:")
for i, length := range lengths {
buf := pool.Get(length)
fmt.Printf("Get(%d) -> allocated %d bytes\n", length, len(buf))
pool.Put(buf)
// check statistics every few operations
if i == 2 || i == len(lengths)-1 {
stats := pool.GetPoolStats()
recentLengths := stats["recent_lengths"].([]int)
totalGet := stats["total_get"].(int64)
totalPut := stats["total_put"].(int64)
fmt.Printf("Current recorded lengths: %v (total get:%d, total put:%d)\n",
recentLengths, totalGet, totalPut)
}
}
// final statistics
stats := pool.GetPoolStats()
recentLengths := stats["recent_lengths"].([]int)
totalGet := stats["total_get"].(int64)
totalPut := stats["total_put"].(int64)
fmt.Printf("\nFinal statistics:\n")
fmt.Printf("Recorded operations: %d\n", len(recentLengths))
fmt.Printf("Total get operations: %d\n", totalGet)
fmt.Printf("Total put operations: %d\n", totalPut)
fmt.Printf("Recent lengths: %v\n", recentLengths)
// Output:
// Simulating get operations:
// Get(100) -> allocated 100 bytes
// Get(200) -> allocated 200 bytes
// Get(50) -> allocated 50 bytes
// Current recorded lengths: [100 200 50] (total get:3, total put:3)
// Get(300) -> allocated 300 bytes
// Get(150) -> allocated 150 bytes
// Get(400) -> allocated 400 bytes
// Get(80) -> allocated 80 bytes
// Current recorded lengths: [100 200 50 300 150 400 80] (total get:7, total put:7)
//
// Final statistics:
// Recorded operations: 7
// Total get operations: 7
// Total put operations: 7
// Recent lengths: [100 200 50 300 150 400 80]
}
// Example_recentLengthsOverflow demonstrates ring buffer overflow when exceeding 256 operations
func Example_recentLengthsOverflow() {
pool := NewPools([]int{128, 256, 512, 1024})
// simulate many operations (more than 256)
fmt.Println("Executing 260 get operations...")
for i := 1; i <= 260; i++ {
pool.Get(i)
}
stats := pool.GetPoolStats()
recentLengths := stats["recent_lengths"].([]int)
totalGet := stats["total_get"].(int64)
fmt.Printf("Total operations: 260\n")
fmt.Printf("Recorded operations: %d (max 256)\n", len(recentLengths))
fmt.Printf("Total get statistics: %d\n", totalGet)
fmt.Printf("Earliest recorded length: %d (should be 5th operation)\n", recentLengths[0])
fmt.Printf("Latest recorded length: %d (should be 260th operation)\n", recentLengths[len(recentLengths)-1])
// Output:
// Executing 260 get operations...
// Total operations: 260
// Recorded operations: 256 (max 256)
// Total get statistics: 260
// Earliest recorded length: 5 (should be 5th operation)
// Latest recorded length: 260 (should be 260th operation)
}
// Example_poolStatsWithRecentLengths demonstrates complete statistics information
func Example_poolStatsWithRecentLengths() {
pool := NewPools([]int{128, 256, 512})
// perform some operations
for i := 0; i < 10; i++ {
buf := pool.Get(100 + i*10)
pool.Put(buf)
}
// get complete statistics
stats := pool.GetPoolStats()
// formatted output
jsonData, _ := json.MarshalIndent(stats, "", " ")
fmt.Printf("BytePool statistics:\n%s\n", jsonData)
// show recent length statistics separately
recentLengths := stats["recent_lengths"].([]int)
fmt.Printf("\nRecent request length analysis:\n")
fmt.Printf("Total requests: %d\n", len(recentLengths))
if len(recentLengths) > 0 {
min, max := recentLengths[0], recentLengths[0]
sum := 0
for _, length := range recentLengths {
if length < min {
min = length
}
if length > max {
max = length
}
sum += length
}
avg := float64(sum) / float64(len(recentLengths))
fmt.Printf("Min length: %d\n", min)
fmt.Printf("Max length: %d\n", max)
fmt.Printf("Average length: %.1f\n", avg)
}
}
// Example_bufferBalance demonstrates GetBuffer and ReleaseBuffer statistics
func Example_bufferBalance() {
pool := NewPools([]int{128, 256, 512})
fmt.Println("Creating 5 Buffers:")
buffers := make([]*Buffer, 5)
for i := 0; i < 5; i++ {
buffers[i] = pool.GetBuffer(100)
fmt.Printf("Created Buffer %d\n", i+1)
}
stats := pool.GetPoolStats()
fmt.Printf("Statistics after creation: get=%d, put=%d\n",
stats["total_get"].(int64), stats["total_put"].(int64))
fmt.Println("\nReleasing 3 Buffers:")
for i := 0; i < 3; i++ {
pool.ReleaseBuffer(buffers[i])
fmt.Printf("Released Buffer %d\n", i+1)
}
stats = pool.GetPoolStats()
fmt.Printf("Statistics after partial release: get=%d, put=%d\n",
stats["total_get"].(int64), stats["total_put"].(int64))
fmt.Println("\nReleasing remaining 2 Buffers:")
for i := 3; i < 5; i++ {
pool.ReleaseBuffer(buffers[i])
fmt.Printf("Released Buffer %d\n", i+1)
}
stats = pool.GetPoolStats()
fmt.Printf("Statistics after full release: get=%d, put=%d\n",
stats["total_get"].(int64), stats["total_put"].(int64))
// Output:
// Creating 5 Buffers:
// Created Buffer 1
// Created Buffer 2
// Created Buffer 3
// Created Buffer 4
// Created Buffer 5
// Statistics after creation: get=5, put=0
//
// Releasing 3 Buffers:
// Released Buffer 1
// Released Buffer 2
// Released Buffer 3
// Statistics after partial release: get=5, put=3
//
// Releasing remaining 2 Buffers:
// Released Buffer 4
// Released Buffer 5
// Statistics after full release: get=5, put=5
}