| 1 |
package base32 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/dromara/dongle/internal/mock" |
| 8 |
) |
| 9 |
|
| 10 |
// BenchmarkStdEncoder_Encode benchmarks the standard base32 encoder with small data |
| 11 |
func BenchmarkStdEncoder_Encode(b *testing.B) { |
| 12 |
data := []byte("Hello, World! This is a test string for base32 encoding benchmark.") |
| 13 |
encoder := NewStdEncoder(StdAlphabet) |
| 14 |
|
| 15 |
b.ResetTimer() |
| 16 |
for i := 0; i < b.N; i++ { |
| 17 |
encoder.Encode(data) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
// BenchmarkStdEncoder_EncodeLarge benchmarks the standard base32 encoder with large data |
| 22 |
func BenchmarkStdEncoder_EncodeLarge(b *testing.B) { |
| 23 |
// Create a larger data set for testing |
| 24 |
data := bytes.Repeat([]byte("Hello, World! "), 1000) // ~15KB |
| 25 |
encoder := NewStdEncoder(StdAlphabet) |
| 26 |
|
| 27 |
b.ResetTimer() |
| 28 |
for i := 0; i < b.N; i++ { |
| 29 |
encoder.Encode(data) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// BenchmarkStdEncoder_EncodeBinary benchmarks the standard base32 encoder with binary data |
| 34 |
func BenchmarkStdEncoder_EncodeBinary(b *testing.B) { |
| 35 |
// Create binary data for testing |
| 36 |
data := make([]byte, 1024) |
| 37 |
for i := range data { |
| 38 |
data[i] = byte(i % 256) |
| 39 |
} |
| 40 |
encoder := NewStdEncoder(StdAlphabet) |
| 41 |
|
| 42 |
b.ResetTimer() |
| 43 |
for i := 0; i < b.N; i++ { |
| 44 |
encoder.Encode(data) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// BenchmarkStdEncoder_EncodeEmpty benchmarks the standard base32 encoder with empty data |
| 49 |
func BenchmarkStdEncoder_EncodeEmpty(b *testing.B) { |
| 50 |
var data []byte |
| 51 |
encoder := NewStdEncoder(StdAlphabet) |
| 52 |
|
| 53 |
b.ResetTimer() |
| 54 |
for i := 0; i < b.N; i++ { |
| 55 |
encoder.Encode(data) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// BenchmarkStdEncoder_EncodeSingleByte benchmarks the standard base32 encoder with single byte |
| 60 |
func BenchmarkStdEncoder_EncodeSingleByte(b *testing.B) { |
| 61 |
data := []byte{0x41} // 'A' |
| 62 |
encoder := NewStdEncoder(StdAlphabet) |
| 63 |
|
| 64 |
b.ResetTimer() |
| 65 |
for i := 0; i < b.N; i++ { |
| 66 |
encoder.Encode(data) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// BenchmarkStdEncoder_EncodeBlockSize benchmarks the standard base32 encoder with block size data |
| 71 |
func BenchmarkStdEncoder_EncodeBlockSize(b *testing.B) { |
| 72 |
// Base32 processes data in 5-byte blocks |
| 73 |
data := make([]byte, 5) |
| 74 |
for i := range data { |
| 75 |
data[i] = byte(i) |
| 76 |
} |
| 77 |
encoder := NewStdEncoder(StdAlphabet) |
| 78 |
|
| 79 |
b.ResetTimer() |
| 80 |
for i := 0; i < b.N; i++ { |
| 81 |
encoder.Encode(data) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// BenchmarkStdEncoder_EncodeHexAlphabet benchmarks the standard base32 encoder with hex alphabet |
| 86 |
func BenchmarkStdEncoder_EncodeHexAlphabet(b *testing.B) { |
| 87 |
data := []byte("Hello, World! This is a test string for base32 hex encoding benchmark.") |
| 88 |
encoder := NewStdEncoder(HexAlphabet) |
| 89 |
|
| 90 |
b.ResetTimer() |
| 91 |
for i := 0; i < b.N; i++ { |
| 92 |
encoder.Encode(data) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// BenchmarkStdDecoder_Decode benchmarks the standard base32 decoder with small data |
| 97 |
func BenchmarkStdDecoder_Decode(b *testing.B) { |
| 98 |
// Create encoded data for testing |
| 99 |
encoder := NewStdEncoder(StdAlphabet) |
| 100 |
original := []byte("Hello, World! This is a test string for base32 decoding benchmark.") |
| 101 |
encoded := encoder.Encode(original) |
| 102 |
decoder := NewStdDecoder(StdAlphabet) |
| 103 |
|
| 104 |
b.ResetTimer() |
| 105 |
for i := 0; i < b.N; i++ { |
| 106 |
decoder.Decode(encoded) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// BenchmarkStdDecoder_DecodeLarge benchmarks the standard base32 decoder with large data |
| 111 |
func BenchmarkStdDecoder_DecodeLarge(b *testing.B) { |
| 112 |
// Create large encoded data for testing |
| 113 |
encoder := NewStdEncoder(StdAlphabet) |
| 114 |
original := bytes.Repeat([]byte("Hello, World! "), 1000) // ~15KB |
| 115 |
encoded := encoder.Encode(original) |
| 116 |
decoder := NewStdDecoder(StdAlphabet) |
| 117 |
|
| 118 |
b.ResetTimer() |
| 119 |
for i := 0; i < b.N; i++ { |
| 120 |
decoder.Decode(encoded) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// BenchmarkStdDecoder_DecodeBinary benchmarks the standard base32 decoder with binary data |
| 125 |
func BenchmarkStdDecoder_DecodeBinary(b *testing.B) { |
| 126 |
// Create binary encoded data for testing |
| 127 |
encoder := NewStdEncoder(StdAlphabet) |
| 128 |
original := make([]byte, 1024) |
| 129 |
for i := range original { |
| 130 |
original[i] = byte(i % 256) |
| 131 |
} |
| 132 |
encoded := encoder.Encode(original) |
| 133 |
decoder := NewStdDecoder(StdAlphabet) |
| 134 |
|
| 135 |
b.ResetTimer() |
| 136 |
for i := 0; i < b.N; i++ { |
| 137 |
decoder.Decode(encoded) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// BenchmarkStdDecoder_DecodeEmpty benchmarks the standard base32 decoder with empty data |
| 142 |
func BenchmarkStdDecoder_DecodeEmpty(b *testing.B) { |
| 143 |
var data []byte |
| 144 |
decoder := NewStdDecoder(StdAlphabet) |
| 145 |
|
| 146 |
b.ResetTimer() |
| 147 |
for i := 0; i < b.N; i++ { |
| 148 |
decoder.Decode(data) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// BenchmarkStdDecoder_DecodeHexAlphabet benchmarks the standard base32 decoder with hex alphabet |
| 153 |
func BenchmarkStdDecoder_DecodeHexAlphabet(b *testing.B) { |
| 154 |
// Create encoded data for testing with hex alphabet |
| 155 |
encoder := NewStdEncoder(HexAlphabet) |
| 156 |
original := []byte("Hello, World! This is a test string for base32 hex decoding benchmark.") |
| 157 |
encoded := encoder.Encode(original) |
| 158 |
decoder := NewStdDecoder(HexAlphabet) |
| 159 |
|
| 160 |
b.ResetTimer() |
| 161 |
for i := 0; i < b.N; i++ { |
| 162 |
decoder.Decode(encoded) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// BenchmarkStreamEncoder_Write benchmarks the streaming base32 encoder |
| 167 |
func BenchmarkStreamEncoder_Write(b *testing.B) { |
| 168 |
data := []byte("Hello, World! This is a test string for streaming base32 encoding benchmark.") |
| 169 |
var buf bytes.Buffer |
| 170 |
encoder := NewStreamEncoder(&buf, StdAlphabet) |
| 171 |
|
| 172 |
b.ResetTimer() |
| 173 |
for i := 0; i < b.N; i++ { |
| 174 |
buf.Reset() |
| 175 |
encoder.Write(data) |
| 176 |
encoder.Close() |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// BenchmarkStreamEncoder_WriteLarge benchmarks the streaming base32 encoder with large data |
| 181 |
func BenchmarkStreamEncoder_WriteLarge(b *testing.B) { |
| 182 |
data := bytes.Repeat([]byte("Hello, World! "), 1000) // ~15KB |
| 183 |
var buf bytes.Buffer |
| 184 |
encoder := NewStreamEncoder(&buf, StdAlphabet) |
| 185 |
|
| 186 |
b.ResetTimer() |
| 187 |
for i := 0; i < b.N; i++ { |
| 188 |
buf.Reset() |
| 189 |
encoder.Write(data) |
| 190 |
encoder.Close() |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// BenchmarkStreamEncoder_WriteChunked benchmarks the streaming base32 encoder with chunked writes |
| 195 |
func BenchmarkStreamEncoder_WriteChunked(b *testing.B) { |
| 196 |
chunks := [][]byte{ |
| 197 |
[]byte("Hello, "), |
| 198 |
[]byte("World! "), |
| 199 |
[]byte("This is a "), |
| 200 |
[]byte("test string "), |
| 201 |
[]byte("for streaming "), |
| 202 |
[]byte("base32 encoding "), |
| 203 |
[]byte("benchmark."), |
| 204 |
} |
| 205 |
var buf bytes.Buffer |
| 206 |
encoder := NewStreamEncoder(&buf, StdAlphabet) |
| 207 |
|
| 208 |
b.ResetTimer() |
| 209 |
for i := 0; i < b.N; i++ { |
| 210 |
buf.Reset() |
| 211 |
for _, chunk := range chunks { |
| 212 |
encoder.Write(chunk) |
| 213 |
} |
| 214 |
encoder.Close() |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// BenchmarkStreamDecoder_Read benchmarks the streaming base32 decoder |
| 219 |
func BenchmarkStreamDecoder_Read(b *testing.B) { |
| 220 |
// Create encoded data for testing |
| 221 |
encoder := NewStdEncoder(StdAlphabet) |
| 222 |
original := []byte("Hello, World! This is a test string for streaming base32 decoding benchmark.") |
| 223 |
encoded := encoder.Encode(original) |
| 224 |
|
| 225 |
// Create a reader from the encoded data |
| 226 |
reader := mock.NewFile(encoded, "test.bin") |
| 227 |
defer reader.Close() |
| 228 |
decoder := NewStreamDecoder(reader, StdAlphabet) |
| 229 |
|
| 230 |
// Buffer to read into |
| 231 |
buf := make([]byte, 1024) |
| 232 |
|
| 233 |
b.ResetTimer() |
| 234 |
for i := 0; i < b.N; i++ { |
| 235 |
reader.Seek(0, 0) // Reset reader position |
| 236 |
decoder.Read(buf) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
// BenchmarkStreamDecoder_ReadLarge benchmarks the streaming base32 decoder with large data |
| 241 |
func BenchmarkStreamDecoder_ReadLarge(b *testing.B) { |
| 242 |
// Create large encoded data for testing |
| 243 |
encoder := NewStdEncoder(StdAlphabet) |
| 244 |
original := bytes.Repeat([]byte("Hello, World! "), 1000) // ~15KB |
| 245 |
encoded := encoder.Encode(original) |
| 246 |
|
| 247 |
// Create a reader from the encoded data |
| 248 |
reader := mock.NewFile(encoded, "test.bin") |
| 249 |
defer reader.Close() |
| 250 |
decoder := NewStreamDecoder(reader, StdAlphabet) |
| 251 |
|
| 252 |
// Buffer to read into |
| 253 |
buf := make([]byte, 1024) |
| 254 |
|
| 255 |
b.ResetTimer() |
| 256 |
for i := 0; i < b.N; i++ { |
| 257 |
reader.Seek(0, 0) // Reset reader position |
| 258 |
decoder.Read(buf) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// BenchmarkStdEncoder_EncodeWithError benchmarks the standard base32 encoder with invalid alphabet |
| 263 |
func BenchmarkStdEncoder_EncodeWithError(b *testing.B) { |
| 264 |
data := []byte("Hello, World!") |
| 265 |
encoder := NewStdEncoder("invalid") // This will cause an error |
| 266 |
|
| 267 |
b.ResetTimer() |
| 268 |
for i := 0; i < b.N; i++ { |
| 269 |
encoder.Encode(data) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
// BenchmarkStdDecoder_DecodeWithError benchmarks the standard base32 decoder with invalid data |
| 274 |
func BenchmarkStdDecoder_DecodeWithError(b *testing.B) { |
| 275 |
// Create invalid base32 data |
| 276 |
invalidData := []byte("INVALID_BASE32_DATA!!!") |
| 277 |
decoder := NewStdDecoder(StdAlphabet) |
| 278 |
|
| 279 |
b.ResetTimer() |
| 280 |
for i := 0; i < b.N; i++ { |
| 281 |
decoder.Decode(invalidData) |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
// BenchmarkStdEncoder_EncodeUnicode benchmarks the standard base32 encoder with Unicode data |
| 286 |
func BenchmarkStdEncoder_EncodeUnicode(b *testing.B) { |
| 287 |
data := []byte("你好世界,这是一个包含中文的测试字符串") |
| 288 |
encoder := NewStdEncoder(StdAlphabet) |
| 289 |
|
| 290 |
b.ResetTimer() |
| 291 |
for i := 0; i < b.N; i++ { |
| 292 |
encoder.Encode(data) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
// BenchmarkStdDecoder_DecodeUnicode benchmarks the standard base32 decoder with unicode data |
| 297 |
func BenchmarkStdDecoder_DecodeUnicode(b *testing.B) { |
| 298 |
// Create encoded unicode data for testing |
| 299 |
encoder := NewStdEncoder(StdAlphabet) |
| 300 |
original := []byte("你好世界,这是一个包含中文的测试字符串") |
| 301 |
encoded := encoder.Encode(original) |
| 302 |
decoder := NewStdDecoder(StdAlphabet) |
| 303 |
|
| 304 |
b.ResetTimer() |
| 305 |
for i := 0; i < b.N; i++ { |
| 306 |
decoder.Decode(encoded) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// BenchmarkStdEncoder_EncodeLeadingZeros benchmarks the standard base32 encoder with leading zeros |
| 311 |
func BenchmarkStdEncoder_EncodeLeadingZeros(b *testing.B) { |
| 312 |
data := []byte{0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05} |
| 313 |
encoder := NewStdEncoder(StdAlphabet) |
| 314 |
|
| 315 |
b.ResetTimer() |
| 316 |
for i := 0; i < b.N; i++ { |
| 317 |
encoder.Encode(data) |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// BenchmarkStdEncoder_EncodeMixedData benchmarks the standard base32 encoder with mixed data types |
| 322 |
func BenchmarkStdEncoder_EncodeMixedData(b *testing.B) { |
| 323 |
data := []byte{ |
| 324 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 325 |
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 326 |
} |
| 327 |
encoder := NewStdEncoder(StdAlphabet) |
| 328 |
|
| 329 |
b.ResetTimer() |
| 330 |
for i := 0; i < b.N; i++ { |
| 331 |
encoder.Encode(data) |
| 332 |
} |
| 333 |
} |