snappy_test.go 8.8 KB

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