lz4sconvert_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2023 Klaus Post. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package s2
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "path/filepath"
  9. "testing"
  10. "github.com/klauspost/compress/internal/lz4ref"
  11. "github.com/klauspost/compress/internal/snapref"
  12. )
  13. func TestLZ4sConverter_ConvertBlock(t *testing.T) {
  14. for _, tf := range testFiles {
  15. t.Run(tf.label, func(t *testing.T) {
  16. if err := downloadBenchmarkFiles(t, tf.filename); err != nil {
  17. t.Fatalf("failed to download testdata: %s", err)
  18. }
  19. bDir := filepath.FromSlash(*benchdataDir)
  20. data := readFile(t, filepath.Join(bDir, tf.filename))
  21. if n := tf.sizeLimit; 0 < n && n < len(data) {
  22. data = data[:n]
  23. }
  24. lz4Data := make([]byte, lz4ref.CompressBlockBound(len(data)))
  25. n, err := lz4ref.CompressBlockLZ4s(data, lz4Data)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if n == 0 {
  30. t.Skip("incompressible")
  31. return
  32. }
  33. t.Log("input size:", len(data))
  34. t.Log("lz4 size:", n)
  35. lz4Data = lz4Data[:n]
  36. s2Dst := make([]byte, binary.MaxVarintLen32, MaxEncodedLen(len(data)))
  37. s2Dst = s2Dst[:binary.PutUvarint(s2Dst, uint64(len(data)))]
  38. hdr := len(s2Dst)
  39. conv := LZ4sConverter{}
  40. szS := 0
  41. out, n, err := conv.ConvertBlockSnappy(s2Dst, lz4Data)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. if n != len(data) {
  46. t.Fatalf("length mismatch: want %d, got %d", len(data), n)
  47. }
  48. szS = len(out) - hdr
  49. t.Log("lz4->snappy size:", szS)
  50. decom, err := snapref.Decode(nil, out)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if !bytes.Equal(decom, data) {
  55. t.Errorf("output mismatch")
  56. }
  57. sz := 0
  58. out, n, err = conv.ConvertBlock(s2Dst, lz4Data)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if n != len(data) {
  63. t.Fatalf("length mismatch: want %d, got %d", len(data), n)
  64. }
  65. sz = len(out) - hdr
  66. t.Log("lz4->s2 size:", sz)
  67. decom, err = Decode(nil, out)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if !bytes.Equal(decom, data) {
  72. t.Errorf("output mismatch")
  73. }
  74. out2 := Encode(s2Dst[:0], data)
  75. sz2 := len(out2) - hdr
  76. t.Log("s2 (default) size:", sz2)
  77. out2 = EncodeBetter(s2Dst[:0], data)
  78. sz3 := len(out2) - hdr
  79. t.Log("s2 (better) size:", sz3)
  80. t.Log("lz4 -> s2 bytes saved:", len(lz4Data)-sz)
  81. t.Log("lz4 -> snappy bytes saved:", len(lz4Data)-szS)
  82. t.Log("data -> s2 (default) bytes saved:", len(lz4Data)-sz2)
  83. t.Log("data -> s2 (better) bytes saved:", len(lz4Data)-sz3)
  84. t.Log("direct data -> s2 (default) compared to converted from lz4:", sz-sz2)
  85. t.Log("direct data -> s2 (better) compared to converted from lz4:", sz-sz3)
  86. })
  87. }
  88. }