github.com/thanos-io/thanos@v0.32.5/pkg/extgrpc/snappy/snappy_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package snappy 5 6 import ( 7 "bytes" 8 "io" 9 "strings" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestSnappy(t *testing.T) { 17 c := newCompressor() 18 assert.Equal(t, "snappy", c.Name()) 19 20 tests := []struct { 21 test string 22 input string 23 }{ 24 {"empty", ""}, 25 {"short", "hello world"}, 26 {"long", strings.Repeat("123456789", 1024)}, 27 } 28 for _, test := range tests { 29 t.Run(test.test, func(t *testing.T) { 30 var buf bytes.Buffer 31 // Compress 32 w, err := c.Compress(&buf) 33 require.NoError(t, err) 34 n, err := w.Write([]byte(test.input)) 35 require.NoError(t, err) 36 assert.Len(t, test.input, n) 37 err = w.Close() 38 require.NoError(t, err) 39 // Decompress 40 r, err := c.Decompress(&buf) 41 require.NoError(t, err) 42 out, err := io.ReadAll(r) 43 require.NoError(t, err) 44 assert.Equal(t, test.input, string(out)) 45 }) 46 } 47 } 48 49 func BenchmarkSnappyCompress(b *testing.B) { 50 data := []byte(strings.Repeat("123456789", 1024)) 51 c := newCompressor() 52 b.ResetTimer() 53 for i := 0; i < b.N; i++ { 54 w, _ := c.Compress(io.Discard) 55 _, _ = w.Write(data) 56 _ = w.Close() 57 } 58 } 59 60 func BenchmarkSnappyDecompress(b *testing.B) { 61 data := []byte(strings.Repeat("123456789", 1024)) 62 c := newCompressor() 63 var buf bytes.Buffer 64 w, _ := c.Compress(&buf) 65 _, _ = w.Write(data) 66 reader := bytes.NewReader(buf.Bytes()) 67 b.ResetTimer() 68 for i := 0; i < b.N; i++ { 69 r, _ := c.Decompress(reader) 70 _, _ = io.ReadAll(r) 71 _, _ = reader.Seek(0, io.SeekStart) 72 } 73 }