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