|
1 | 1 | package store |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | "github.com/zeebo/assert" |
8 | 9 | ) |
9 | 10 |
|
| 11 | +// BenchmarkCompression 对比各压缩算法的性能 |
| 12 | +func BenchmarkCompression(b *testing.B) { |
| 13 | + // 使用较大的数据块进行压缩测试 |
| 14 | + largeValue := strings.Repeat("This is a test string that will be compressed. ", 100) |
| 15 | + data := []byte(largeValue) |
| 16 | + |
| 17 | + testCases := []struct { |
| 18 | + name string |
| 19 | + algo CompressionType |
| 20 | + }{ |
| 21 | + {"LZ4", CompressionLZ4}, |
| 22 | + {"Snappy", CompressionSnappy}, |
| 23 | + {"ZSTD", CompressionZSTD}, |
| 24 | + } |
| 25 | + |
| 26 | + for _, tc := range testCases { |
| 27 | + b.Run(tc.name, func(b *testing.B) { |
| 28 | + b.Run("Compress", func(b *testing.B) { |
| 29 | + for i := 0; i < b.N; i++ { |
| 30 | + compressed, err := compressData(data, tc.algo) |
| 31 | + if err != nil { |
| 32 | + b.Fatal(err) |
| 33 | + } |
| 34 | + // 防止编译器优化 |
| 35 | + _ = len(compressed) |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + compressed, err := compressData(data, tc.algo) |
| 40 | + if err != nil { |
| 41 | + b.Fatal(err) |
| 42 | + } |
| 43 | + |
| 44 | + b.Run("Decompress", func(b *testing.B) { |
| 45 | + for i := 0; i < b.N; i++ { |
| 46 | + decompressed, err := decompressData(compressed) |
| 47 | + if err != nil { |
| 48 | + b.Fatal(err) |
| 49 | + } |
| 50 | + // 防止编译器优化 |
| 51 | + _ = len(decompressed) |
| 52 | + } |
| 53 | + }) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// BenchmarkCompressionRatio 对比各算法的压缩率 |
| 59 | +func BenchmarkCompressionRatio(b *testing.B) { |
| 60 | + testCases := []struct { |
| 61 | + name string |
| 62 | + algo CompressionType |
| 63 | + }{ |
| 64 | + {"LZ4", CompressionLZ4}, |
| 65 | + {"Snappy", CompressionSnappy}, |
| 66 | + {"ZSTD", CompressionZSTD}, |
| 67 | + } |
| 68 | + |
| 69 | + dataSizes := []int{1024, 10240, 102400} // 1KB, 10KB, 100KB |
| 70 | + |
| 71 | + for _, size := range dataSizes { |
| 72 | + // 生成不同大小的测试数据 |
| 73 | + var data []byte |
| 74 | + for i := 0; i < size; i++ { |
| 75 | + data = append(data, byte('A'+i%26)) |
| 76 | + } |
| 77 | + |
| 78 | + for _, tc := range testCases { |
| 79 | + tc := tc |
| 80 | + size := size |
| 81 | + b.Run(fmt.Sprintf("%s-%dB", tc.name, size), func(b *testing.B) { |
| 82 | + for i := 0; i < b.N; i++ { |
| 83 | + compressed, err := compressData(data, tc.algo) |
| 84 | + if err != nil { |
| 85 | + b.Fatal(err) |
| 86 | + } |
| 87 | + ratio := float64(len(compressed)) / float64(len(data)) |
| 88 | + // 防止编译器优化 |
| 89 | + _ = ratio |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// BenchmarkStoreCompression 带存储的压缩性能测试 |
| 97 | +func BenchmarkStoreCompression(b *testing.B) { |
| 98 | + testCases := []struct { |
| 99 | + name string |
| 100 | + algo CompressionType |
| 101 | + }{ |
| 102 | + {"LZ4", CompressionLZ4}, |
| 103 | + {"Snappy", CompressionSnappy}, |
| 104 | + {"ZSTD", CompressionZSTD}, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tc := range testCases { |
| 108 | + tc := tc |
| 109 | + b.Run(tc.name, func(b *testing.B) { |
| 110 | + dbPath := b.TempDir() |
| 111 | + store, err := NewBotreonStoreWithCompression(dbPath, tc.algo) |
| 112 | + if err != nil { |
| 113 | + b.Fatal(err) |
| 114 | + } |
| 115 | + defer store.Close() |
| 116 | + |
| 117 | + largeValue := strings.Repeat("This is a test string that will be compressed. ", 100) |
| 118 | + key := "bench_key" |
| 119 | + |
| 120 | + b.ResetTimer() |
| 121 | + for i := 0; i < b.N; i++ { |
| 122 | + err := store.Set(key, largeValue) |
| 123 | + if err != nil { |
| 124 | + b.Fatal(err) |
| 125 | + } |
| 126 | + _, err = store.Get(key) |
| 127 | + if err != nil { |
| 128 | + b.Fatal(err) |
| 129 | + } |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
10 | 135 | func TestCompressionLZ4(t *testing.T) { |
11 | 136 | dbPath := t.TempDir() |
12 | 137 | store, err := NewBotreonStoreWithCompression(dbPath, CompressionLZ4) |
@@ -67,6 +192,17 @@ func TestCompressionNone(t *testing.T) { |
67 | 192 | assert.Equal(t, largeValue, value) |
68 | 193 | } |
69 | 194 |
|
| 195 | +func TestCompressionDefaultIsSnappy(t *testing.T) { |
| 196 | + dbPath := t.TempDir() |
| 197 | + // 不指定压缩算法,使用默认 |
| 198 | + store, err := NewBotreonStore(dbPath) |
| 199 | + assert.NoError(t, err) |
| 200 | + defer store.Close() |
| 201 | + |
| 202 | + // 验证默认压缩算法是 Snappy |
| 203 | + assert.Equal(t, CompressionSnappy, store.GetCompression()) |
| 204 | +} |
| 205 | + |
70 | 206 | func TestCompressionSmallData(t *testing.T) { |
71 | 207 | dbPath := t.TempDir() |
72 | 208 | store, err := NewBotreonStoreWithCompression(dbPath, CompressionLZ4) |
@@ -134,6 +270,26 @@ func TestCompressionBackwardCompatibility(t *testing.T) { |
134 | 270 | assert.True(t, len(value2) > 0) |
135 | 271 | } |
136 | 272 |
|
| 273 | +func TestCompressionSnappy(t *testing.T) { |
| 274 | + dbPath := t.TempDir() |
| 275 | + store, err := NewBotreonStoreWithCompression(dbPath, CompressionSnappy) |
| 276 | + assert.NoError(t, err) |
| 277 | + defer store.Close() |
| 278 | + |
| 279 | + // 测试大字符串压缩 |
| 280 | + largeValue := strings.Repeat("This is a test string that will be compressed. ", 100) |
| 281 | + key := "large_key" |
| 282 | + |
| 283 | + // 写入 |
| 284 | + err = store.Set(key, largeValue) |
| 285 | + assert.NoError(t, err) |
| 286 | + |
| 287 | + // 读取 |
| 288 | + value, err := store.Get(key) |
| 289 | + assert.NoError(t, err) |
| 290 | + assert.Equal(t, largeValue, value) |
| 291 | +} |
| 292 | + |
137 | 293 | func TestCompressionSwitch(t *testing.T) { |
138 | 294 | dbPath := t.TempDir() |
139 | 295 | store, err := NewBotreonStoreWithCompression(dbPath, CompressionLZ4) |
|
0 commit comments