bench_test.go 4.1 KB

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