bench_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 BenchmarkCompressHC(b *testing.B) {
  19. buf := make([]byte, len(pg1661))
  20. b.ReportAllocs()
  21. b.ResetTimer()
  22. for i := 0; i < b.N; i++ {
  23. lz4.CompressBlockHC(pg1661, buf, 16)
  24. }
  25. }
  26. func BenchmarkUncompress(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.UncompressBlock(pg1661LZ4, buf)
  32. }
  33. }
  34. func mustLoadFile(f string) []byte {
  35. b, err := ioutil.ReadFile(f)
  36. if err != nil {
  37. panic(err)
  38. }
  39. return b
  40. }
  41. var (
  42. pg1661 = mustLoadFile("testdata/pg1661.txt")
  43. digits = mustLoadFile("testdata/e.txt")
  44. twain = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt")
  45. random = mustLoadFile("testdata/random.data")
  46. pg1661LZ4 = mustLoadFile("testdata/pg1661.txt.lz4")
  47. digitsLZ4 = mustLoadFile("testdata/e.txt.lz4")
  48. twainLZ4 = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.lz4")
  49. randomLZ4 = mustLoadFile("testdata/random.data.lz4")
  50. )
  51. func benchmarkUncompress(b *testing.B, compressed []byte) {
  52. r := bytes.NewReader(compressed)
  53. zr := lz4.NewReader(r)
  54. // Determine the uncompressed size of testfile.
  55. uncompressedSize, err := io.Copy(ioutil.Discard, zr)
  56. if err != nil {
  57. b.Fatal(err)
  58. }
  59. b.SetBytes(uncompressedSize)
  60. b.ReportAllocs()
  61. b.ResetTimer()
  62. for i := 0; i < b.N; i++ {
  63. r.Reset(compressed)
  64. zr.Reset(r)
  65. io.Copy(ioutil.Discard, zr)
  66. }
  67. }
  68. func BenchmarkUncompressPg1661(b *testing.B) { benchmarkUncompress(b, pg1661LZ4) }
  69. func BenchmarkUncompressDigits(b *testing.B) { benchmarkUncompress(b, digitsLZ4) }
  70. func BenchmarkUncompressTwain(b *testing.B) { benchmarkUncompress(b, twainLZ4) }
  71. func BenchmarkUncompressRand(b *testing.B) { benchmarkUncompress(b, randomLZ4) }
  72. func benchmarkCompress(b *testing.B, uncompressed []byte) {
  73. w := bytes.NewBuffer(nil)
  74. zw := lz4.NewWriter(w)
  75. r := bytes.NewReader(uncompressed)
  76. // Determine the compressed size of testfile.
  77. compressedSize, err := io.Copy(zw, r)
  78. if err != nil {
  79. b.Fatal(err)
  80. }
  81. if err := zw.Close(); err != nil {
  82. b.Fatal(err)
  83. }
  84. b.SetBytes(compressedSize)
  85. b.ReportAllocs()
  86. b.ResetTimer()
  87. for i := 0; i < b.N; i++ {
  88. r.Reset(uncompressed)
  89. zw.Reset(w)
  90. io.Copy(zw, r)
  91. }
  92. }
  93. func BenchmarkCompressPg1661(b *testing.B) { benchmarkCompress(b, pg1661) }
  94. func BenchmarkCompressDigits(b *testing.B) { benchmarkCompress(b, digits) }
  95. func BenchmarkCompressTwain(b *testing.B) { benchmarkCompress(b, twain) }
  96. func BenchmarkCompressRand(b *testing.B) { benchmarkCompress(b, random) }