| 313 | func (nopWriteCloser) Close() error { return nil } |
| 314 | |
| 315 | func BenchmarkCompression(b *testing.B) { |
| 316 | benchmarks := []struct { |
| 317 | codec pkg.Codec |
| 318 | function func(*testing.B, pkg.Codec, *bytes.Buffer, []byte) float64 |
| 319 | }{ |
| 320 | { |
| 321 | codec: &noopCodec{}, |
| 322 | function: benchmarkCompression, |
| 323 | }, |
| 324 | { |
| 325 | codec: new(gzip.Codec), |
| 326 | function: benchmarkCompression, |
| 327 | }, |
| 328 | { |
| 329 | codec: new(snappy.Codec), |
| 330 | function: benchmarkCompression, |
| 331 | }, |
| 332 | { |
| 333 | codec: new(lz4.Codec), |
| 334 | function: benchmarkCompression, |
| 335 | }, |
| 336 | { |
| 337 | codec: new(zstd.Codec), |
| 338 | function: benchmarkCompression, |
| 339 | }, |
| 340 | } |
| 341 | |
| 342 | f, err := os.Open(filepath.Join(os.Getenv("GOROOT"), "src/encoding/json/testdata/code.json.gz")) |
| 343 | if err != nil { |
| 344 | b.Fatal(err) |
| 345 | } |
| 346 | defer f.Close() |
| 347 | |
| 348 | z, err := gz.NewReader(f) |
| 349 | if err != nil { |
| 350 | b.Fatal(err) |
| 351 | } |
| 352 | |
| 353 | payload, err := ioutil.ReadAll(z) |
| 354 | if err != nil { |
| 355 | b.Fatal(err) |
| 356 | } |
| 357 | |
| 358 | buffer := bytes.Buffer{} |
| 359 | buffer.Grow(len(payload)) |
| 360 | |
| 361 | ts := &bytes.Buffer{} |
| 362 | tw := tabwriter.NewWriter(ts, 0, 8, 0, '\t', 0) |
| 363 | defer func() { |
| 364 | tw.Flush() |
| 365 | fmt.Printf("input => %.2f MB\n", float64(len(payload))/(1024*1024)) |
| 366 | fmt.Println(ts) |
| 367 | }() |
| 368 | |
| 369 | for i := range benchmarks { |
| 370 | benchmark := &benchmarks[i] |
| 371 | ratio := 0.0 |
| 372 | |