snappy_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 (
  19. download = flag.Bool("download", false, "If true, download any missing files before running benchmarks")
  20. testdata = flag.String("testdata", "testdata", "Directory containing the test data")
  21. )
  22. func TestMaxEncodedLenOfMaxUncompressedChunkLen(t *testing.T) {
  23. got := maxEncodedLenOfMaxUncompressedChunkLen
  24. want := MaxEncodedLen(maxUncompressedChunkLen)
  25. if got != want {
  26. t.Fatalf("got %d, want %d", got, want)
  27. }
  28. }
  29. func roundtrip(b, ebuf, dbuf []byte) error {
  30. d, err := Decode(dbuf, Encode(ebuf, b))
  31. if err != nil {
  32. return fmt.Errorf("decoding error: %v", err)
  33. }
  34. if !bytes.Equal(b, d) {
  35. return fmt.Errorf("roundtrip mismatch:\n\twant %v\n\tgot %v", b, d)
  36. }
  37. return nil
  38. }
  39. func TestEmpty(t *testing.T) {
  40. if err := roundtrip(nil, nil, nil); err != nil {
  41. t.Fatal(err)
  42. }
  43. }
  44. func TestSmallCopy(t *testing.T) {
  45. for _, ebuf := range [][]byte{nil, make([]byte, 20), make([]byte, 64)} {
  46. for _, dbuf := range [][]byte{nil, make([]byte, 20), make([]byte, 64)} {
  47. for i := 0; i < 32; i++ {
  48. s := "aaaa" + strings.Repeat("b", i) + "aaaabbbb"
  49. if err := roundtrip([]byte(s), ebuf, dbuf); err != nil {
  50. t.Errorf("len(ebuf)=%d, len(dbuf)=%d, i=%d: %v", len(ebuf), len(dbuf), i, err)
  51. }
  52. }
  53. }
  54. }
  55. }
  56. func TestSmallRand(t *testing.T) {
  57. rng := rand.New(rand.NewSource(1))
  58. for n := 1; n < 20000; n += 23 {
  59. b := make([]byte, n)
  60. for i := range b {
  61. b[i] = uint8(rng.Uint32())
  62. }
  63. if err := roundtrip(b, nil, nil); err != nil {
  64. t.Fatal(err)
  65. }
  66. }
  67. }
  68. func TestSmallRegular(t *testing.T) {
  69. for n := 1; n < 20000; n += 23 {
  70. b := make([]byte, n)
  71. for i := range b {
  72. b[i] = uint8(i%10 + 'a')
  73. }
  74. if err := roundtrip(b, nil, nil); err != nil {
  75. t.Fatal(err)
  76. }
  77. }
  78. }
  79. func TestInvalidVarint(t *testing.T) {
  80. data := []byte("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00")
  81. if _, err := DecodedLen(data); err != ErrCorrupt {
  82. t.Errorf("DecodedLen: got %v, want ErrCorrupt", err)
  83. }
  84. if _, err := Decode(nil, data); err != ErrCorrupt {
  85. t.Errorf("Decode: got %v, want ErrCorrupt", err)
  86. }
  87. // The encoded varint overflows 32 bits
  88. data = []byte("\xff\xff\xff\xff\xff\x00")
  89. if _, err := DecodedLen(data); err != ErrCorrupt {
  90. t.Errorf("DecodedLen: got %v, want ErrCorrupt", err)
  91. }
  92. if _, err := Decode(nil, data); err != ErrCorrupt {
  93. t.Errorf("Decode: got %v, want ErrCorrupt", err)
  94. }
  95. }
  96. func TestDecode(t *testing.T) {
  97. testCases := []struct {
  98. desc string
  99. input string
  100. want string
  101. wantErr error
  102. }{{
  103. `decodedLen=0x100000000 is too long`,
  104. "\x80\x80\x80\x80\x10" + "\x00\x41",
  105. "",
  106. ErrCorrupt,
  107. }, {
  108. `decodedLen=3; tagLiteral, 0-byte length; length=3; valid input`,
  109. "\x03" + "\x08\xff\xff\xff",
  110. "\xff\xff\xff",
  111. nil,
  112. }, {
  113. `decodedLen=1; tagLiteral, 1-byte length; not enough length bytes`,
  114. "\x01" + "\xf0",
  115. "",
  116. ErrCorrupt,
  117. }, {
  118. `decodedLen=3; tagLiteral, 1-byte length; length=3; valid input`,
  119. "\x03" + "\xf0\x02\xff\xff\xff",
  120. "\xff\xff\xff",
  121. nil,
  122. }, {
  123. `decodedLen=1; tagLiteral, 2-byte length; not enough length bytes`,
  124. "\x01" + "\xf4\x00",
  125. "",
  126. ErrCorrupt,
  127. }, {
  128. `decodedLen=3; tagLiteral, 2-byte length; length=3; valid input`,
  129. "\x03" + "\xf4\x02\x00\xff\xff\xff",
  130. "\xff\xff\xff",
  131. nil,
  132. }, {
  133. `decodedLen=1; tagLiteral, 3-byte length; not enough length bytes`,
  134. "\x01" + "\xf8\x00\x00",
  135. "",
  136. ErrCorrupt,
  137. }, {
  138. `decodedLen=3; tagLiteral, 3-byte length; length=3; valid input`,
  139. "\x03" + "\xf8\x02\x00\x00\xff\xff\xff",
  140. "\xff\xff\xff",
  141. nil,
  142. }, {
  143. `decodedLen=1; tagLiteral, 4-byte length; not enough length bytes`,
  144. "\x01" + "\xfc\x00\x00\x00",
  145. "",
  146. ErrCorrupt,
  147. }, {
  148. `decodedLen=1; tagLiteral, 4-byte length; length=3; not enough dst bytes`,
  149. "\x01" + "\xfc\x02\x00\x00\x00\xff\xff\xff",
  150. "",
  151. ErrCorrupt,
  152. }, {
  153. `decodedLen=4; tagLiteral, 4-byte length; length=3; not enough src bytes`,
  154. "\x04" + "\xfc\x02\x00\x00\x00\xff",
  155. "",
  156. ErrCorrupt,
  157. }, {
  158. `decodedLen=3; tagLiteral, 4-byte length; length=3; valid input`,
  159. "\x03" + "\xfc\x02\x00\x00\x00\xff\xff\xff",
  160. "\xff\xff\xff",
  161. nil,
  162. }, {
  163. `decodedLen=4; tagCopy1, 1 extra length|offset byte; not enough extra bytes`,
  164. "\x04" + "\x01",
  165. "",
  166. ErrCorrupt,
  167. }, {
  168. `decodedLen=4; tagCopy2, 2 extra length|offset bytes; not enough extra bytes`,
  169. "\x04" + "\x02\x00",
  170. "",
  171. ErrCorrupt,
  172. }, {
  173. `decodedLen=4; tagCopy4; unsupported COPY_4 tag`,
  174. "\x04" + "\x03\x00\x00\x00\x00",
  175. "",
  176. errUnsupportedCopy4Tag,
  177. }, {
  178. `decodedLen=4; tagLiteral (4 bytes "abcd"); valid input`,
  179. "\x04" + "\x0cabcd",
  180. "abcd",
  181. nil,
  182. }, {
  183. `decodedLen=8; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=4; valid input`,
  184. "\x08" + "\x0cabcd" + "\x01\x04",
  185. "abcdabcd",
  186. nil,
  187. }, {
  188. `decodedLen=8; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=0; zero offset`,
  189. "\x08" + "\x0cabcd" + "\x01\x00",
  190. "",
  191. ErrCorrupt,
  192. }, {
  193. `decodedLen=9; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=4; inconsistent dLen`,
  194. "\x09" + "\x0cabcd" + "\x01\x04",
  195. "",
  196. ErrCorrupt,
  197. }, {
  198. `decodedLen=8; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=5; offset too large`,
  199. "\x08" + "\x0cabcd" + "\x01\x05",
  200. "",
  201. ErrCorrupt,
  202. }, {
  203. `decodedLen=7; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=4; length too large`,
  204. "\x07" + "\x0cabcd" + "\x01\x04",
  205. "",
  206. ErrCorrupt,
  207. }}
  208. for _, tc := range testCases {
  209. g, gotErr := Decode(nil, []byte(tc.input))
  210. if got := string(g); got != tc.want || gotErr != tc.wantErr {
  211. t.Errorf("%s:\ngot %q, %v\nwant %q, %v", tc.desc, got, gotErr, tc.want, tc.wantErr)
  212. }
  213. }
  214. }
  215. func cmp(a, b []byte) error {
  216. if len(a) != len(b) {
  217. return fmt.Errorf("got %d bytes, want %d", len(a), len(b))
  218. }
  219. for i := range a {
  220. if a[i] != b[i] {
  221. return fmt.Errorf("byte #%d: got 0x%02x, want 0x%02x", i, a[i], b[i])
  222. }
  223. }
  224. return nil
  225. }
  226. func TestFramingFormat(t *testing.T) {
  227. // src is comprised of alternating 1e5-sized sequences of random
  228. // (incompressible) bytes and repeated (compressible) bytes. 1e5 was chosen
  229. // because it is larger than maxUncompressedChunkLen (64k).
  230. src := make([]byte, 1e6)
  231. rng := rand.New(rand.NewSource(1))
  232. for i := 0; i < 10; i++ {
  233. if i%2 == 0 {
  234. for j := 0; j < 1e5; j++ {
  235. src[1e5*i+j] = uint8(rng.Intn(256))
  236. }
  237. } else {
  238. for j := 0; j < 1e5; j++ {
  239. src[1e5*i+j] = uint8(i)
  240. }
  241. }
  242. }
  243. buf := new(bytes.Buffer)
  244. if _, err := NewWriter(buf).Write(src); err != nil {
  245. t.Fatalf("Write: encoding: %v", err)
  246. }
  247. dst, err := ioutil.ReadAll(NewReader(buf))
  248. if err != nil {
  249. t.Fatalf("ReadAll: decoding: %v", err)
  250. }
  251. if err := cmp(dst, src); err != nil {
  252. t.Fatal(err)
  253. }
  254. }
  255. func TestWriterGoldenOutput(t *testing.T) {
  256. buf := new(bytes.Buffer)
  257. w := NewBufferedWriter(buf)
  258. defer w.Close()
  259. w.Write([]byte("abcd")) // Not compressible.
  260. w.Flush()
  261. w.Write(bytes.Repeat([]byte{'A'}, 100)) // Compressible.
  262. w.Flush()
  263. got := buf.String()
  264. want := strings.Join([]string{
  265. magicChunk,
  266. "\x01\x08\x00\x00", // Uncompressed chunk, 8 bytes long (including 4 byte checksum).
  267. "\x68\x10\xe6\xb6", // Checksum.
  268. "\x61\x62\x63\x64", // Uncompressed payload: "abcd".
  269. "\x00\x0d\x00\x00", // Compressed chunk, 13 bytes long (including 4 byte checksum).
  270. "\x37\xcb\xbc\x9d", // Checksum.
  271. "\x64", // Compressed payload: Uncompressed length (varint encoded): 100.
  272. "\x00\x41", // Compressed payload: tagLiteral, length=1, "A".
  273. "\xfe\x01\x00", // Compressed payload: tagCopy2, length=64, offset=1.
  274. "\x8a\x01\x00", // Compressed payload: tagCopy2, length=35, offset=1.
  275. }, "")
  276. if got != want {
  277. t.Fatalf("\ngot: % x\nwant: % x", got, want)
  278. }
  279. }
  280. func TestNewBufferedWriter(t *testing.T) {
  281. // Test all 32 possible sub-sequences of these 5 input slices.
  282. //
  283. // Their lengths sum to 400,000, which is over 6 times the Writer ibuf
  284. // capacity: 6 * maxUncompressedChunkLen is 393,216.
  285. inputs := [][]byte{
  286. bytes.Repeat([]byte{'a'}, 40000),
  287. bytes.Repeat([]byte{'b'}, 150000),
  288. bytes.Repeat([]byte{'c'}, 60000),
  289. bytes.Repeat([]byte{'d'}, 120000),
  290. bytes.Repeat([]byte{'e'}, 30000),
  291. }
  292. loop:
  293. for i := 0; i < 1<<uint(len(inputs)); i++ {
  294. var want []byte
  295. buf := new(bytes.Buffer)
  296. w := NewBufferedWriter(buf)
  297. for j, input := range inputs {
  298. if i&(1<<uint(j)) == 0 {
  299. continue
  300. }
  301. if _, err := w.Write(input); err != nil {
  302. t.Errorf("i=%#02x: j=%d: Write: %v", i, j, err)
  303. continue loop
  304. }
  305. want = append(want, input...)
  306. }
  307. if err := w.Close(); err != nil {
  308. t.Errorf("i=%#02x: Close: %v", i, err)
  309. continue
  310. }
  311. got, err := ioutil.ReadAll(NewReader(buf))
  312. if err != nil {
  313. t.Errorf("i=%#02x: ReadAll: %v", i, err)
  314. continue
  315. }
  316. if err := cmp(got, want); err != nil {
  317. t.Errorf("i=%#02x: %v", i, err)
  318. continue
  319. }
  320. }
  321. }
  322. func TestFlush(t *testing.T) {
  323. buf := new(bytes.Buffer)
  324. w := NewBufferedWriter(buf)
  325. defer w.Close()
  326. if _, err := w.Write(bytes.Repeat([]byte{'x'}, 20)); err != nil {
  327. t.Fatalf("Write: %v", err)
  328. }
  329. if n := buf.Len(); n != 0 {
  330. t.Fatalf("before Flush: %d bytes were written to the underlying io.Writer, want 0", n)
  331. }
  332. if err := w.Flush(); err != nil {
  333. t.Fatalf("Flush: %v", err)
  334. }
  335. if n := buf.Len(); n == 0 {
  336. t.Fatalf("after Flush: %d bytes were written to the underlying io.Writer, want non-0", n)
  337. }
  338. }
  339. func TestReaderReset(t *testing.T) {
  340. gold := bytes.Repeat([]byte("All that is gold does not glitter,\n"), 10000)
  341. buf := new(bytes.Buffer)
  342. if _, err := NewWriter(buf).Write(gold); err != nil {
  343. t.Fatalf("Write: %v", err)
  344. }
  345. encoded, invalid, partial := buf.String(), "invalid", "partial"
  346. r := NewReader(nil)
  347. for i, s := range []string{encoded, invalid, partial, encoded, partial, invalid, encoded, encoded} {
  348. if s == partial {
  349. r.Reset(strings.NewReader(encoded))
  350. if _, err := r.Read(make([]byte, 101)); err != nil {
  351. t.Errorf("#%d: %v", i, err)
  352. continue
  353. }
  354. continue
  355. }
  356. r.Reset(strings.NewReader(s))
  357. got, err := ioutil.ReadAll(r)
  358. switch s {
  359. case encoded:
  360. if err != nil {
  361. t.Errorf("#%d: %v", i, err)
  362. continue
  363. }
  364. if err := cmp(got, gold); err != nil {
  365. t.Errorf("#%d: %v", i, err)
  366. continue
  367. }
  368. case invalid:
  369. if err == nil {
  370. t.Errorf("#%d: got nil error, want non-nil", i)
  371. continue
  372. }
  373. }
  374. }
  375. }
  376. func TestWriterReset(t *testing.T) {
  377. gold := bytes.Repeat([]byte("Not all those who wander are lost;\n"), 10000)
  378. const n = 20
  379. for _, buffered := range []bool{false, true} {
  380. var w *Writer
  381. if buffered {
  382. w = NewBufferedWriter(nil)
  383. defer w.Close()
  384. } else {
  385. w = NewWriter(nil)
  386. }
  387. var gots, wants [][]byte
  388. failed := false
  389. for i := 0; i <= n; i++ {
  390. buf := new(bytes.Buffer)
  391. w.Reset(buf)
  392. want := gold[:len(gold)*i/n]
  393. if _, err := w.Write(want); err != nil {
  394. t.Errorf("#%d: Write: %v", i, err)
  395. failed = true
  396. continue
  397. }
  398. if buffered {
  399. if err := w.Flush(); err != nil {
  400. t.Errorf("#%d: Flush: %v", i, err)
  401. failed = true
  402. continue
  403. }
  404. }
  405. got, err := ioutil.ReadAll(NewReader(buf))
  406. if err != nil {
  407. t.Errorf("#%d: ReadAll: %v", i, err)
  408. failed = true
  409. continue
  410. }
  411. gots = append(gots, got)
  412. wants = append(wants, want)
  413. }
  414. if failed {
  415. continue
  416. }
  417. for i := range gots {
  418. if err := cmp(gots[i], wants[i]); err != nil {
  419. t.Errorf("#%d: %v", i, err)
  420. }
  421. }
  422. }
  423. }
  424. func TestWriterResetWithoutFlush(t *testing.T) {
  425. buf0 := new(bytes.Buffer)
  426. buf1 := new(bytes.Buffer)
  427. w := NewBufferedWriter(buf0)
  428. if _, err := w.Write([]byte("xxx")); err != nil {
  429. t.Fatalf("Write #0: %v", err)
  430. }
  431. // Note that we don't Flush the Writer before calling Reset.
  432. w.Reset(buf1)
  433. if _, err := w.Write([]byte("yyy")); err != nil {
  434. t.Fatalf("Write #1: %v", err)
  435. }
  436. if err := w.Flush(); err != nil {
  437. t.Fatalf("Flush: %v", err)
  438. }
  439. got, err := ioutil.ReadAll(NewReader(buf1))
  440. if err != nil {
  441. t.Fatalf("ReadAll: %v", err)
  442. }
  443. if err := cmp(got, []byte("yyy")); err != nil {
  444. t.Fatal(err)
  445. }
  446. }
  447. type writeCounter int
  448. func (c *writeCounter) Write(p []byte) (int, error) {
  449. *c++
  450. return len(p), nil
  451. }
  452. // TestNumUnderlyingWrites tests that each Writer flush only makes one or two
  453. // Write calls on its underlying io.Writer, depending on whether or not the
  454. // flushed buffer was compressible.
  455. func TestNumUnderlyingWrites(t *testing.T) {
  456. testCases := []struct {
  457. input []byte
  458. want int
  459. }{
  460. {bytes.Repeat([]byte{'x'}, 100), 1},
  461. {bytes.Repeat([]byte{'y'}, 100), 1},
  462. {[]byte("ABCDEFGHIJKLMNOPQRST"), 2},
  463. }
  464. var c writeCounter
  465. w := NewBufferedWriter(&c)
  466. defer w.Close()
  467. for i, tc := range testCases {
  468. c = 0
  469. if _, err := w.Write(tc.input); err != nil {
  470. t.Errorf("#%d: Write: %v", i, err)
  471. continue
  472. }
  473. if err := w.Flush(); err != nil {
  474. t.Errorf("#%d: Flush: %v", i, err)
  475. continue
  476. }
  477. if int(c) != tc.want {
  478. t.Errorf("#%d: got %d underlying writes, want %d", i, c, tc.want)
  479. continue
  480. }
  481. }
  482. }
  483. func benchDecode(b *testing.B, src []byte) {
  484. encoded := Encode(nil, src)
  485. // Bandwidth is in amount of uncompressed data.
  486. b.SetBytes(int64(len(src)))
  487. b.ResetTimer()
  488. for i := 0; i < b.N; i++ {
  489. Decode(src, encoded)
  490. }
  491. }
  492. func benchEncode(b *testing.B, src []byte) {
  493. // Bandwidth is in amount of uncompressed data.
  494. b.SetBytes(int64(len(src)))
  495. dst := make([]byte, MaxEncodedLen(len(src)))
  496. b.ResetTimer()
  497. for i := 0; i < b.N; i++ {
  498. Encode(dst, src)
  499. }
  500. }
  501. func readFile(b testing.TB, filename string) []byte {
  502. src, err := ioutil.ReadFile(filename)
  503. if err != nil {
  504. b.Skipf("skipping benchmark: %v", err)
  505. }
  506. if len(src) == 0 {
  507. b.Fatalf("%s has zero length", filename)
  508. }
  509. return src
  510. }
  511. // expand returns a slice of length n containing repeated copies of src.
  512. func expand(src []byte, n int) []byte {
  513. dst := make([]byte, n)
  514. for x := dst; len(x) > 0; {
  515. i := copy(x, src)
  516. x = x[i:]
  517. }
  518. return dst
  519. }
  520. func benchWords(b *testing.B, n int, decode bool) {
  521. // Note: the file is OS-language dependent so the resulting values are not
  522. // directly comparable for non-US-English OS installations.
  523. data := expand(readFile(b, "/usr/share/dict/words"), n)
  524. if decode {
  525. benchDecode(b, data)
  526. } else {
  527. benchEncode(b, data)
  528. }
  529. }
  530. func BenchmarkWordsDecode1e1(b *testing.B) { benchWords(b, 1e1, true) }
  531. func BenchmarkWordsDecode1e2(b *testing.B) { benchWords(b, 1e2, true) }
  532. func BenchmarkWordsDecode1e3(b *testing.B) { benchWords(b, 1e3, true) }
  533. func BenchmarkWordsDecode1e4(b *testing.B) { benchWords(b, 1e4, true) }
  534. func BenchmarkWordsDecode1e5(b *testing.B) { benchWords(b, 1e5, true) }
  535. func BenchmarkWordsDecode1e6(b *testing.B) { benchWords(b, 1e6, true) }
  536. func BenchmarkWordsEncode1e1(b *testing.B) { benchWords(b, 1e1, false) }
  537. func BenchmarkWordsEncode1e2(b *testing.B) { benchWords(b, 1e2, false) }
  538. func BenchmarkWordsEncode1e3(b *testing.B) { benchWords(b, 1e3, false) }
  539. func BenchmarkWordsEncode1e4(b *testing.B) { benchWords(b, 1e4, false) }
  540. func BenchmarkWordsEncode1e5(b *testing.B) { benchWords(b, 1e5, false) }
  541. func BenchmarkWordsEncode1e6(b *testing.B) { benchWords(b, 1e6, false) }
  542. func BenchmarkRandomEncode(b *testing.B) {
  543. rng := rand.New(rand.NewSource(1))
  544. data := make([]byte, 1<<20)
  545. for i := range data {
  546. data[i] = uint8(rng.Intn(256))
  547. }
  548. benchEncode(b, data)
  549. }
  550. // testFiles' values are copied directly from
  551. // https://raw.githubusercontent.com/google/snappy/master/snappy_unittest.cc
  552. // The label field is unused in snappy-go.
  553. //
  554. // If this list changes (due to the upstream C++ list changing), remember to
  555. // update the .gitignore file in this repository.
  556. var testFiles = []struct {
  557. label string
  558. filename string
  559. sizeLimit int
  560. }{
  561. {"html", "html", 0},
  562. {"urls", "urls.10K", 0},
  563. {"jpg", "fireworks.jpeg", 0},
  564. {"jpg_200", "fireworks.jpeg", 200},
  565. {"pdf", "paper-100k.pdf", 0},
  566. {"html4", "html_x_4", 0},
  567. {"txt1", "alice29.txt", 0},
  568. {"txt2", "asyoulik.txt", 0},
  569. {"txt3", "lcet10.txt", 0},
  570. {"txt4", "plrabn12.txt", 0},
  571. {"pb", "geo.protodata", 0},
  572. {"gaviota", "kppkn.gtb", 0},
  573. }
  574. // The test data files are present at this canonical URL.
  575. const baseURL = "https://raw.githubusercontent.com/google/snappy/master/testdata/"
  576. func downloadTestdata(b *testing.B, basename string) (errRet error) {
  577. filename := filepath.Join(*testdata, basename)
  578. if stat, err := os.Stat(filename); err == nil && stat.Size() != 0 {
  579. return nil
  580. }
  581. if !*download {
  582. b.Skipf("test data not found; skipping benchmark without the -download flag")
  583. }
  584. // Download the official snappy C++ implementation reference test data
  585. // files for benchmarking.
  586. if err := os.Mkdir(*testdata, 0777); err != nil && !os.IsExist(err) {
  587. return fmt.Errorf("failed to create testdata: %s", err)
  588. }
  589. f, err := os.Create(filename)
  590. if err != nil {
  591. return fmt.Errorf("failed to create %s: %s", filename, err)
  592. }
  593. defer f.Close()
  594. defer func() {
  595. if errRet != nil {
  596. os.Remove(filename)
  597. }
  598. }()
  599. url := baseURL + basename
  600. resp, err := http.Get(url)
  601. if err != nil {
  602. return fmt.Errorf("failed to download %s: %s", url, err)
  603. }
  604. defer resp.Body.Close()
  605. if s := resp.StatusCode; s != http.StatusOK {
  606. return fmt.Errorf("downloading %s: HTTP status code %d (%s)", url, s, http.StatusText(s))
  607. }
  608. _, err = io.Copy(f, resp.Body)
  609. if err != nil {
  610. return fmt.Errorf("failed to download %s to %s: %s", url, filename, err)
  611. }
  612. return nil
  613. }
  614. func benchFile(b *testing.B, n int, decode bool) {
  615. if err := downloadTestdata(b, testFiles[n].filename); err != nil {
  616. b.Fatalf("failed to download testdata: %s", err)
  617. }
  618. data := readFile(b, filepath.Join(*testdata, testFiles[n].filename))
  619. if n := testFiles[n].sizeLimit; 0 < n && n < len(data) {
  620. data = data[:n]
  621. }
  622. if decode {
  623. benchDecode(b, data)
  624. } else {
  625. benchEncode(b, data)
  626. }
  627. }
  628. // Naming convention is kept similar to what snappy's C++ implementation uses.
  629. func Benchmark_UFlat0(b *testing.B) { benchFile(b, 0, true) }
  630. func Benchmark_UFlat1(b *testing.B) { benchFile(b, 1, true) }
  631. func Benchmark_UFlat2(b *testing.B) { benchFile(b, 2, true) }
  632. func Benchmark_UFlat3(b *testing.B) { benchFile(b, 3, true) }
  633. func Benchmark_UFlat4(b *testing.B) { benchFile(b, 4, true) }
  634. func Benchmark_UFlat5(b *testing.B) { benchFile(b, 5, true) }
  635. func Benchmark_UFlat6(b *testing.B) { benchFile(b, 6, true) }
  636. func Benchmark_UFlat7(b *testing.B) { benchFile(b, 7, true) }
  637. func Benchmark_UFlat8(b *testing.B) { benchFile(b, 8, true) }
  638. func Benchmark_UFlat9(b *testing.B) { benchFile(b, 9, true) }
  639. func Benchmark_UFlat10(b *testing.B) { benchFile(b, 10, true) }
  640. func Benchmark_UFlat11(b *testing.B) { benchFile(b, 11, true) }
  641. func Benchmark_ZFlat0(b *testing.B) { benchFile(b, 0, false) }
  642. func Benchmark_ZFlat1(b *testing.B) { benchFile(b, 1, false) }
  643. func Benchmark_ZFlat2(b *testing.B) { benchFile(b, 2, false) }
  644. func Benchmark_ZFlat3(b *testing.B) { benchFile(b, 3, false) }
  645. func Benchmark_ZFlat4(b *testing.B) { benchFile(b, 4, false) }
  646. func Benchmark_ZFlat5(b *testing.B) { benchFile(b, 5, false) }
  647. func Benchmark_ZFlat6(b *testing.B) { benchFile(b, 6, false) }
  648. func Benchmark_ZFlat7(b *testing.B) { benchFile(b, 7, false) }
  649. func Benchmark_ZFlat8(b *testing.B) { benchFile(b, 8, false) }
  650. func Benchmark_ZFlat9(b *testing.B) { benchFile(b, 9, false) }
  651. func Benchmark_ZFlat10(b *testing.B) { benchFile(b, 10, false) }
  652. func Benchmark_ZFlat11(b *testing.B) { benchFile(b, 11, false) }