Skip to content

Commit

Permalink
chore: compression benchmarks (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
fracasula authored Oct 23, 2024
1 parent 50d8320 commit 21abca6
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions compress/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,85 @@ 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 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) {
b.Run("fastest", func(b *testing.B) {
b.ReportAllocs()
c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoFastest)
r, _ := c.Compress(loremIpsumDolor)
defer func() { _ = c.Close() }()

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = c.Decompress(r)
}
})
b.Run("default", func(b *testing.B) {
b.ReportAllocs()
c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoDefault)
r, _ := c.Compress(loremIpsumDolor)
defer func() { _ = c.Close() }()

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = c.Decompress(r)
}
})
b.Run("best", func(b *testing.B) {
b.ReportAllocs()
c, _ := New(CompressionAlgoZstdCgo, CompressionLevelZstdCgoBest)
r, _ := c.Compress(loremIpsumDolor)
defer func() { _ = c.Close() }()

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = c.Decompress(r)
}
})
})
}

0 comments on commit 21abca6

Please sign in to comment.