bench_test.go 3.4 KB

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