snappy_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package zstd
  2. import (
  3. "bytes"
  4. "io"
  5. "os"
  6. "testing"
  7. snappy "github.com/klauspost/compress/internal/snapref"
  8. )
  9. func TestSnappy_ConvertSimple(t *testing.T) {
  10. in, err := os.ReadFile("testdata/z000028")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. var comp bytes.Buffer
  15. w := snappy.NewBufferedWriter(&comp)
  16. _, err = io.Copy(w, bytes.NewBuffer(in))
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. err = w.Close()
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. snapLen := comp.Len()
  25. s := SnappyConverter{}
  26. var dst bytes.Buffer
  27. n, err := s.Convert(&comp, &dst)
  28. if err != io.EOF {
  29. t.Fatal(err)
  30. }
  31. if n != int64(dst.Len()) {
  32. t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
  33. }
  34. t.Log("SnappyConverter len", snapLen, "-> zstd len", dst.Len())
  35. dec, err := NewReader(nil)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. defer dec.Close()
  40. decoded, err := dec.DecodeAll(dst.Bytes(), nil)
  41. if err != nil {
  42. t.Error(err, len(decoded))
  43. }
  44. if !bytes.Equal(decoded, in) {
  45. os.WriteFile("testdata/z000028.got", decoded, os.ModePerm)
  46. t.Fatal("Decoded does not match")
  47. }
  48. t.Log("Encoded content matched")
  49. }
  50. func TestSnappy_ConvertXML(t *testing.T) {
  51. f, err := os.Open("testdata/xml.zst")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. defer f.Close()
  56. dec, err := NewReader(f)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. defer dec.Close()
  61. in, err := io.ReadAll(dec)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. var comp bytes.Buffer
  66. w := snappy.NewBufferedWriter(&comp)
  67. _, err = io.Copy(w, bytes.NewBuffer(in))
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. err = w.Close()
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. snapLen := comp.Len()
  76. s := SnappyConverter{}
  77. var dst bytes.Buffer
  78. n, err := s.Convert(&comp, &dst)
  79. if err != io.EOF {
  80. t.Fatal(err)
  81. }
  82. if n != int64(dst.Len()) {
  83. t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
  84. }
  85. t.Log("Snappy len", snapLen, "-> zstd len", dst.Len())
  86. decoded, err := dec.DecodeAll(dst.Bytes(), nil)
  87. if err != nil {
  88. t.Error(err, len(decoded))
  89. }
  90. if !bytes.Equal(decoded, in) {
  91. os.WriteFile("testdata/xml.got", decoded, os.ModePerm)
  92. t.Fatal("Decoded does not match")
  93. }
  94. t.Log("Encoded content matched")
  95. }
  96. func TestSnappy_ConvertSilesia(t *testing.T) {
  97. if testing.Short() {
  98. t.SkipNow()
  99. }
  100. in, err := os.ReadFile("testdata/silesia.tar")
  101. if err != nil {
  102. if os.IsNotExist(err) {
  103. t.Skip("Missing testdata/silesia.tar")
  104. return
  105. }
  106. t.Fatal(err)
  107. }
  108. var comp bytes.Buffer
  109. w := snappy.NewBufferedWriter(&comp)
  110. _, err = io.Copy(w, bytes.NewBuffer(in))
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. err = w.Close()
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. snapLen := comp.Len()
  119. s := SnappyConverter{}
  120. var dst bytes.Buffer
  121. n, err := s.Convert(&comp, &dst)
  122. if err != io.EOF {
  123. t.Fatal(err)
  124. }
  125. if n != int64(dst.Len()) {
  126. t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
  127. }
  128. t.Log("SnappyConverter len", snapLen, "-> zstd len", dst.Len())
  129. dec, err := NewReader(nil)
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. defer dec.Close()
  134. decoded, err := dec.DecodeAll(dst.Bytes(), nil)
  135. if err != nil {
  136. t.Error(err, len(decoded))
  137. }
  138. if !bytes.Equal(decoded, in) {
  139. os.WriteFile("testdata/silesia.tar.got", decoded, os.ModePerm)
  140. t.Fatal("Decoded does not match")
  141. }
  142. t.Log("Encoded content matched")
  143. }
  144. func TestSnappy_ConvertEnwik9(t *testing.T) {
  145. if testing.Short() {
  146. t.SkipNow()
  147. }
  148. file := "testdata/enwik9.zst"
  149. f, err := os.Open(file)
  150. if err != nil {
  151. if os.IsNotExist(err) {
  152. t.Skip("To run extended tests, download http://mattmahoney.net/dc/enwik9.zip unzip it \n" +
  153. "compress it with 'zstd -15 -T0 enwik9' and place it in " + file)
  154. }
  155. }
  156. defer f.Close()
  157. dec, err := NewReader(f)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. defer dec.Close()
  162. in, err := io.ReadAll(dec)
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. var comp bytes.Buffer
  167. w := snappy.NewBufferedWriter(&comp)
  168. _, err = io.Copy(w, bytes.NewBuffer(in))
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. err = w.Close()
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. snapLen := comp.Len()
  177. s := SnappyConverter{}
  178. var dst bytes.Buffer
  179. n, err := s.Convert(&comp, &dst)
  180. if err != io.EOF {
  181. t.Fatal(err)
  182. }
  183. if n != int64(dst.Len()) {
  184. t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
  185. }
  186. t.Log("SnappyConverter len", snapLen, "-> zstd len", dst.Len())
  187. decoded, err := dec.DecodeAll(dst.Bytes(), nil)
  188. if err != nil {
  189. t.Error(err, len(decoded))
  190. }
  191. if !bytes.Equal(decoded, in) {
  192. os.WriteFile("testdata/enwik9.got", decoded, os.ModePerm)
  193. t.Fatal("Decoded does not match")
  194. }
  195. t.Log("Encoded content matched")
  196. }
  197. func BenchmarkSnappy_ConvertXML(b *testing.B) {
  198. f, err := os.Open("testdata/xml.zst")
  199. if err != nil {
  200. b.Fatal(err)
  201. }
  202. defer f.Close()
  203. dec, err := NewReader(f)
  204. if err != nil {
  205. b.Fatal(err)
  206. }
  207. defer dec.Close()
  208. in, err := io.ReadAll(dec)
  209. if err != nil {
  210. b.Fatal(err)
  211. }
  212. var comp bytes.Buffer
  213. w := snappy.NewBufferedWriter(&comp)
  214. _, err = io.Copy(w, bytes.NewBuffer(in))
  215. if err != nil {
  216. b.Fatal(err)
  217. }
  218. err = w.Close()
  219. if err != nil {
  220. b.Fatal(err)
  221. }
  222. s := SnappyConverter{}
  223. compBytes := comp.Bytes()
  224. _, err = s.Convert(&comp, io.Discard)
  225. if err != io.EOF {
  226. b.Fatal(err)
  227. }
  228. b.ResetTimer()
  229. b.ReportAllocs()
  230. b.SetBytes(int64(len(in)))
  231. for i := 0; i < b.N; i++ {
  232. _, err := s.Convert(bytes.NewBuffer(compBytes), io.Discard)
  233. if err != io.EOF {
  234. b.Fatal(err)
  235. }
  236. }
  237. }
  238. func BenchmarkSnappy_Enwik9(b *testing.B) {
  239. f, err := os.Open("testdata/enwik9.zst")
  240. if err != nil {
  241. b.Skip(err)
  242. return
  243. }
  244. defer f.Close()
  245. dec, err := NewReader(f)
  246. if err != nil {
  247. b.Fatal(err)
  248. }
  249. defer dec.Close()
  250. in, err := io.ReadAll(dec)
  251. if err != nil {
  252. b.Fatal(err)
  253. }
  254. defer dec.Close()
  255. var comp bytes.Buffer
  256. w := snappy.NewBufferedWriter(&comp)
  257. _, err = io.Copy(w, bytes.NewBuffer(in))
  258. if err != nil {
  259. b.Fatal(err)
  260. }
  261. err = w.Close()
  262. if err != nil {
  263. b.Fatal(err)
  264. }
  265. s := SnappyConverter{}
  266. compBytes := comp.Bytes()
  267. _, err = s.Convert(&comp, io.Discard)
  268. if err != io.EOF {
  269. b.Fatal(err)
  270. }
  271. b.ResetTimer()
  272. b.ReportAllocs()
  273. b.SetBytes(int64(len(in)))
  274. for i := 0; i < b.N; i++ {
  275. _, err := s.Convert(bytes.NewBuffer(compBytes), io.Discard)
  276. if err != io.EOF {
  277. b.Fatal(err)
  278. }
  279. }
  280. }
  281. func BenchmarkSnappy_ConvertSilesia(b *testing.B) {
  282. in, err := os.ReadFile("testdata/silesia.tar")
  283. if err != nil {
  284. if os.IsNotExist(err) {
  285. b.Skip("Missing testdata/silesia.tar")
  286. return
  287. }
  288. b.Fatal(err)
  289. }
  290. var comp bytes.Buffer
  291. w := snappy.NewBufferedWriter(&comp)
  292. _, err = io.Copy(w, bytes.NewBuffer(in))
  293. if err != nil {
  294. b.Fatal(err)
  295. }
  296. err = w.Close()
  297. if err != nil {
  298. b.Fatal(err)
  299. }
  300. s := SnappyConverter{}
  301. compBytes := comp.Bytes()
  302. _, err = s.Convert(&comp, io.Discard)
  303. if err != io.EOF {
  304. b.Fatal(err)
  305. }
  306. b.ResetTimer()
  307. b.ReportAllocs()
  308. b.SetBytes(int64(len(in)))
  309. for i := 0; i < b.N; i++ {
  310. _, err := s.Convert(bytes.NewBuffer(compBytes), io.Discard)
  311. if err != io.EOF {
  312. b.Fatal(err)
  313. }
  314. }
  315. }