Skip to content

Commit 2e90ea6

Browse files
author
tac0turtle
committed
move compression to da
1 parent 684dded commit 2e90ea6

File tree

8 files changed

+100
-88
lines changed

8 files changed

+100
-88
lines changed
File renamed without changes.
File renamed without changes.

compression/cmd/benchmark/main.go renamed to da/compression/cmd/benchmark/main.go

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import (
88
"strconv"
99
"time"
1010

11-
"github.com/evstack/ev-node/compression"
1211
"github.com/evstack/ev-node/core/da"
12+
"github.com/evstack/ev-node/da/compression"
1313
)
1414

1515
// BenchmarkResult holds the results of a compression benchmark
1616
type BenchmarkResult struct {
17-
Algorithm string
18-
Level int
19-
DataSize int
20-
CompressedSize int
21-
CompressionRatio float64
22-
CompressTime time.Duration
23-
DecompressTime time.Duration
24-
CompressionSpeed float64 // MB/s
17+
Algorithm string
18+
Level int
19+
DataSize int
20+
CompressedSize int
21+
CompressionRatio float64
22+
CompressTime time.Duration
23+
DecompressTime time.Duration
24+
CompressionSpeed float64 // MB/s
2525
DecompressionSpeed float64 // MB/s
2626
}
2727

