bench_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 [1 << 16]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 [1 << 16]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 benchmarkCompress(b *testing.B, uncompressed []byte) {
  83. w := bytes.NewBuffer(nil)
  84. zw := lz4.NewWriter(w)
  85. r := bytes.NewReader(uncompressed)
  86. // Determine the compressed size of testfile.
  87. compressedSize, err := io.Copy(zw, r)
  88. if err != nil {
  89. b.Fatal(err)
  90. }
  91. if err := zw.Close(); err != nil {
  92. b.Fatal(err)
  93. }
  94. b.SetBytes(compressedSize)
  95. b.ReportAllocs()
  96. b.ResetTimer()
  97. for i := 0; i < b.N; i++ {
  98. r.Reset(uncompressed)
  99. zw.Reset(w)
  100. io.Copy(zw, r)
  101. }
  102. }
  103. func BenchmarkCompressPg1661(b *testing.B) { benchmarkCompress(b, pg1661) }
  104. func BenchmarkCompressDigits(b *testing.B) { benchmarkCompress(b, digits) }
  105. func BenchmarkCompressTwain(b *testing.B) { benchmarkCompress(b, twain) }
  106. func BenchmarkCompressRand(b *testing.B) { benchmarkCompress(b, random) }