From 0bb6a9ffcac2686cd7ac5e7cf9cafc1b16e61e72 Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Wed, 23 Oct 2024 13:28:34 +0200 Subject: [PATCH 1/3] chore: compression benchmarks --- compress/benchmark_test.go | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/compress/benchmark_test.go b/compress/benchmark_test.go index 28741844..e15f3d75 100644 --- a/compress/benchmark_test.go +++ b/compress/benchmark_test.go @@ -32,3 +32,83 @@ func BenchmarkNew(b *testing.B) { } }) } + +/* +BenchmarkCompress/zstd-cgo/fastest-24 330330 3634 ns/op 512 B/op 1 allocs/op +BenchmarkCompress/zstd-cgo/default-24 205960 5673 ns/op 512 B/op 1 allocs/op +BenchmarkCompress/zstd-cgo/best-24 51088 23294 ns/op 512 B/op 1 allocs/op +*/ +func BenchmarkCompress(b *testing.B) { + b.Run("zstd-cgo", func(b *testing.B) { + b.Run("fastest", func(b *testing.B) { + b.ReportAllocs() + c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoFastest) + defer func() { _ = c.Close() }() + + for i := 0; i < b.N; i++ { + _, _ = c.Compress(loremIpsumDolor) + } + }) + b.Run("default", func(b *testing.B) { + b.ReportAllocs() + c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoDefault) + defer func() { _ = c.Close() }() + + for i := 0; i < b.N; i++ { + _, _ = c.Compress(loremIpsumDolor) + } + }) + b.Run("best", func(b *testing.B) { + b.ReportAllocs() + c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoBest) + defer func() { _ = c.Close() }() + + for i := 0; i < b.N; i++ { + _, _ = c.Compress(loremIpsumDolor) + } + }) + }) +} + +/* +Benchmark to prove that no matter the compression level, the decompression always takes the same time. + +BenchmarkDecompress/zstd-cgo/fastest-24 518247 2217 ns/op 448 B/op 1 allocs/op +BenchmarkDecompress/zstd-cgo/default-24 528907 2199 ns/op 448 B/op 1 allocs/op +BenchmarkDecompress/zstd-cgo/best-24 527167 2208 ns/op 448 B/op 1 allocs/op +*/ +func BenchmarkDecompress(b *testing.B) { + b.Run("zstd-cgo", func(b *testing.B) { + c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoBest) + r, _ := c.Compress(loremIpsumDolor) + _ = c.Close() + + b.Run("fastest", func(b *testing.B) { + b.ReportAllocs() + c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoFastest) + defer func() { _ = c.Close() }() + + for i := 0; i < b.N; i++ { + _, _ = c.Decompress(r) + } + }) + b.Run("default", func(b *testing.B) { + b.ReportAllocs() + c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoDefault) + defer func() { _ = c.Close() }() + + for i := 0; i < b.N; i++ { + _, _ = c.Decompress(r) + } + }) + b.Run("best", func(b *testing.B) { + b.ReportAllocs() + c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoBest) + defer func() { _ = c.Close() }() + + for i := 0; i < b.N; i++ { + _, _ = c.Decompress(r) + } + }) + }) +} From 8d892a45c5c137491be3149af6cf73502044ea0c Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Wed, 23 Oct 2024 13:47:01 +0200 Subject: [PATCH 2/3] chore: fixing bug --- compress/benchmark_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/compress/benchmark_test.go b/compress/benchmark_test.go index e15f3d75..f2174246 100644 --- a/compress/benchmark_test.go +++ b/compress/benchmark_test.go @@ -73,19 +73,16 @@ func BenchmarkCompress(b *testing.B) { /* Benchmark to prove that no matter the compression level, the decompression always takes the same time. -BenchmarkDecompress/zstd-cgo/fastest-24 518247 2217 ns/op 448 B/op 1 allocs/op -BenchmarkDecompress/zstd-cgo/default-24 528907 2199 ns/op 448 B/op 1 allocs/op -BenchmarkDecompress/zstd-cgo/best-24 527167 2208 ns/op 448 B/op 1 allocs/op +BenchmarkDecompress/zstd-cgo/fastest-24 567346 2048 ns/op 448 B/op 1 allocs/op +BenchmarkDecompress/zstd-cgo/default-24 527701 2210 ns/op 448 B/op 1 allocs/op +BenchmarkDecompress/zstd-cgo/best-24 525459 2208 ns/op 448 B/op 1 allocs/op */ func BenchmarkDecompress(b *testing.B) { b.Run("zstd-cgo", func(b *testing.B) { - c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoBest) - r, _ := c.Compress(loremIpsumDolor) - _ = c.Close() - b.Run("fastest", func(b *testing.B) { b.ReportAllocs() c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoFastest) + r, _ := c.Compress(loremIpsumDolor) defer func() { _ = c.Close() }() for i := 0; i < b.N; i++ { @@ -95,6 +92,7 @@ func BenchmarkDecompress(b *testing.B) { b.Run("default", func(b *testing.B) { b.ReportAllocs() c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoDefault) + r, _ := c.Compress(loremIpsumDolor) defer func() { _ = c.Close() }() for i := 0; i < b.N; i++ { @@ -104,6 +102,7 @@ func BenchmarkDecompress(b *testing.B) { b.Run("best", func(b *testing.B) { b.ReportAllocs() c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoBest) + r, _ := c.Compress(loremIpsumDolor) defer func() { _ = c.Close() }() for i := 0; i < b.N; i++ { From 59893660fb0b7daa544cce94ca0c5bd438e8c3b3 Mon Sep 17 00:00:00 2001 From: Francesco Casula Date: Wed, 23 Oct 2024 13:48:43 +0200 Subject: [PATCH 3/3] chore: resetting timer --- compress/benchmark_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compress/benchmark_test.go b/compress/benchmark_test.go index f2174246..440444f9 100644 --- a/compress/benchmark_test.go +++ b/compress/benchmark_test.go @@ -73,9 +73,9 @@ func BenchmarkCompress(b *testing.B) { /* Benchmark to prove that no matter the compression level, the decompression always takes the same time. -BenchmarkDecompress/zstd-cgo/fastest-24 567346 2048 ns/op 448 B/op 1 allocs/op -BenchmarkDecompress/zstd-cgo/default-24 527701 2210 ns/op 448 B/op 1 allocs/op -BenchmarkDecompress/zstd-cgo/best-24 525459 2208 ns/op 448 B/op 1 allocs/op +BenchmarkDecompress/zstd-cgo/fastest-24 562878 2031 ns/op 448 B/op 1 allocs/op +BenchmarkDecompress/zstd-cgo/default-24 506319 2210 ns/op 448 B/op 1 allocs/op +BenchmarkDecompress/zstd-cgo/best-24 526634 2198 ns/op 448 B/op 1 allocs/op */ func BenchmarkDecompress(b *testing.B) { b.Run("zstd-cgo", func(b *testing.B) { @@ -85,6 +85,7 @@ func BenchmarkDecompress(b *testing.B) { r, _ := c.Compress(loremIpsumDolor) defer func() { _ = c.Close() }() + b.ResetTimer() for i := 0; i < b.N; i++ { _, _ = c.Decompress(r) } @@ -95,6 +96,7 @@ func BenchmarkDecompress(b *testing.B) { r, _ := c.Compress(loremIpsumDolor) defer func() { _ = c.Close() }() + b.ResetTimer() for i := 0; i < b.N; i++ { _, _ = c.Decompress(r) } @@ -105,6 +107,7 @@ func BenchmarkDecompress(b *testing.B) { r, _ := c.Compress(loremIpsumDolor) defer func() { _ = c.Close() }() + b.ResetTimer() for i := 0; i < b.N; i++ { _, _ = c.Decompress(r) }