bench_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package lz4_test
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. "testing"
  7. "github.com/pierrec/lz4/v3"
  8. )
  9. func BenchmarkCompress(b *testing.B) {
  10. buf := make([]byte, len(pg1661))
  11. b.ReportAllocs()
  12. b.ResetTimer()
  13. for i := 0; i < b.N; i++ {
  14. _, _ = lz4.CompressBlock(pg1661, buf, nil)
  15. }
  16. }
  17. func BenchmarkCompressRandom(b *testing.B) {
  18. buf := make([]byte, len(randomLZ4))
  19. b.ReportAllocs()
  20. b.SetBytes(int64(len(random)))
  21. b.ResetTimer()
  22. for i := 0; i < b.N; i++ {
  23. _, _ = lz4.CompressBlock(random, buf, nil)
  24. }
  25. }
  26. func BenchmarkCompressHC(b *testing.B) {
  27. buf := make([]byte, len(pg1661))
  28. b.ReportAllocs()
  29. b.ResetTimer()
  30. for i := 0; i < b.N; i++ {
  31. _, _ = lz4.CompressBlockHC(pg1661, buf, 16)
  32. }
  33. }
  34. func BenchmarkUncompress(b *testing.B) {
  35. buf := make([]byte, len(pg1661))
  36. b.ReportAllocs()
  37. b.ResetTimer()
  38. for i := 0; i < b.N; i++ {
  39. _, _ = lz4.UncompressBlock(pg1661LZ4, buf)
  40. }
  41. }
  42. func mustLoadFile(f string) []byte {
  43. b, err := ioutil.ReadFile(f)
  44. if err != nil {
  45. panic(err)
  46. }
  47. return b
  48. }
  49. var (
  50. pg1661 = mustLoadFile("testdata/pg1661.txt")
  51. digits = mustLoadFile("testdata/e.txt")
  52. twain = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt")
  53. random = mustLoadFile("testdata/random.data")
  54. pg1661LZ4 = mustLoadFile("testdata/pg1661.txt.lz4")
  55. digitsLZ4 = mustLoadFile("testdata/e.txt.lz4")
  56. twainLZ4 = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.lz4")
  57. randomLZ4 = mustLoadFile("testdata/random.data.lz4")
  58. )
  59. func benchmarkUncompress(b *testing.B, compressed []byte) {
  60. r := bytes.NewReader(compressed)
  61. zr := lz4.NewReader(r)
  62. // Determine the uncompressed size of testfile.
  63. uncompressedSize, err := io.Copy(ioutil.Discard, zr)
  64. if err != nil {
  65. b.Fatal(err)
  66. }
  67. b.SetBytes(uncompressedSize)
  68. b.ReportAllocs()
  69. b.ResetTimer()
  70. for i := 0; i < b.N; i++ {
  71. r.Reset(compressed)
  72. zr.Reset(r)
  73. _, _ = io.Copy(ioutil.Discard, zr)
  74. }
  75. }
  76. func BenchmarkUncompressPg1661(b *testing.B) { benchmarkUncompress(b, pg1661LZ4) }
  77. func BenchmarkUncompressDigits(b *testing.B) { benchmarkUncompress(b, digitsLZ4) }
  78. func BenchmarkUncompressTwain(b *testing.B) { benchmarkUncompress(b, twainLZ4) }
  79. func BenchmarkUncompressRand(b *testing.B) { benchmarkUncompress(b, randomLZ4) }
  80. func benchmarkSkipBytes(b *testing.B, compressed []byte) {
  81. r := bytes.NewReader(compressed)
  82. zr := lz4.NewReader(r)
  83. // Determine the uncompressed size of testfile.
  84. uncompressedSize, err := io.Copy(ioutil.Discard, zr)
  85. if err != nil {
  86. b.Fatal(err)
  87. }
  88. b.SetBytes(uncompressedSize)
  89. b.ReportAllocs()
  90. b.ResetTimer()
  91. for i := 0; i < b.N; i++ {
  92. r.Reset(compressed)
  93. zr.Reset(r)
  94. zr.Seek(uncompressedSize, io.SeekCurrent)
  95. _, _ = io.Copy(ioutil.Discard, zr)
  96. }
  97. }
  98. func BenchmarkSkipBytesPg1661(b *testing.B) { benchmarkSkipBytes(b, pg1661LZ4) }
  99. func BenchmarkSkipBytesDigits(b *testing.B) { benchmarkSkipBytes(b, digitsLZ4) }
  100. func BenchmarkSkipBytesTwain(b *testing.B) { benchmarkSkipBytes(b, twainLZ4) }
  101. func BenchmarkSkipBytesRand(b *testing.B) { benchmarkSkipBytes(b, randomLZ4) }
  102. func benchmarkCompress(b *testing.B, uncompressed []byte) {
  103. w := bytes.NewBuffer(nil)
  104. zw := lz4.NewWriter(w)
  105. r := bytes.NewReader(uncompressed)
  106. // Determine the compressed size of testfile.
  107. compressedSize, err := io.Copy(zw, r)
  108. if err != nil {
  109. b.Fatal(err)
  110. }
  111. if err := zw.Close(); err != nil {
  112. b.Fatal(err)
  113. }
  114. b.SetBytes(compressedSize)
  115. b.ReportAllocs()
  116. b.ResetTimer()
  117. for i := 0; i < b.N; i++ {
  118. r.Reset(uncompressed)
  119. zw.Reset(w)
  120. _, _ = io.Copy(zw, r)
  121. }
  122. }
  123. func BenchmarkCompressPg1661(b *testing.B) { benchmarkCompress(b, pg1661) }
  124. func BenchmarkCompressDigits(b *testing.B) { benchmarkCompress(b, digits) }
  125. func BenchmarkCompressTwain(b *testing.B) { benchmarkCompress(b, twain) }
  126. func BenchmarkCompressRand(b *testing.B) { benchmarkCompress(b, random) }
  127. // Benchmark to check reallocations upon Reset().
  128. // See issue https://github.com/pierrec/lz4/issues/52.
  129. func BenchmarkWriterReset(b *testing.B) {
  130. b.ReportAllocs()
  131. zw := lz4.NewWriter(nil)
  132. src := mustLoadFile("testdata/gettysburg.txt")
  133. var buf bytes.Buffer
  134. for n := 0; n < b.N; n++ {
  135. buf.Reset()
  136. zw.Reset(&buf)
  137. _, _ = zw.Write(src)
  138. _ = zw.Close()
  139. }
  140. }