1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
|
package huff0
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/klauspost/compress/flate"
"github.com/klauspost/compress/zip"
)
type inputFn func() ([]byte, error)
var testfiles = []struct {
name string
fn inputFn
err1X error
err4X error
}{
// Digits is the digits of the irrational number e. Its decimal representation
// does not repeat, but there are only 10 possible digits, so it should be
// reasonably compressible.
{name: "digits", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/e.txt") }},
// gettysburg.txt is a small plain text.
{name: "gettysburg", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/gettysburg.txt") }},
// Twain is Project Gutenberg's edition of Mark Twain's classic English novel.
{name: "twain", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/Mark.Twain-Tom.Sawyer.txt") }},
// Random bytes
{name: "random", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/sharnd.out") }, err1X: ErrIncompressible, err4X: ErrIncompressible},
// Low entropy
{name: "low-ent.10k", fn: func() ([]byte, error) { return []byte(strings.Repeat("1221", 10000)), nil }},
// Super Low entropy
{name: "superlow-ent-10k", fn: func() ([]byte, error) { return []byte(strings.Repeat("1", 10000) + strings.Repeat("2", 500)), nil }},
// Zero bytes
{name: "zeroes", fn: func() ([]byte, error) { return make([]byte, 10000), nil }, err1X: ErrUseRLE, err4X: ErrUseRLE},
{name: "crash1", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/crash1.bin") }, err1X: ErrIncompressible, err4X: ErrIncompressible},
{name: "crash2", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/crash2.bin") }, err4X: ErrIncompressible},
{name: "crash3", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/crash3.bin") }, err1X: ErrIncompressible, err4X: ErrIncompressible},
{name: "endzerobits", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/endzerobits.bin") }, err1X: nil, err4X: ErrIncompressible},
{name: "endnonzero", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/endnonzero.bin") }, err4X: ErrIncompressible},
{name: "case1", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/case1.bin") }, err1X: nil},
{name: "case2", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/case2.bin") }, err1X: nil},
{name: "case3", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/case3.bin") }, err1X: nil},
{name: "pngdata.001", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/pngdata.bin") }, err1X: nil},
{name: "normcount2", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/normcount2.bin") }, err1X: nil},
}
type fuzzInput struct {
name string
fn inputFn
}
// testfilesExtended is used for regression testing the decoder.
// These files are expected to fail, but not crash
var testfilesExtended []fuzzInput
func init() {
data, err := ioutil.ReadFile("testdata/regression.zip")
if err != nil {
panic(err)
}
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
panic(err)
}
for _, tt := range zr.File {
if tt.UncompressedSize64 == 0 {
continue
}
rc, err := tt.Open()
if err != nil {
panic(err)
}
b, err := ioutil.ReadAll(rc)
if err != nil {
panic(err)
}
testfilesExtended = append(testfilesExtended, fuzzInput{
name: filepath.Base(tt.Name),
fn: func() ([]byte, error) {
return b, nil
},
})
}
}
func TestCompressRegression(t *testing.T) {
// Match the fuzz function
var testInput = func(t *testing.T, data []byte) int {
var enc Scratch
enc.WantLogLess = 5
comp, _, err := Compress1X(data, &enc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
if len(comp) >= len(data)-len(data)>>enc.WantLogLess {
panic(fmt.Errorf("too large output provided. got %d, but should be < %d", len(comp), len(data)-len(data)>>enc.WantLogLess))
}
dec, remain, err := ReadTable(comp, nil)
if err != nil {
panic(err)
}
out, err := dec.Decompress1X(remain)
if err != nil {
t.Error(err)
}
if !bytes.Equal(out, data) {
t.Error("decompression 1x mismatch")
}
// Reuse as 4X
enc.Reuse = ReusePolicyAllow
comp, reUsed, err := Compress4X(data, &enc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
if len(comp) >= len(data)-len(data)>>enc.WantLogLess {
panic(fmt.Errorf("too large output provided. got %d, but should be < %d", len(comp), len(data)-len(data)>>enc.WantLogLess))
}
remain = comp
if !reUsed {
dec, remain, err = ReadTable(comp, dec)
if err != nil {
panic(err)
}
}
out, err = dec.Decompress4X(remain, len(data))
if err != nil {
t.Error(err)
}
if !bytes.Equal(out, data) {
t.Error("decompression 4x with reuse mismatch")
}
enc.Reuse = ReusePolicyNone
comp, reUsed, err = Compress4X(data, &enc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
if reUsed {
panic("reused when asked not to")
}
if len(comp) >= len(data)-len(data)>>enc.WantLogLess {
panic(fmt.Errorf("too large output provided. got %d, but should be < %d", len(comp), len(data)-len(data)>>enc.WantLogLess))
}
dec, remain, err = ReadTable(comp, dec)
if err != nil {
panic(err)
}
out, err = dec.Decompress4X(remain, len(data))
if err != nil {
t.Error(err)
}
if !bytes.Equal(out, data) {
t.Error("decompression 4x mismatch")
}
// Reuse as 1X
dec.Reuse = ReusePolicyAllow
comp, reUsed, err = Compress1X(data, &enc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
if len(comp) >= len(data)-len(data)>>enc.WantLogLess {
panic(fmt.Errorf("too large output provided. got %d, but should be < %d", len(comp), len(data)-len(data)>>enc.WantLogLess))
}
remain = comp
if !reUsed {
dec, remain, err = ReadTable(comp, dec)
if err != nil {
panic(err)
}
}
out, err = dec.Decompress1X(remain)
if err != nil {
t.Error(err)
}
if !bytes.Equal(out, data) {
t.Error("decompression 1x with reuse mismatch")
}
return 1
}
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
testInput(t, buf0)
})
}
for _, test := range testfilesExtended {
t.Run(test.name, func(t *testing.T) {
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
testInput(t, buf0)
})
}
}
func TestCompress1X(t *testing.T) {
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
var s Scratch
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
b, re, err := Compress1X(buf0, &s)
if err != test.err1X {
t.Errorf("want error %v (%T), got %v (%T)", test.err1X, test.err1X, err, err)
}
if err != nil {
t.Log(test.name, err.Error())
return
}
if b == nil {
t.Error("got no output")
return
}
min := s.minSize(len(buf0))
if len(s.OutData) < min {
t.Errorf("output data length (%d) below shannon limit (%d)", len(s.OutData), min)
}
if len(s.OutTable) == 0 {
t.Error("got no table definition")
}
if re {
t.Error("claimed to have re-used.")
}
if len(s.OutData) == 0 {
t.Error("got no data output")
}
t.Logf("%s: %d -> %d bytes (%.2f:1) re:%t (table: %d bytes)", test.name, len(buf0), len(b), float64(len(buf0))/float64(len(b)), re, len(s.OutTable))
s.Out = nil
bRe, _, err := Compress1X(b, &s)
if err == nil {
t.Log("Could re-compress to", len(bRe))
}
})
}
}
func TestCompress1XMustReuse(t *testing.T) {
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
var s Scratch
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
b, re, err := Compress1X(buf0, &s)
if err != test.err1X {
t.Errorf("want error %v (%T), got %v (%T)", test.err1X, test.err1X, err, err)
}
if err != nil {
t.Log(test.name, err.Error())
return
}
if b == nil {
t.Error("got no output")
return
}
min := s.minSize(len(buf0))
if len(s.OutData) < min {
t.Errorf("output data length (%d) below shannon limit (%d)", len(s.OutData), min)
}
if len(s.OutTable) == 0 {
t.Error("got no table definition")
}
if re {
t.Error("claimed to have re-used.")
}
if len(s.OutData) == 0 {
t.Error("got no data output")
}
t.Logf("%s: %d -> %d bytes (%.2f:1) re:%t (table: %d bytes)", test.name, len(buf0), len(b), float64(len(buf0))/float64(len(b)), re, len(s.OutTable))
table := s.OutTable
prevTable := s.prevTable
for i, v := range prevTable {
// Clear unused sections for comparison
if v.nBits == 0 {
prevTable[i].val = 0
}
}
b = s.OutData
actl := s.actualTableLog
// Use only the table data to recompress.
s = Scratch{}
s2 := &s
s.Reuse = ReusePolicyMust
s2, _, err = ReadTable(table, s2)
if err != nil {
t.Error("Could not read table", err)
return
}
if !reflect.DeepEqual(prevTable, s2.prevTable) {
t.Errorf("prevtable mismatch.\ngot %v\nwant %v", s2.prevTable, prevTable)
}
if actl != s.actualTableLog {
t.Errorf("tablelog mismatch, want %d, got %d", actl, s.actualTableLog)
}
b2, reused, err := Compress1X(buf0, s2)
if err != nil {
t.Error("Could not re-compress with prev table", err)
}
if !reused {
t.Error("didn't reuse...")
return
}
if len(b2) != len(b) {
t.Errorf("recompressed to different size, want %d, got %d", len(b), len(b2))
return
}
if !bytes.Equal(b, b2) {
for i := range b {
if b[i] != b2[i] {
t.Errorf("recompressed to different output. First mismatch at byte %d, (want %x != got %x)", i, b[i], b2[i])
return
}
}
}
})
}
}
func TestCompress4X(t *testing.T) {
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
var s Scratch
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
b, re, err := Compress4X(buf0, &s)
if err != test.err4X {
t.Errorf("want error %v (%T), got %v (%T)", test.err1X, test.err4X, err, err)
}
if err != nil {
t.Log(test.name, err.Error())
return
}
if b == nil {
t.Error("got no output")
return
}
if len(s.OutTable) == 0 {
t.Error("got no table definition")
}
if re {
t.Error("claimed to have re-used.")
}
if len(s.OutData) == 0 {
t.Error("got no data output")
}
t.Logf("%s: %d -> %d bytes (%.2f:1) %t (table: %d bytes)", test.name, len(buf0), len(b), float64(len(buf0))/float64(len(b)), re, len(s.OutTable))
})
}
}
func TestCompress4XReuse(t *testing.T) {
rng := rand.NewSource(0x1337)
var s Scratch
s.Reuse = ReusePolicyAllow
for i := 0; i < 255; i++ {
if testing.Short() && i > 10 {
break
}
t.Run(fmt.Sprint("test-", i), func(t *testing.T) {
buf0 := make([]byte, BlockSizeMax)
for j := range buf0 {
buf0[j] = byte(int64(i) + (rng.Int63() & 3))
}
b, re, err := Compress4X(buf0, &s)
if err != nil {
t.Fatal(err)
}
if b == nil {
t.Error("got no output")
return
}
if len(s.OutData) == 0 {
t.Error("got no data output")
}
if re {
t.Error("claimed to have re-used. Unlikely.")
}
t.Logf("%s: %d -> %d bytes (%.2f:1) %t (table: %d bytes)", t.Name(), len(buf0), len(b), float64(len(buf0))/float64(len(b)), re, len(s.OutTable))
})
}
}
func TestCompress4XReuseActually(t *testing.T) {
rng := rand.NewSource(0x1337)
var s Scratch
s.Reuse = ReusePolicyAllow
for i := 0; i < 255; i++ {
if testing.Short() && i > 10 {
break
}
t.Run(fmt.Sprint("test-", i), func(t *testing.T) {
buf0 := make([]byte, BlockSizeMax)
for j := range buf0 {
buf0[j] = byte(rng.Int63() & 7)
}
b, re, err := Compress4X(buf0, &s)
if err != nil {
t.Fatal(err)
}
if b == nil {
t.Error("got no output")
return
}
if len(s.OutData) == 0 {
t.Error("got no data output")
}
if re && i == 0 {
t.Error("Claimed to have re-used on first loop.")
}
if !re && i > 0 {
t.Error("Expected table to be reused")
}
t.Logf("%s: %d -> %d bytes (%.2f:1) %t (table: %d bytes)", t.Name(), len(buf0), len(b), float64(len(buf0))/float64(len(b)), re, len(s.OutTable))
})
}
}
func TestCompress1XReuse(t *testing.T) {
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
var s Scratch
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
b, re, err := Compress1X(buf0, &s)
if err != test.err1X {
t.Errorf("want error %v (%T), got %v (%T)", test.err1X, test.err1X, err, err)
}
if err != nil {
t.Log(test.name, err.Error())
return
}
if b == nil {
t.Error("got no output")
return
}
firstData := len(s.OutData)
s.Reuse = ReusePolicyAllow
b, re, err = Compress1X(buf0, &s)
if err != nil {
t.Errorf("got secondary error %v (%T)", err, err)
return
}
if !re {
t.Error("Didn't re-use even if data was the same")
}
if len(s.OutTable) != 0 {
t.Error("got table definition, don't want any")
}
if len(s.OutData) == 0 {
t.Error("got no data output")
}
if len(b) != firstData {
t.Errorf("data length did not match first: %d, second:%d", firstData, len(b))
}
t.Logf("%s: %d -> %d bytes (%.2f:1) %t", test.name, len(buf0), len(b), float64(len(buf0))/float64(len(b)), re)
})
}
}
func BenchmarkDeflate(b *testing.B) {
for _, tt := range testfiles {
test := tt
if test.err1X != nil {
continue
}
b.Run(test.name, func(b *testing.B) {
dec, err := flate.NewWriter(ioutil.Discard, flate.HuffmanOnly)
if err != nil {
b.Fatal(err)
}
if test.err1X != nil {
b.Skip("skipping")
}
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
dec.Reset(ioutil.Discard)
n, err := dec.Write(buf0)
if err != nil {
b.Fatal(err)
}
if n != len(buf0) {
b.Fatal("mismatch", n, len(buf0))
}
dec.Close()
}
})
}
}
func BenchmarkCompress1XReuseNone(b *testing.B) {
for _, tt := range testfiles {
test := tt
if test.err1X != nil {
continue
}
b.Run(test.name, func(b *testing.B) {
var s Scratch
s.Reuse = ReusePolicyNone
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
_, re, err := Compress1X(buf0, &s)
if err != test.err1X {
b.Fatal("unexpected error:", err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
_, re, _ = Compress1X(buf0, &s)
if re {
b.Fatal("reused")
}
}
})
}
}
func BenchmarkCompress1XReuseAllow(b *testing.B) {
for _, tt := range testfiles {
test := tt
if test.err1X != nil {
continue
}
b.Run(test.name, func(b *testing.B) {
var s Scratch
s.Reuse = ReusePolicyAllow
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
_, re, err := Compress1X(buf0, &s)
if err != test.err1X {
b.Fatal("unexpected error:", err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
_, re, _ = Compress1X(buf0, &s)
if !re {
b.Fatal("not reused")
}
}
})
}
}
func BenchmarkCompress1XReusePrefer(b *testing.B) {
for _, tt := range testfiles {
test := tt
if test.err1X != nil {
continue
}
b.Run(test.name, func(b *testing.B) {
var s Scratch
s.Reuse = ReusePolicyPrefer
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
_, re, err := Compress1X(buf0, &s)
if err != test.err1X {
b.Fatal("unexpected error:", err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
_, re, _ = Compress1X(buf0, &s)
if !re {
b.Fatal("not reused")
}
}
})
}
}
func BenchmarkCompress4XReuseNone(b *testing.B) {
for _, tt := range testfiles {
test := tt
if test.err4X != nil {
continue
}
b.Run(test.name, func(b *testing.B) {
var s Scratch
s.Reuse = ReusePolicyNone
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
_, re, err := Compress4X(buf0, &s)
if err != test.err1X {
b.Fatal("unexpected error:", err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
_, re, _ = Compress4X(buf0, &s)
if re {
b.Fatal("reused")
}
}
})
}
}
func BenchmarkCompress4XReuseAllow(b *testing.B) {
for _, tt := range testfiles {
test := tt
if test.err4X != nil {
continue
}
b.Run(test.name, func(b *testing.B) {
var s Scratch
s.Reuse = ReusePolicyAllow
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
_, re, err := Compress4X(buf0, &s)
if err != test.err1X {
b.Fatal("unexpected error:", err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
_, re, _ = Compress4X(buf0, &s)
if !re {
b.Fatal("not reused")
}
}
})
}
}
func BenchmarkCompress4XReusePrefer(b *testing.B) {
for _, tt := range testfiles {
test := tt
if test.err4X != nil {
continue
}
b.Run(test.name, func(b *testing.B) {
var s Scratch
s.Reuse = ReusePolicyPrefer
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
if len(buf0) > BlockSizeMax {
buf0 = buf0[:BlockSizeMax]
}
_, re, err := Compress4X(buf0, &s)
if err != test.err4X {
b.Fatal("unexpected error:", err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
_, re, _ = Compress4X(buf0, &s)
if !re {
b.Fatal("not reused")
}
}
})
}
}
func BenchmarkCompress1XSizes(b *testing.B) {
test := testfiles[0]
sizes := []int{1e2, 2e2, 5e2, 1e3, 5e3, 1e4, 5e4}
for _, size := range sizes {
b.Run(test.name+"-"+fmt.Sprint(size), func(b *testing.B) {
var s Scratch
s.Reuse = ReusePolicyNone
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
buf0 = buf0[:size]
_, re, err := Compress1X(buf0, &s)
if err != test.err1X {
b.Fatal("unexpected error:", err)
}
//b.Log("Size:", len(o))
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
_, re, _ = Compress1X(buf0, &s)
if re {
b.Fatal("reused")
}
}
})
}
}
func BenchmarkCompress4XSizes(b *testing.B) {
test := testfiles[0]
sizes := []int{1e2, 2e2, 5e2, 1e3, 5e3, 1e4, 5e4}
for _, size := range sizes {
b.Run(test.name+"-"+fmt.Sprint(size), func(b *testing.B) {
var s Scratch
s.Reuse = ReusePolicyNone
buf0, err := test.fn()
if err != nil {
b.Fatal(err)
}
buf0 = buf0[:size]
_, re, err := Compress4X(buf0, &s)
if err != test.err1X {
b.Fatal("unexpected error:", err)
}
//b.Log("Size:", len(o))
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(buf0)))
for i := 0; i < b.N; i++ {
_, re, _ = Compress4X(buf0, &s)
if re {
b.Fatal("reused")
}
}
})
}
}
|