trpc.group/trpc-go/trpc-go@v1.0.3/codec/compress_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package codec_test 15 16 import ( 17 "crypto/rand" 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/require" 22 23 "trpc.group/trpc-go/trpc-go/codec" 24 ) 25 26 // go test -v -coverprofile=cover.out 27 // go tool cover -func=cover.out 28 29 func TestCompress(t *testing.T) { 30 in := []byte("body") 31 32 compress := codec.GetCompressor(0) 33 out1, err := compress.Compress(in) 34 assert.Nil(t, err) 35 assert.Equal(t, out1, in) 36 out1, err = codec.Compress(0, in) 37 assert.Nil(t, err) 38 assert.Equal(t, out1, in) 39 out2, err := compress.Decompress(in) 40 assert.Nil(t, err) 41 assert.Equal(t, out2, in) 42 out2, err = codec.Decompress(0, in) 43 assert.Nil(t, err) 44 assert.Equal(t, out2, in) 45 46 empty := &codec.NoopCompress{} 47 codec.RegisterCompressor(1, empty) 48 49 compress = codec.GetCompressor(1) 50 assert.Equal(t, empty, compress) 51 in = nil 52 out3, err := codec.Compress(0, in) 53 assert.Nil(t, err) 54 assert.Equal(t, out3, in) 55 56 in = nil 57 out4, err := compress.Decompress(in) 58 assert.Nil(t, err) 59 assert.Equal(t, out4, in) 60 out4, err = codec.Decompress(0, in) 61 assert.Nil(t, err) 62 assert.Equal(t, out4, in) 63 t.Run("invalid compress type", func(t *testing.T) { 64 const invalidCompressType = -1 65 66 in = []byte("body") 67 out5, err := codec.Compress(invalidCompressType, in) 68 assert.Nil(t, out5) 69 assert.NotNil(t, err) 70 71 in = []byte("body") 72 out6, err := codec.Decompress(invalidCompressType, in) 73 assert.Nil(t, out6) 74 assert.NotNil(t, err) 75 }) 76 } 77 78 func TestRegisterNegativeCompress(t *testing.T) { 79 const negativeCompressType = -1 80 codec.RegisterCompressor(negativeCompressType, &codec.NoopCompress{}) 81 in := []byte("body") 82 bts, err := codec.Compress(negativeCompressType, in) 83 require.Nil(t, err) 84 out, err := codec.Decompress(negativeCompressType, bts) 85 require.Nil(t, err) 86 require.Equal(t, in, out) 87 } 88 89 func TestGzip(t *testing.T) { 90 91 compress := &codec.GzipCompress{} 92 93 emptyIn := []byte{} 94 95 out1, err := compress.Compress(emptyIn) 96 assert.Nil(t, err) 97 assert.Equal(t, len(out1), 0) 98 99 out2, err := compress.Decompress(emptyIn) 100 assert.Nil(t, err) 101 assert.Equal(t, len(out2), 0) 102 103 in := []byte("A long time ago in a galaxy far, far away...") 104 105 out3, err := compress.Compress(in) 106 assert.Nil(t, err) 107 assert.Equal(t, []byte{0x1f, 0x8b, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x72, 0x54, 108 0xc8, 0xc9, 0xcf, 0x4b, 0x57, 0x28, 0xc9, 0xcc, 0x4d, 0x55, 0x48, 0x4c, 0xcf, 0x57, 109 0xc8, 0xcc, 0x53, 0x48, 0x54, 0x48, 0x4f, 0xcc, 0x49, 0xac, 0xa8, 0x54, 0x48, 0x4b, 110 0x2c, 0xd2, 0x1, 0x11, 0xa, 0x89, 0xe5, 0x89, 0x95, 0x7a, 0x7a, 0x7a, 0x80, 0x0, 0x0, 111 0x0, 0xff, 0xff, 0x10, 0x8a, 0xa3, 0xef, 0x2c, 0x0, 0x0, 0x0}, out3) 112 113 out4, err := compress.Decompress(out3) 114 assert.Nil(t, err) 115 assert.Equal(t, out4, in) 116 117 invalidIn := []byte("hahahahah") 118 _, err = compress.Decompress(invalidIn) 119 assert.NotNil(t, err) 120 } 121 122 func TestZlib(t *testing.T) { 123 124 compress := &codec.ZlibCompress{} 125 126 emptyIn := []byte{} 127 128 out1, err := compress.Compress(emptyIn) 129 assert.Nil(t, err) 130 assert.Equal(t, len(out1), 0) 131 132 out2, err := compress.Decompress(emptyIn) 133 assert.Nil(t, err) 134 assert.Equal(t, len(out2), 0) 135 136 in := []byte("A long time ago in a galaxy far, far away...") 137 138 out3, err := compress.Compress(in) 139 assert.Nil(t, err) 140 141 out4, err := compress.Decompress(out3) 142 assert.Nil(t, err) 143 assert.Equal(t, out4, in) 144 145 invalidIn := []byte("hahahahah") 146 _, err = compress.Decompress(invalidIn) 147 assert.NotNil(t, err) 148 } 149 150 func TestSnappy(t *testing.T) { 151 compress := &codec.SnappyCompress{} 152 testSnappyCompressor(t, compress) 153 } 154 155 func TestSnappyWithPool(t *testing.T) { 156 compress := codec.NewSnappyCompressor() 157 testSnappyCompressor(t, compress) 158 } 159 160 func TestSnappyBlockFormat(t *testing.T) { 161 compress := codec.NewSnappyBlockCompressor() 162 testSnappyBlockCompressor(t, compress) 163 } 164 165 func testSnappyCompressor(t *testing.T, compress *codec.SnappyCompress) { 166 emptyIn := []byte{} 167 168 out1, err := compress.Compress(emptyIn) 169 assert.Nil(t, err) 170 assert.Equal(t, len(out1), 0) 171 172 out2, err := compress.Decompress(emptyIn) 173 assert.Nil(t, err) 174 assert.Equal(t, len(out2), 0) 175 176 in := []byte("A long time ago in a galaxy far, far away...") 177 178 out3, err := compress.Compress(in) 179 assert.Nil(t, err) 180 assert.Equal(t, []byte{0xff, 0x6, 0x0, 0x0, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, 0x1, 181 0x30, 0x0, 0x0, 0xc0, 0xe7, 0x2c, 0x24, 0x41, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 182 0x74, 0x69, 0x6d, 0x65, 0x20, 0x61, 0x67, 0x6f, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 183 0x67, 0x61, 0x6c, 0x61, 0x78, 0x79, 0x20, 0x66, 0x61, 0x72, 0x2c, 0x20, 0x66, 0x61, 184 0x72, 0x20, 0x61, 0x77, 0x61, 0x79, 0x2e, 0x2e, 0x2e}, out3) 185 186 out4, err := compress.Decompress(out3) 187 assert.Nil(t, err) 188 assert.Equal(t, out4, in) 189 190 invalidIn := []byte("hahahahah") 191 _, err = compress.Decompress(invalidIn) 192 assert.NotNil(t, err) 193 } 194 195 func testSnappyBlockCompressor(t *testing.T, compress *codec.SnappyBlockCompressor) { 196 emptyIn := []byte{} 197 198 out1, err := compress.Compress(emptyIn) 199 assert.Nil(t, err) 200 assert.Equal(t, len(out1), 0) 201 202 out2, err := compress.Decompress(emptyIn) 203 assert.Nil(t, err) 204 assert.Equal(t, len(out2), 0) 205 206 in := []byte("A long time ago in a galaxy far, far away...") 207 208 out3, err := compress.Compress(in) 209 assert.Nil(t, err) 210 assert.Equal(t, []byte{0x2c, 0xac, 0x41, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x74, 211 0x69, 0x6d, 0x65, 0x20, 0x61, 0x67, 0x6f, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 212 0x67, 0x61, 0x6c, 0x61, 0x78, 0x79, 0x20, 0x66, 0x61, 0x72, 0x2c, 0x20, 0x66, 213 0x61, 0x72, 0x20, 0x61, 0x77, 0x61, 0x79, 0x2e, 0x2e, 0x2e}, out3) 214 215 out4, err := compress.Decompress(out3) 216 assert.Nil(t, err) 217 assert.Equal(t, out4, in) 218 219 invalidIn := []byte("hahahahah") 220 _, err = compress.Decompress(invalidIn) 221 assert.NotNil(t, err) 222 } 223 224 func BenchmarkGzipCompress_Compress(b *testing.B) { 225 in := make([]byte, 10280) 226 rand.Read(in) 227 compress := &codec.GzipCompress{} 228 for i := 0; i < b.N; i++ { 229 compress.Compress(in) 230 } 231 } 232 233 func BenchmarkGzipCompress_Decompress(b *testing.B) { 234 in := make([]byte, 10280) 235 rand.Read(in) 236 compress := &codec.GzipCompress{} 237 compressBytes, _ := compress.Compress(in) 238 239 for i := 0; i < b.N; i++ { 240 compress.Decompress(compressBytes) 241 } 242 } 243 244 func BenchmarkSnappyBlockCompress_Compress(b *testing.B) { 245 in := make([]byte, 10280) 246 rand.Read(in) 247 compress := &codec.SnappyBlockCompressor{} 248 for i := 0; i < b.N; i++ { 249 compress.Compress(in) 250 } 251 } 252 253 func BenchmarkSnappyBlockCompress_Decompress(b *testing.B) { 254 in := make([]byte, 10280) 255 rand.Read(in) 256 compress := &codec.SnappyBlockCompressor{} 257 compressBytes, _ := compress.Compress(in) 258 259 for i := 0; i < b.N; i++ { 260 compress.Decompress(compressBytes) 261 } 262 } 263 264 func BenchmarkSnappyCompress_Compress_Pool(b *testing.B) { 265 in := make([]byte, 10280) 266 rand.Read(in) 267 compress := codec.NewSnappyCompressor() 268 for i := 0; i < b.N; i++ { 269 compress.Compress(in) 270 } 271 } 272 273 func BenchmarkSnappyCompress_Compress_NoPool(b *testing.B) { 274 in := make([]byte, 10280) 275 rand.Read(in) 276 compress := &codec.SnappyCompress{} 277 for i := 0; i < b.N; i++ { 278 compress.Compress(in) 279 } 280 } 281 282 func BenchmarkSnappyCompress_Decompress_Pool(b *testing.B) { 283 in := make([]byte, 10280) 284 rand.Read(in) 285 compress := codec.NewSnappyCompressor() 286 compressBytes, _ := compress.Compress(in) 287 288 for i := 0; i < b.N; i++ { 289 compress.Decompress(compressBytes) 290 } 291 } 292 293 func BenchmarkSnappyCompress_Decompress_NoPool(b *testing.B) { 294 in := make([]byte, 10280) 295 rand.Read(in) 296 compress := &codec.SnappyCompress{} 297 compressBytes, _ := compress.Compress(in) 298 299 for i := 0; i < b.N; i++ { 300 compress.Decompress(compressBytes) 301 } 302 }