snappy_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package zstd
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "github.com/klauspost/compress/snappy"
  9. )
  10. func TestSnappy_ConvertSimple(t *testing.T) {
  11. in, err := ioutil.ReadFile("testdata/z000028")
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. var comp bytes.Buffer
  16. w := snappy.NewBufferedWriter(&comp)
  17. _, err = io.Copy(w, bytes.NewBuffer(in))
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. err = w.Close()
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. snapLen := comp.Len()
  26. s := SnappyConverter{}
  27. var dst bytes.Buffer
  28. n, err := s.Convert(&comp, &dst)
  29. if err != io.EOF {
  30. t.Fatal(err)
  31. }
  32. if n != int64(dst.Len()) {
  33. t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
  34. }
  35. t.Log("SnappyConverter len", snapLen, "-> zstd len", dst.Len())
  36. dec, err := NewReader(nil)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. defer dec.Close()
  41. decoded, err := dec.DecodeAll(dst.Bytes(), nil)
  42. if err != nil {
  43. t.Error(err, len(decoded))
  44. }
  45. if !bytes.Equal(decoded, in) {
  46. ioutil.WriteFile("testdata/z000028.got", decoded, os.ModePerm)
  47. t.Fatal("Decoded does not match")
  48. }
  49. t.Log("Encoded content matched")
  50. }
  51. func TestSnappy_ConvertXML(t *testing.T) {
  52. f, err := os.Open("testdata/xml.zst")
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. dec, err := NewReader(f)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. defer dec.Close()
  61. in, err := ioutil.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. ioutil.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 := ioutil.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. ioutil.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. dec, err := NewReader(f)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. defer dec.Close()
  161. in, err := ioutil.ReadAll(dec)
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. var comp bytes.Buffer
  166. w := snappy.NewBufferedWriter(&comp)
  167. _, err = io.Copy(w, bytes.NewBuffer(in))
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. err = w.Close()
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. snapLen := comp.Len()
  176. s := SnappyConverter{}
  177. var dst bytes.Buffer
  178. n, err := s.Convert(&comp, &dst)
  179. if err != io.EOF {
  180. t.Fatal(err)
  181. }
  182. if n != int64(dst.Len()) {
  183. t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
  184. }
  185. t.Log("SnappyConverter len", snapLen, "-> zstd len", dst.Len())
  186. decoded, err := dec.DecodeAll(dst.Bytes(), nil)
  187. if err != nil {
  188. t.Error(err, len(decoded))
  189. }
  190. if !bytes.Equal(decoded, in) {
  191. ioutil.WriteFile("testdata/enwik9.got", decoded, os.ModePerm)
  192. t.Fatal("Decoded does not match")
  193. }
  194. t.Log("Encoded content matched")
  195. }
  196. func BenchmarkSnappy_ConvertXML(b *testing.B) {
  197. f, err := os.Open("testdata/xml.zst")
  198. if err != nil {
  199. b.Fatal(err)
  200. }
  201. dec, err := NewReader(f)
  202. if err != nil {
  203. b.Fatal(err)
  204. }
  205. defer dec.Close()
  206. in, err := ioutil.ReadAll(dec)
  207. if err != nil {
  208. b.Fatal(err)
  209. }
  210. var comp bytes.Buffer
  211. w := snappy.NewBufferedWriter(&comp)
  212. _, err = io.Copy(w, bytes.NewBuffer(in))
  213. if err != nil {
  214. b.Fatal(err)
  215. }
  216. err = w.Close()
  217. if err != nil {
  218. b.Fatal(err)
  219. }
  220. s := SnappyConverter{}
  221. compBytes := comp.Bytes()
  222. _, err = s.Convert(&comp, ioutil.Discard)
  223. if err != io.EOF {
  224. b.Fatal(err)
  225. }
  226. b.ResetTimer()
  227. b.ReportAllocs()
  228. b.SetBytes(int64(len(in)))
  229. for i := 0; i < b.N; i++ {
  230. _, err := s.Convert(bytes.NewBuffer(compBytes), ioutil.Discard)
  231. if err != io.EOF {
  232. b.Fatal(err)
  233. }
  234. }
  235. }
  236. func BenchmarkSnappy_Enwik9(b *testing.B) {
  237. f, err := os.Open("testdata/enwik9.zst")
  238. if err != nil {
  239. b.Skip(err)
  240. return
  241. }
  242. dec, err := NewReader(f)
  243. if err != nil {
  244. b.Fatal(err)
  245. }
  246. defer dec.Close()
  247. in, err := ioutil.ReadAll(dec)
  248. if err != nil {
  249. b.Fatal(err)
  250. }
  251. defer dec.Close()
  252. var comp bytes.Buffer
  253. w := snappy.NewBufferedWriter(&comp)
  254. _, err = io.Copy(w, bytes.NewBuffer(in))
  255. if err != nil {
  256. b.Fatal(err)
  257. }
  258. err = w.Close()
  259. if err != nil {
  260. b.Fatal(err)
  261. }
  262. s := SnappyConverter{}
  263. compBytes := comp.Bytes()
  264. _, err = s.Convert(&comp, ioutil.Discard)
  265. if err != io.EOF {
  266. b.Fatal(err)
  267. }
  268. b.ResetTimer()
  269. b.ReportAllocs()
  270. b.SetBytes(int64(len(in)))
  271. for i := 0; i < b.N; i++ {
  272. _, err := s.Convert(bytes.NewBuffer(compBytes), ioutil.Discard)
  273. if err != io.EOF {
  274. b.Fatal(err)
  275. }
  276. }
  277. }
  278. func BenchmarkSnappy_ConvertSilesia(b *testing.B) {
  279. in, err := ioutil.ReadFile("testdata/silesia.tar")
  280. if err != nil {
  281. if os.IsNotExist(err) {
  282. b.Skip("Missing testdata/silesia.tar")
  283. return
  284. }
  285. b.Fatal(err)
  286. }
  287. var comp bytes.Buffer
  288. w := snappy.NewBufferedWriter(&comp)
  289. _, err = io.Copy(w, bytes.NewBuffer(in))
  290. if err != nil {
  291. b.Fatal(err)
  292. }
  293. err = w.Close()
  294. if err != nil {
  295. b.Fatal(err)
  296. }
  297. s := SnappyConverter{}
  298. compBytes := comp.Bytes()
  299. _, err = s.Convert(&comp, ioutil.Discard)
  300. if err != io.EOF {
  301. b.Fatal(err)
  302. }
  303. b.ResetTimer()
  304. b.ReportAllocs()
  305. b.SetBytes(int64(len(in)))
  306. for i := 0; i < b.N; i++ {
  307. _, err := s.Convert(bytes.NewBuffer(compBytes), ioutil.Discard)
  308. if err != io.EOF {
  309. b.Fatal(err)
  310. }
  311. }
  312. }