|
| 1 | +package grpcencoding |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/cortexproject/cortex/pkg/util/grpcencoding/snappy" |
| 11 | + "github.com/cortexproject/cortex/pkg/util/grpcencoding/snappyblock" |
| 12 | + "github.com/cortexproject/cortex/pkg/util/grpcencoding/zstd" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "google.golang.org/grpc/encoding" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCompressors(t *testing.T) { |
| 20 | + testCases := []struct { |
| 21 | + name string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: snappy.Name, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: snappyblock.Name, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: zstd.Name, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + for _, tc := range testCases { |
| 35 | + t.Run(tc.name, func(t *testing.T) { |
| 36 | + testCompress(tc.name, t) |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func testCompress(name string, t *testing.T) { |
| 42 | + c := encoding.GetCompressor(name) |
| 43 | + assert.Equal(t, name, c.Name()) |
| 44 | + |
| 45 | + tests := []struct { |
| 46 | + test string |
| 47 | + input string |
| 48 | + }{ |
| 49 | + {"empty", ""}, |
| 50 | + {"short", "hello world"}, |
| 51 | + {"long", strings.Repeat("123456789", 2024)}, |
| 52 | + } |
| 53 | + for _, test := range tests { |
| 54 | + t.Run(test.test, func(t *testing.T) { |
| 55 | + buf := &bytes.Buffer{} |
| 56 | + // Compress |
| 57 | + w, err := c.Compress(buf) |
| 58 | + require.NoError(t, err) |
| 59 | + n, err := w.Write([]byte(test.input)) |
| 60 | + require.NoError(t, err) |
| 61 | + assert.Len(t, test.input, n) |
| 62 | + err = w.Close() |
| 63 | + require.NoError(t, err) |
| 64 | + compressedBytes := buf.Bytes() |
| 65 | + buf = bytes.NewBuffer(compressedBytes) |
| 66 | + |
| 67 | + // Decompress |
| 68 | + r, err := c.Decompress(buf) |
| 69 | + require.NoError(t, err) |
| 70 | + out, err := io.ReadAll(r) |
| 71 | + require.NoError(t, err) |
| 72 | + assert.Equal(t, test.input, string(out)) |
| 73 | + |
| 74 | + if sizer, ok := c.(interface { |
| 75 | + DecompressedSize(compressedBytes []byte) int |
| 76 | + }); ok { |
| 77 | + buf = bytes.NewBuffer(compressedBytes) |
| 78 | + r, err := c.Decompress(buf) |
| 79 | + require.NoError(t, err) |
| 80 | + out, err := io.ReadAll(r) |
| 81 | + require.NoError(t, err) |
| 82 | + assert.Equal(t, len(out), sizer.DecompressedSize(compressedBytes)) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func BenchmarkCompress(b *testing.B) { |
| 89 | + data := []byte(strings.Repeat("123456789", 1024)) |
| 90 | + |
| 91 | + testCases := []struct { |
| 92 | + name string |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: snappy.Name, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: snappyblock.Name, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: zstd.Name, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tc := range testCases { |
| 106 | + b.Run(tc.name, func(b *testing.B) { |
| 107 | + c := encoding.GetCompressor("snappy") |
| 108 | + b.ResetTimer() |
| 109 | + for i := 0; i < b.N; i++ { |
| 110 | + w, _ := c.Compress(io.Discard) |
| 111 | + _, _ = w.Write(data) |
| 112 | + _ = w.Close() |
| 113 | + } |
| 114 | + b.ReportAllocs() |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func BenchmarkDecompress(b *testing.B) { |
| 120 | + data := []byte(strings.Repeat("123456789", 1024)) |
| 121 | + |
| 122 | + testCases := []struct { |
| 123 | + name string |
| 124 | + }{ |
| 125 | + { |
| 126 | + name: snappy.Name, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: snappyblock.Name, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: zstd.Name, |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + for _, tc := range testCases { |
| 137 | + b.Run(tc.name, func(b *testing.B) { |
| 138 | + c := encoding.GetCompressor(tc.name) |
| 139 | + var buf bytes.Buffer |
| 140 | + w, _ := c.Compress(&buf) |
| 141 | + _, _ = w.Write(data) |
| 142 | + w.Close() |
| 143 | + b.ResetTimer() |
| 144 | + for i := 0; i < b.N; i++ { |
| 145 | + _, _, err := decompress(c, buf.Bytes(), 10000) |
| 146 | + require.NoError(b, err) |
| 147 | + } |
| 148 | + b.ReportAllocs() |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// This function was copied from: https://github.com/grpc/grpc-go/blob/70c52915099a3b30848d0cb22e2f8951dd5aed7f/rpc_util.go#L765 |
| 154 | +func decompress(compressor encoding.Compressor, d []byte, maxReceiveMessageSize int) ([]byte, int, error) { |
| 155 | + dcReader, err := compressor.Decompress(bytes.NewReader(d)) |
| 156 | + if err != nil { |
| 157 | + return nil, 0, err |
| 158 | + } |
| 159 | + if sizer, ok := compressor.(interface { |
| 160 | + DecompressedSize(compressedBytes []byte) int |
| 161 | + }); ok { |
| 162 | + if size := sizer.DecompressedSize(d); size >= 0 { |
| 163 | + if size > maxReceiveMessageSize { |
| 164 | + return nil, size, nil |
| 165 | + } |
| 166 | + // size is used as an estimate to size the buffer, but we |
| 167 | + // will read more data if available. |
| 168 | + // +MinRead so ReadFrom will not reallocate if size is correct. |
| 169 | + buf := bytes.NewBuffer(make([]byte, 0, size+bytes.MinRead)) |
| 170 | + bytesRead, err := buf.ReadFrom(io.LimitReader(dcReader, int64(maxReceiveMessageSize)+1)) |
| 171 | + return buf.Bytes(), int(bytesRead), err |
| 172 | + } |
| 173 | + } |
| 174 | + // Read from LimitReader with limit max+1. So if the underlying |
| 175 | + // reader is over limit, the result will be bigger than max. |
| 176 | + d, err = ioutil.ReadAll(io.LimitReader(dcReader, int64(maxReceiveMessageSize)+1)) |
| 177 | + return d, len(d), err |
| 178 | +} |
0 commit comments