snappy_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 benchDecode(b *testing.B, src []byte) {
  74. encoded, err := Encode(nil, src)
  75. if err != nil {
  76. b.Fatal(err)
  77. }
  78. // Bandwidth is in amount of uncompressed data.
  79. b.SetBytes(int64(len(src)))
  80. b.ResetTimer()
  81. for i := 0; i < b.N; i++ {
  82. Decode(src, encoded)
  83. }
  84. }
  85. func benchEncode(b *testing.B, src []byte) {
  86. // Bandwidth is in amount of uncompressed data.
  87. b.SetBytes(int64(len(src)))
  88. dst := make([]byte, MaxEncodedLen(len(src)))
  89. b.ResetTimer()
  90. for i := 0; i < b.N; i++ {
  91. Encode(dst, src)
  92. }
  93. }
  94. func readFile(b *testing.B, filename string) []byte {
  95. src, err := ioutil.ReadFile(filename)
  96. if err != nil {
  97. b.Fatalf("failed reading %s: %s", filename, err)
  98. }
  99. if len(src) == 0 {
  100. b.Fatalf("%s has zero length", filename)
  101. }
  102. return src
  103. }
  104. // expand returns a slice of length n containing repeated copies of src.
  105. func expand(src []byte, n int) []byte {
  106. dst := make([]byte, n)
  107. for x := dst; len(x) > 0; {
  108. i := copy(x, src)
  109. x = x[i:]
  110. }
  111. return dst
  112. }
  113. func benchWords(b *testing.B, n int, decode bool) {
  114. // Note: the file is OS-language dependent so the resulting values are not
  115. // directly comparable for non-US-English OS installations.
  116. data := expand(readFile(b, "/usr/share/dict/words"), n)
  117. if decode {
  118. benchDecode(b, data)
  119. } else {
  120. benchEncode(b, data)
  121. }
  122. }
  123. func BenchmarkWordsDecode1e3(b *testing.B) { benchWords(b, 1e3, true) }
  124. func BenchmarkWordsDecode1e4(b *testing.B) { benchWords(b, 1e4, true) }
  125. func BenchmarkWordsDecode1e5(b *testing.B) { benchWords(b, 1e5, true) }
  126. func BenchmarkWordsDecode1e6(b *testing.B) { benchWords(b, 1e6, true) }
  127. func BenchmarkWordsEncode1e3(b *testing.B) { benchWords(b, 1e3, false) }
  128. func BenchmarkWordsEncode1e4(b *testing.B) { benchWords(b, 1e4, false) }
  129. func BenchmarkWordsEncode1e5(b *testing.B) { benchWords(b, 1e5, false) }
  130. func BenchmarkWordsEncode1e6(b *testing.B) { benchWords(b, 1e6, false) }
  131. // testFiles' values are copied directly from
  132. // https://code.google.com/p/snappy/source/browse/trunk/snappy_unittest.cc.
  133. // The label field is unused in snappy-go.
  134. var testFiles = []struct {
  135. label string
  136. filename string
  137. }{
  138. {"html", "html"},
  139. {"urls", "urls.10K"},
  140. {"jpg", "house.jpg"},
  141. {"pdf", "mapreduce-osdi-1.pdf"},
  142. {"html4", "html_x_4"},
  143. {"cp", "cp.html"},
  144. {"c", "fields.c"},
  145. {"lsp", "grammar.lsp"},
  146. {"xls", "kennedy.xls"},
  147. {"txt1", "alice29.txt"},
  148. {"txt2", "asyoulik.txt"},
  149. {"txt3", "lcet10.txt"},
  150. {"txt4", "plrabn12.txt"},
  151. {"bin", "ptt5"},
  152. {"sum", "sum"},
  153. {"man", "xargs.1"},
  154. {"pb", "geo.protodata"},
  155. {"gaviota", "kppkn.gtb"},
  156. }
  157. // The test data files are present at this canonical URL.
  158. const baseURL = "https://snappy.googlecode.com/svn/trunk/testdata/"
  159. func downloadTestdata(basename string) (errRet error) {
  160. filename := filepath.Join("testdata", basename)
  161. f, err := os.Create(filename)
  162. if err != nil {
  163. return fmt.Errorf("failed to create %s: %s", filename, err)
  164. }
  165. defer f.Close()
  166. defer func() {
  167. if errRet != nil {
  168. os.Remove(filename)
  169. }
  170. }()
  171. resp, err := http.Get(baseURL + basename)
  172. if err != nil {
  173. return fmt.Errorf("failed to download %s: %s", baseURL+basename, err)
  174. }
  175. defer resp.Body.Close()
  176. _, err = io.Copy(f, resp.Body)
  177. if err != nil {
  178. return fmt.Errorf("failed to write %s: %s", filename, err)
  179. }
  180. return nil
  181. }
  182. func benchFile(b *testing.B, n int, decode bool) {
  183. filename := filepath.Join("testdata", testFiles[n].filename)
  184. if stat, err := os.Stat(filename); err != nil || stat.Size() == 0 {
  185. if !*download {
  186. b.Fatal("test data not found; skipping benchmark without the -download flag")
  187. }
  188. // Download the official snappy C++ implementation reference test data
  189. // files for benchmarking.
  190. if err := os.Mkdir("testdata", 0777); err != nil && !os.IsExist(err) {
  191. b.Fatalf("failed to create testdata: %s", err)
  192. }
  193. for _, tf := range testFiles {
  194. if err := downloadTestdata(tf.filename); err != nil {
  195. b.Fatalf("failed to download testdata: %s", err)
  196. }
  197. }
  198. }
  199. data := readFile(b, filename)
  200. if decode {
  201. benchDecode(b, data)
  202. } else {
  203. benchEncode(b, data)
  204. }
  205. }
  206. // Naming convention is kept similar to what snappy's C++ implementation uses.
  207. func Benchmark_UFlat0(b *testing.B) { benchFile(b, 0, true) }
  208. func Benchmark_UFlat1(b *testing.B) { benchFile(b, 1, true) }
  209. func Benchmark_UFlat2(b *testing.B) { benchFile(b, 2, true) }
  210. func Benchmark_UFlat3(b *testing.B) { benchFile(b, 3, true) }
  211. func Benchmark_UFlat4(b *testing.B) { benchFile(b, 4, true) }
  212. func Benchmark_UFlat5(b *testing.B) { benchFile(b, 5, true) }
  213. func Benchmark_UFlat6(b *testing.B) { benchFile(b, 6, true) }
  214. func Benchmark_UFlat7(b *testing.B) { benchFile(b, 7, true) }
  215. func Benchmark_UFlat8(b *testing.B) { benchFile(b, 8, true) }
  216. func Benchmark_UFlat9(b *testing.B) { benchFile(b, 9, true) }
  217. func Benchmark_UFlat10(b *testing.B) { benchFile(b, 10, true) }
  218. func Benchmark_UFlat11(b *testing.B) { benchFile(b, 11, true) }
  219. func Benchmark_UFlat12(b *testing.B) { benchFile(b, 12, true) }
  220. func Benchmark_UFlat13(b *testing.B) { benchFile(b, 13, true) }
  221. func Benchmark_UFlat14(b *testing.B) { benchFile(b, 14, true) }
  222. func Benchmark_UFlat15(b *testing.B) { benchFile(b, 15, true) }
  223. func Benchmark_UFlat16(b *testing.B) { benchFile(b, 16, true) }
  224. func Benchmark_UFlat17(b *testing.B) { benchFile(b, 17, true) }
  225. func Benchmark_ZFlat0(b *testing.B) { benchFile(b, 0, false) }
  226. func Benchmark_ZFlat1(b *testing.B) { benchFile(b, 1, false) }
  227. func Benchmark_ZFlat2(b *testing.B) { benchFile(b, 2, false) }
  228. func Benchmark_ZFlat3(b *testing.B) { benchFile(b, 3, false) }
  229. func Benchmark_ZFlat4(b *testing.B) { benchFile(b, 4, false) }
  230. func Benchmark_ZFlat5(b *testing.B) { benchFile(b, 5, false) }
  231. func Benchmark_ZFlat6(b *testing.B) { benchFile(b, 6, false) }
  232. func Benchmark_ZFlat7(b *testing.B) { benchFile(b, 7, false) }
  233. func Benchmark_ZFlat8(b *testing.B) { benchFile(b, 8, false) }
  234. func Benchmark_ZFlat9(b *testing.B) { benchFile(b, 9, false) }
  235. func Benchmark_ZFlat10(b *testing.B) { benchFile(b, 10, false) }
  236. func Benchmark_ZFlat11(b *testing.B) { benchFile(b, 11, false) }
  237. func Benchmark_ZFlat12(b *testing.B) { benchFile(b, 12, false) }
  238. func Benchmark_ZFlat13(b *testing.B) { benchFile(b, 13, false) }
  239. func Benchmark_ZFlat14(b *testing.B) { benchFile(b, 14, false) }
  240. func Benchmark_ZFlat15(b *testing.B) { benchFile(b, 15, false) }
  241. func Benchmark_ZFlat16(b *testing.B) { benchFile(b, 16, false) }
  242. func Benchmark_ZFlat17(b *testing.B) { benchFile(b, 17, false) }