@@ -65,13 +65,13 @@ func generateTestData(dataType TestDataType, size int) da.Blob {
6565
}
6666
}
6767
return data[:size]
68-
68+
6969
case Random:
7070
// Random data that doesn't compress well
7171
data := make([]byte, size)
7272
rand.Read(data)
7373
return data
74-
74+
7575
case JSON:
7676
// JSON-like structured data
7777
jsonTemplate := `{"id":%d,"name":"user_%d","email":"user%[email protected]","data":"%s","timestamp":%d,"active":true}`
@@ -91,7 +91,7 @@ func generateTestData(dataType TestDataType, size int) da.Blob {
9191
counter++
9292
}
9393
return data[:min(len(data), size)]
94-
94+
9595
case Text:
9696
// Natural language text (moderately compressible)
9797
words := []string{
@@ -125,16 +125,16 @@ func runBenchmark(config compression.Config, testData da.Blob, iterations int) (
125125
return nil, err
126126
}
127127
defer compressor.Close()
128-
128+
129129
// Warm up
130130
_, err = compression.CompressBlob(testData)
131131
if err != nil {
132132
return nil, err
133133
}
134-
134+
135135
var totalCompressTime, totalDecompressTime time.Duration
136136
var compressedData da.Blob
137-
137+
138138
// Run compression benchmark
139139
for i := 0; i < iterations; i++ {
140140
start := time.Now()
@@ -144,7 +144,7 @@ func runBenchmark(config compression.Config, testData da.Blob, iterations int) (
144144
}
145145
totalCompressTime += time.Since(start)
146146
}
147-
147+
148148
// Run decompression benchmark
149149
for i := 0; i < iterations; i++ {
150150
start := time.Now()
@@ -154,21 +154,21 @@ func runBenchmark(config compression.Config, testData da.Blob, iterations int) (
154154
}
155155
totalDecompressTime += time.Since(start)
156156
}
157-
157+
158158
avgCompressTime := totalCompressTime / time.Duration(iterations)
159159
avgDecompressTime := totalDecompressTime / time.Duration(iterations)
160-
160+
161161
compressionRatio := float64(len(compressedData)) / float64(len(testData))
162162
compressionSpeed := float64(len(testData)) / 1024 / 1024 / avgCompressTime.Seconds()
163163
decompressionSpeed := float64(len(testData)) / 1024 / 1024 / avgDecompressTime.Seconds()
164-
164+
165165
// Get actual compressed size (minus header)
166166
info := compression.GetCompressionInfo(compressedData)
167167
actualCompressedSize := int(info.CompressedSize)
168168
if !info.IsCompressed {
169169
actualCompressedSize = int(info.OriginalSize)
170170
}
171-
171+
172172
return &BenchmarkResult{
173173
Algorithm: "zstd",
174174
Level: config.ZstdLevel,
@@ -188,7 +188,7 @@ func printResults(dataType TestDataType, results []*BenchmarkResult) {
188188
"Level", "Size", "Compressed", "Ratio", "Comp Time", "Comp Speed", "Decomp Time", "Decomp Speed")
189189
fmt.Printf("%-6s %-10s %-12s %-8s %-12s %-15s %-12s %-15s\n",
190190
"-----", "--------", "----------", "------", "---------", "-----------", "----------", "-------------")
191-
191+
192192
for _, result := range results {
193193
fmt.Printf("%-6d %-10s %-12s %-8.3f %-12s %-15s %-12s %-15s\n",
194194
result.Level,
@@ -240,96 +240,95 @@ func min(a, b int) int {
240240
func main() {
241241
fmt.Println("EV-Node Zstd Compression Benchmark")
242242
fmt.Println("==================================")
243-
243+
244244
// Parse command line arguments
245245
iterations := 100
246246
if len(os.Args) > 1 {
247247
if i, err := strconv.Atoi(os.Args[1]); err == nil {
248248
iterations = i
249249
}
250250
}
251-
251+
252252
testSizes := []int{1024, 4096, 16384, 65536} // 1KB, 4KB, 16KB, 64KB
253253
testDataTypes := []TestDataType{Repetitive, Text, JSON, Random}
254254
zstdLevels := []int{1, 3, 6, 9} // Test different levels, highlighting level 3
255-
255+
256256
fmt.Printf("Running %d iterations per test\n", iterations)
257257
fmt.Printf("Test sizes: %v\n", testSizes)
258258
fmt.Printf("Zstd levels: %v (level 3 is recommended)\n", zstdLevels)
259-
259+
260260
for _, dataType := range testDataTypes {
261261
var allResults []*BenchmarkResult
262-
262+
263263
for _, size := range testSizes {
264264
testData := generateTestData(dataType, size)
265-
265+
266266
for _, level := range zstdLevels {
267267
config := compression.Config{
268268
Enabled: true,
269269
ZstdLevel: level,
270270
MinCompressionRatio: 0.05, // Allow more compression attempts for benchmarking
271271
}
272-
272+
273273
result, err := runBenchmark(config, testData, iterations)
274274
if err != nil {
275275
fmt.Printf("Error benchmarking %s data (size: %d, level: %d): %v\n",
276276
dataType, size, level, err)
277277
continue
278278
}
279-
279+
280280
allResults = append(allResults, result)
281281
}
282282
}
283-
283+
284284
printResults(dataType, allResults)
285285
}
286-
286+
287287
// Print recommendations
288288
fmt.Printf("\n=== Recommendations ===\n")
289289
fmt.Printf("• **Zstd Level 3**: Optimal balance of compression ratio and speed\n")
290290
fmt.Printf("• **Best for EV-Node**: Fast compression (~100-200 MB/s) with good ratios (~20-40%%)\n")
291291
fmt.Printf("• **Memory efficient**: Lower memory usage than higher compression levels\n")
292292
fmt.Printf("• **Production ready**: Widely used default level in many applications\n")
293293
fmt.Printf("\n")
294-
294+
295295
// Real-world example
296296
fmt.Printf("=== Real-World Example ===\n")
297297
realWorldData := generateTestData(JSON, 10240) // 10KB typical blob
298-
config := compression.DefaultConfig()
299-
298+
300299
start := time.Now()
301300
compressed, err := compression.CompressBlob(realWorldData)
302301
compressTime := time.Since(start)
303-
302+
304303
if err != nil {
305304
fmt.Printf("Error in real-world example: %v\n", err)
306305
return
307306
}
308-
307+
309308
start = time.Now()
310309
decompressed, err := compression.DecompressBlob(compressed)
311310
decompressTime := time.Since(start)
312-
311+
313312
if err != nil {
314313
fmt.Printf("Error decompressing: %v\n", err)
315314
return
316315
}
317-
316+
318317
if !bytes.Equal(realWorldData, decompressed) {
319318
fmt.Printf("Data integrity error!\n")
320319
return
321320
}
322-
321+
323322
info := compression.GetCompressionInfo(compressed)
324-
323+
325324
fmt.Printf("Original size: %s\n", formatBytes(len(realWorldData)))
326325
fmt.Printf("Compressed size: %s\n", formatBytes(int(info.CompressedSize)))
327-
fmt.Printf("Compression ratio: %.1f%% (%.1f%% savings)\n",
326+
fmt.Printf("Compression ratio: %.1f%% (%.1f%% savings)\n",
328327
info.CompressionRatio*100, (1-info.CompressionRatio)*100)
329328
fmt.Printf("Compression time: %s\n", formatDuration(compressTime))
330329
fmt.Printf("Decompression time: %s\n", formatDuration(decompressTime))
331-
fmt.Printf("Compression speed: %.1f MB/s\n",
330+
fmt.Printf("Compression speed: %.1f MB/s\n",
332331
float64(len(realWorldData))/1024/1024/compressTime.Seconds())
333-
fmt.Printf("Decompression speed: %.1f MB/s\n",
332+
fmt.Printf("Decompression speed: %.1f MB/s\n",
334333
float64(len(realWorldData))/1024/1024/decompressTime.Seconds())
335-
}
334+
}

0 commit comments

Comments
 (0)