|
1 | | -package gocql |
| 1 | +package gocql_test |
2 | 2 |
|
3 | 3 | import ( |
4 | 4 | "bytes" |
| 5 | + "os" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | "github.com/golang/snappy" |
| 9 | + |
| 10 | + "github.com/gocql/gocql" |
8 | 11 | ) |
9 | 12 |
|
| 13 | +type frameExample struct { |
| 14 | + Name string |
| 15 | + Frame []byte |
| 16 | + FilePath string |
| 17 | +} |
| 18 | + |
| 19 | +var frameExamples = struct { |
| 20 | + Requests []frameExample |
| 21 | + Responses []frameExample |
| 22 | +}{ |
| 23 | + Requests: []frameExample{ |
| 24 | + { |
| 25 | + Name: "Small query request", |
| 26 | + FilePath: "testdata/frames/small_query_request.bin", |
| 27 | + }, |
| 28 | + { |
| 29 | + Name: "Medium query request", |
| 30 | + FilePath: "testdata/frames/medium_query_request.bin", |
| 31 | + }, |
| 32 | + { |
| 33 | + Name: "Big query request", |
| 34 | + FilePath: "testdata/frames/big_query_request.bin", |
| 35 | + }, |
| 36 | + { |
| 37 | + Name: "Prepare statement request", |
| 38 | + FilePath: "testdata/frames/prepare_statement_request.bin", |
| 39 | + }, |
| 40 | + }, |
| 41 | + Responses: []frameExample{ |
| 42 | + { |
| 43 | + Name: "Small query response", |
| 44 | + FilePath: "testdata/frames/small_query_response.bin", |
| 45 | + }, |
| 46 | + { |
| 47 | + Name: "Medium query response", |
| 48 | + FilePath: "testdata/frames/medium_query_response.bin", |
| 49 | + }, |
| 50 | + { |
| 51 | + Name: "Big query response", |
| 52 | + FilePath: "testdata/frames/big_query_response.bin", |
| 53 | + }, |
| 54 | + { |
| 55 | + Name: "Prepare statement response", |
| 56 | + FilePath: "testdata/frames/prepare_statement_response.bin", |
| 57 | + }, |
| 58 | + }, |
| 59 | +} |
| 60 | + |
10 | 61 | func TestSnappyCompressor(t *testing.T) { |
11 | | - c := SnappyCompressor{} |
12 | | - if c.Name() != "snappy" { |
13 | | - t.Fatalf("expected name to be 'snappy', got %v", c.Name()) |
14 | | - } |
| 62 | + t.Run("basic", func(t *testing.T) { |
| 63 | + c := gocql.SnappyCompressor{} |
| 64 | + if c.Name() != "snappy" { |
| 65 | + t.Fatalf("expected name to be 'snappy', got %v", c.Name()) |
| 66 | + } |
15 | 67 |
|
16 | | - str := "My Test String" |
17 | | - //Test Encoding |
18 | | - expected := snappy.Encode(nil, []byte(str)) |
19 | | - if res, err := c.Encode([]byte(str)); err != nil { |
20 | | - t.Fatalf("failed to encode '%v' with error %v", str, err) |
21 | | - } else if bytes.Compare(expected, res) != 0 { |
22 | | - t.Fatal("failed to match the expected encoded value with the result encoded value.") |
23 | | - } |
| 68 | + str := "My Test String" |
| 69 | + //Test Encoding |
| 70 | + expected := snappy.Encode(nil, []byte(str)) |
| 71 | + if res, err := c.Encode([]byte(str)); err != nil { |
| 72 | + t.Fatalf("failed to encode '%v' with error %v", str, err) |
| 73 | + } else if bytes.Compare(expected, res) != 0 { |
| 74 | + t.Fatal("failed to match the expected encoded value with the result encoded value.") |
| 75 | + } |
24 | 76 |
|
25 | | - val, err := c.Encode([]byte(str)) |
26 | | - if err != nil { |
27 | | - t.Fatalf("failed to encode '%v' with error '%v'", str, err) |
28 | | - } |
| 77 | + val, err := c.Encode([]byte(str)) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("failed to encode '%v' with error '%v'", str, err) |
| 80 | + } |
| 81 | + |
| 82 | + //Test Decoding |
| 83 | + if expected, err := snappy.Decode(nil, val); err != nil { |
| 84 | + t.Fatalf("failed to decode '%v' with error %v", val, err) |
| 85 | + } else if res, err := c.Decode(val); err != nil { |
| 86 | + t.Fatalf("failed to decode '%v' with error %v", val, err) |
| 87 | + } else if bytes.Compare(expected, res) != 0 { |
| 88 | + t.Fatal("failed to match the expected decoded value with the result decoded value.") |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("frame-examples", func(t *testing.T) { |
| 93 | + c := gocql.SnappyCompressor{} |
29 | 94 |
|
30 | | - //Test Decoding |
31 | | - if expected, err := snappy.Decode(nil, val); err != nil { |
32 | | - t.Fatalf("failed to decode '%v' with error %v", val, err) |
33 | | - } else if res, err := c.Decode(val); err != nil { |
34 | | - t.Fatalf("failed to decode '%v' with error %v", val, err) |
35 | | - } else if bytes.Compare(expected, res) != 0 { |
36 | | - t.Fatal("failed to match the expected decoded value with the result decoded value.") |
| 95 | + t.Run("Encode", func(t *testing.T) { |
| 96 | + for _, frame := range frameExamples.Requests { |
| 97 | + t.Run(frame.Name, func(t *testing.T) { |
| 98 | + encoded, err := c.Encode(frame.Frame) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("failed to encode frame %s", frame.Name) |
| 101 | + } |
| 102 | + decoded, err := c.Decode(encoded) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("failed to decode frame %s", frame.Name) |
| 105 | + } |
| 106 | + |
| 107 | + if bytes.Compare(decoded, frame.Frame) != 0 { |
| 108 | + t.Fatalf("failed to match the decoded value with the original value") |
| 109 | + } |
| 110 | + t.Logf("Compression rate %f", float64(len(encoded))/float64(len(frame.Frame))) |
| 111 | + }) |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + t.Run("Decode", func(t *testing.T) { |
| 116 | + for _, frame := range frameExamples.Responses { |
| 117 | + t.Run(frame.Name, func(t *testing.T) { |
| 118 | + decoded, err := c.Decode(frame.Frame) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("failed to decode frame %s", frame.Name) |
| 121 | + } |
| 122 | + |
| 123 | + if len(decoded) == 0 { |
| 124 | + t.Fatalf("frame was decoded to empty slice") |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | + }) |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +func BenchmarkSnappyCompressor(b *testing.B) { |
| 133 | + c := gocql.SnappyCompressor{} |
| 134 | + b.Run("Decode", func(b *testing.B) { |
| 135 | + for _, frame := range frameExamples.Responses { |
| 136 | + b.Run(frame.Name, func(b *testing.B) { |
| 137 | + for x := 0; x < b.N; x++ { |
| 138 | + _, _ = c.Decode(frame.Frame) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | + }) |
| 143 | + |
| 144 | + b.Run("Encode", func(b *testing.B) { |
| 145 | + for _, frame := range frameExamples.Requests { |
| 146 | + b.Run(frame.Name, func(b *testing.B) { |
| 147 | + for x := 0; x < b.N; x++ { |
| 148 | + _, _ = c.Encode(frame.Frame) |
| 149 | + } |
| 150 | + }) |
| 151 | + } |
| 152 | + }) |
| 153 | +} |
| 154 | + |
| 155 | +func init() { |
| 156 | + var err error |
| 157 | + for id, def := range frameExamples.Requests { |
| 158 | + frameExamples.Requests[id].Frame, err = os.ReadFile(def.FilePath) |
| 159 | + if err != nil { |
| 160 | + panic("can't read file " + def.FilePath) |
| 161 | + } |
| 162 | + } |
| 163 | + for id, def := range frameExamples.Responses { |
| 164 | + frameExamples.Responses[id].Frame, err = os.ReadFile(def.FilePath) |
| 165 | + if err != nil { |
| 166 | + panic("can't read file " + def.FilePath) |
| 167 | + } |
37 | 168 | } |
38 | 169 | } |
0 commit comments