snappy_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright 2011 The Snappy-Go Authors. 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 snappy
  5. import (
  6. "bytes"
  7. "flag"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "math/rand"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "strings"
  16. "testing"
  17. )
  18. var download = flag.Bool("download", false, "If true, download any missing files before running benchmarks")
  19. func roundtrip(b, ebuf, dbuf []byte) error {
  20. e, err := Encode(ebuf, b)
  21. if err != nil {
  22. return fmt.Errorf("encoding error: %v", err)
  23. }
  24. d, err := Decode(dbuf, e)
  25. if err != nil {
  26. return fmt.Errorf("decoding error: %v", err)
  27. }
  28. if !bytes.Equal(b, d) {
  29. return fmt.Errorf("roundtrip mismatch:\n\twant %v\n\tgot %v", b, d)
  30. }
  31. return nil
  32. }
  33. func TestEmpty(t *testing.T) {
  34. if err := roundtrip(nil, nil, nil); err != nil {
  35. t.Fatal(err)
  36. }
  37. }
  38. func TestSmallCopy(t *testing.T) {
  39. for _, ebuf := range [][]byte{nil, make([]byte, 20), make([]byte, 64)} {
  40. for _, dbuf := range [][]byte{nil, make([]byte, 20), make([]byte, 64)} {
  41. for i := 0; i < 32; i++ {
  42. s := "aaaa" + strings.Repeat("b", i) + "aaaabbbb"
  43. if err := roundtrip([]byte(s), ebuf, dbuf); err != nil {
  44. t.Errorf("len(ebuf)=%d, len(dbuf)=%d, i=%d: %v", len(ebuf), len(dbuf), i, err)
  45. }
  46. }
  47. }
  48. }
  49. }
  50. func TestSmallRand(t *testing.T) {
  51. rand.Seed(27354294)
  52. for n := 1; n < 20000; n += 23 {
  53. b := make([]byte, n)
  54. for i := range b {
  55. b[i] = uint8(rand.Uint32())
  56. }
  57. if err := roundtrip(b, nil, nil); err != nil {
  58. t.Fatal(err)
  59. }
  60. }
  61. }
  62. func TestSmallRegular(t *testing.T) {
  63. for n := 1; n < 20000; n += 23 {
  64. b := make([]byte, n)
  65. for i := range b {
  66. b[i] = uint8(i%10 + 'a')
  67. }
  68. if err := roundtrip(b, nil, nil); err != nil {
  69. t.Fatal(err)
  70. }
  71. }
  72. }
  73. func cmp(a, b []byte) error {
  74. if len(a) != len(b) {
  75. return fmt.Errorf("got %d bytes, want %d", len(a), len(b))
  76. }
  77. for i := range a {
  78. if a[i] != b[i] {
  79. return fmt.Errorf("byte #%d: got 0x%02x, want 0x%02x", i, a[i], b[i])
  80. }
  81. }
  82. return nil
  83. }
  84. func TestFramingFormat(t *testing.T) {
  85. for _, tf := range testFiles {
  86. if err := downloadTestdata(tf.filename); err != nil {
  87. t.Fatalf("failed to download testdata: %s", err)
  88. }
  89. src := readFile(t, filepath.Join("testdata", tf.filename))
  90. buf := new(bytes.Buffer)
  91. if _, err := NewWriter(buf).Write(src); err != nil {
  92. t.Errorf("%s: encoding: %v", tf.filename, err)
  93. continue
  94. }
  95. dst, err := ioutil.ReadAll(NewReader(buf))
  96. if err != nil {
  97. t.Errorf("%s: decoding: %v", tf.filename, err)
  98. continue
  99. }
  100. if err := cmp(dst, src); err != nil {
  101. t.Errorf("%s: %v", tf.filename, err)
  102. continue
  103. }
  104. }
  105. }
  106. func TestReaderReset(t *testing.T) {
  107. gold := bytes.Repeat([]byte("All that is gold does not glitter,\n"), 10000)
  108. buf := new(bytes.Buffer)
  109. if _, err := NewWriter(buf).Write(gold); err != nil {
  110. t.Fatalf("Write: %v", err)
  111. }
  112. encoded, invalid, partial := buf.String(), "invalid", "partial"
  113. r := NewReader(nil)
  114. for i, s := range []string{encoded, invalid, partial, encoded, partial, invalid, encoded, encoded} {
  115. if s == partial {
  116. r.Reset(strings.NewReader(encoded))
  117. if _, err := r.Read(make([]byte, 101)); err != nil {
  118. t.Errorf("#%d: %v", i, err)
  119. continue
  120. }
  121. continue
  122. }
  123. r.Reset(strings.NewReader(s))
  124. got, err := ioutil.ReadAll(r)
  125. switch s {
  126. case encoded:
  127. if err != nil {
  128. t.Errorf("#%d: %v", i, err)
  129. continue
  130. }
  131. if err := cmp(got, gold); err != nil {
  132. t.Errorf("%#d: %v", i, err)
  133. continue
  134. }
  135. case invalid:
  136. if err == nil {
  137. t.Errorf("#%d: got nil error, want non-nil", i)
  138. continue
  139. }
  140. }
  141. }
  142. }
  143. func TestWriterReset(t *testing.T) {
  144. gold := bytes.Repeat([]byte("Not all those who wander are lost;\n"), 10000)
  145. var gots, wants [][]byte
  146. const n = 20
  147. w, failed := NewWriter(nil), false
  148. for i := 0; i <= n; i++ {
  149. buf := new(bytes.Buffer)
  150. w.Reset(buf)
  151. want := gold[:len(gold)*i/n]
  152. if _, err := w.Write(want); err != nil {
  153. t.Errorf("#%d: Write: %v", i, err)
  154. failed = true
  155. continue
  156. }
  157. got, err := ioutil.ReadAll(NewReader(buf))
  158. if err != nil {
  159. t.Errorf("#%d: ReadAll: %v", i, err)
  160. failed = true
  161. continue
  162. }
  163. gots = append(gots, got)
  164. wants = append(wants, want)
  165. }
  166. if failed {
  167. return
  168. }
  169. for i := range gots {
  170. if err := cmp(gots[i], wants[i]); err != nil {
  171. t.Errorf("#%d: %v", i, err)
  172. }
  173. }
  174. }
  175. func benchDecode(b *testing.B, src []byte) {
  176. encoded, err := Encode(nil, src)
  177. if err != nil {
  178. b.Fatal(err)
  179. }
  180. // Bandwidth is in amount of uncompressed data.
  181. b.SetBytes(int64(len(src)))
  182. b.ResetTimer()
  183. for i := 0; i < b.N; i++ {
  184. Decode(src, encoded)
  185. }
  186. }
  187. func benchEncode(b *testing.B, src []byte) {
  188. // Bandwidth is in amount of uncompressed data.
  189. b.SetBytes(int64(len(src)))
  190. dst := make([]byte, MaxEncodedLen(len(src)))
  191. b.ResetTimer()
  192. for i := 0; i < b.N; i++ {
  193. Encode(dst, src)
  194. }
  195. }
  196. func readFile(b testing.TB, filename string) []byte {
  197. src, err := ioutil.ReadFile(filename)
  198. if err != nil {
  199. b.Fatalf("failed reading %s: %s", filename, err)
  200. }
  201. if len(src) == 0 {
  202. b.Fatalf("%s has zero length", filename)
  203. }
  204. return src
  205. }
  206. // expand returns a slice of length n containing repeated copies of src.
  207. func expand(src []byte, n int) []byte {
  208. dst := make([]byte, n)
  209. for x := dst; len(x) > 0; {
  210. i := copy(x, src)
  211. x = x[i:]
  212. }
  213. return dst
  214. }
  215. func benchWords(b *testing.B, n int, decode bool) {
  216. // Note: the file is OS-language dependent so the resulting values are not
  217. // directly comparable for non-US-English OS installations.
  218. data := expand(readFile(b, "/usr/share/dict/words"), n)
  219. if decode {
  220. benchDecode(b, data)
  221. } else {
  222. benchEncode(b, data)
  223. }
  224. }
  225. func BenchmarkWordsDecode1e3(b *testing.B) { benchWords(b, 1e3, true) }
  226. func BenchmarkWordsDecode1e4(b *testing.B) { benchWords(b, 1e4, true) }
  227. func BenchmarkWordsDecode1e5(b *testing.B) { benchWords(b, 1e5, true) }
  228. func BenchmarkWordsDecode1e6(b *testing.B) { benchWords(b, 1e6, true) }
  229. func BenchmarkWordsEncode1e3(b *testing.B) { benchWords(b, 1e3, false) }
  230. func BenchmarkWordsEncode1e4(b *testing.B) { benchWords(b, 1e4, false) }
  231. func BenchmarkWordsEncode1e5(b *testing.B) { benchWords(b, 1e5, false) }
  232. func BenchmarkWordsEncode1e6(b *testing.B) { benchWords(b, 1e6, false) }
  233. // testFiles' values are copied directly from
  234. // https://code.google.com/p/snappy/source/browse/trunk/snappy_unittest.cc.
  235. // The label field is unused in snappy-go.
  236. var testFiles = []struct {
  237. label string
  238. filename string
  239. }{
  240. {"html", "html"},
  241. {"urls", "urls.10K"},
  242. {"jpg", "house.jpg"},
  243. {"pdf", "mapreduce-osdi-1.pdf"},
  244. {"html4", "html_x_4"},
  245. {"cp", "cp.html"},
  246. {"c", "fields.c"},
  247. {"lsp", "grammar.lsp"},
  248. {"xls", "kennedy.xls"},
  249. {"txt1", "alice29.txt"},
  250. {"txt2", "asyoulik.txt"},
  251. {"txt3", "lcet10.txt"},
  252. {"txt4", "plrabn12.txt"},
  253. {"bin", "ptt5"},
  254. {"sum", "sum"},
  255. {"man", "xargs.1"},
  256. {"pb", "geo.protodata"},
  257. {"gaviota", "kppkn.gtb"},
  258. }
  259. // The test data files are present at this canonical URL.
  260. const baseURL = "https://snappy.googlecode.com/svn/trunk/testdata/"
  261. func downloadTestdata(basename string) (errRet error) {
  262. filename := filepath.Join("testdata", basename)
  263. if stat, err := os.Stat(filename); err == nil && stat.Size() != 0 {
  264. return nil
  265. }
  266. if !*download {
  267. return fmt.Errorf("test data not found; skipping benchmark without the -download flag")
  268. }
  269. // Download the official snappy C++ implementation reference test data
  270. // files for benchmarking.
  271. if err := os.Mkdir("testdata", 0777); err != nil && !os.IsExist(err) {
  272. return fmt.Errorf("failed to create testdata: %s", err)
  273. }
  274. f, err := os.Create(filename)
  275. if err != nil {
  276. return fmt.Errorf("failed to create %s: %s", filename, err)
  277. }
  278. defer f.Close()
  279. defer func() {
  280. if errRet != nil {
  281. os.Remove(filename)
  282. }
  283. }()
  284. resp, err := http.Get(baseURL + basename)
  285. if err != nil {
  286. return fmt.Errorf("failed to download %s: %s", baseURL+basename, err)
  287. }
  288. defer resp.Body.Close()
  289. _, err = io.Copy(f, resp.Body)
  290. if err != nil {
  291. return fmt.Errorf("failed to write %s: %s", filename, err)
  292. }
  293. return nil
  294. }
  295. func benchFile(b *testing.B, n int, decode bool) {
  296. if err := downloadTestdata(testFiles[n].filename); err != nil {
  297. b.Fatalf("failed to download testdata: %s", err)
  298. }
  299. data := readFile(b, filepath.Join("testdata", testFiles[n].filename))
  300. if decode {
  301. benchDecode(b, data)
  302. } else {
  303. benchEncode(b, data)
  304. }
  305. }
  306. // Naming convention is kept similar to what snappy's C++ implementation uses.
  307. func Benchmark_UFlat0(b *testing.B) { benchFile(b, 0, true) }
  308. func Benchmark_UFlat1(b *testing.B) { benchFile(b, 1, true) }
  309. func Benchmark_UFlat2(b *testing.B) { benchFile(b, 2, true) }
  310. func Benchmark_UFlat3(b *testing.B) { benchFile(b, 3, true) }
  311. func Benchmark_UFlat4(b *testing.B) { benchFile(b, 4, true) }
  312. func Benchmark_UFlat5(b *testing.B) { benchFile(b, 5, true) }
  313. func Benchmark_UFlat6(b *testing.B) { benchFile(b, 6, true) }
  314. func Benchmark_UFlat7(b *testing.B) { benchFile(b, 7, true) }
  315. func Benchmark_UFlat8(b *testing.B) { benchFile(b, 8, true) }
  316. func Benchmark_UFlat9(b *testing.B) { benchFile(b, 9, true) }
  317. func Benchmark_UFlat10(b *testing.B) { benchFile(b, 10, true) }
  318. func Benchmark_UFlat11(b *testing.B) { benchFile(b, 11, true) }
  319. func Benchmark_UFlat12(b *testing.B) { benchFile(b, 12, true) }
  320. func Benchmark_UFlat13(b *testing.B) { benchFile(b, 13, true) }
  321. func Benchmark_UFlat14(b *testing.B) { benchFile(b, 14, true) }
  322. func Benchmark_UFlat15(b *testing.B) { benchFile(b, 15, true) }
  323. func Benchmark_UFlat16(b *testing.B) { benchFile(b, 16, true) }
  324. func Benchmark_UFlat17(b *testing.B) { benchFile(b, 17, true) }
  325. func Benchmark_ZFlat0(b *testing.B) { benchFile(b, 0, false) }
  326. func Benchmark_ZFlat1(b *testing.B) { benchFile(b, 1, false) }
  327. func Benchmark_ZFlat2(b *testing.B) { benchFile(b, 2, false) }
  328. func Benchmark_ZFlat3(b *testing.B) { benchFile(b, 3, false) }
  329. func Benchmark_ZFlat4(b *testing.B) { benchFile(b, 4, false) }
  330. func Benchmark_ZFlat5(b *testing.B) { benchFile(b, 5, false) }
  331. func Benchmark_ZFlat6(b *testing.B) { benchFile(b, 6, false) }
  332. func Benchmark_ZFlat7(b *testing.B) { benchFile(b, 7, false) }
  333. func Benchmark_ZFlat8(b *testing.B) { benchFile(b, 8, false) }
  334. func Benchmark_ZFlat9(b *testing.B) { benchFile(b, 9, false) }
  335. func Benchmark_ZFlat10(b *testing.B) { benchFile(b, 10, false) }
  336. func Benchmark_ZFlat11(b *testing.B) { benchFile(b, 11, false) }
  337. func Benchmark_ZFlat12(b *testing.B) { benchFile(b, 12, false) }
  338. func Benchmark_ZFlat13(b *testing.B) { benchFile(b, 13, false) }
  339. func Benchmark_ZFlat14(b *testing.B) { benchFile(b, 14, false) }
  340. func Benchmark_ZFlat15(b *testing.B) { benchFile(b, 15, false) }
  341. func Benchmark_ZFlat16(b *testing.B) { benchFile(b, 16, false) }
  342. func Benchmark_ZFlat17(b *testing.B) { benchFile(b, 17, false) }