Skip to content

Commit

Permalink
tests: [zstd] Factor out common benchmark routine (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMula committed Mar 10, 2022
1 parent 28fb801 commit 2d315d3
Showing 1 changed file with 12 additions and 39 deletions.
51 changes: 12 additions & 39 deletions zstd/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1427,53 +1427,18 @@ func BenchmarkDecoder_DecodeAllParallel(b *testing.B) {
}
}

func BenchmarkDecoderSilesia(b *testing.B) {
fn := "testdata/silesia.tar.zst"
data, err := ioutil.ReadFile(fn)
func benchmarkDecoderWithFile(path string, b *testing.B) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
b.Skip("Missing testdata/silesia.tar.zst")
b.Skipf("Missing %s", path)
return
}
b.Fatal(err)
}
dec, err := NewReader(nil, WithDecoderLowmem(false))
if err != nil {
b.Fatal(err)
}
defer dec.Close()
err = dec.Reset(bytes.NewBuffer(data))
if err != nil {
b.Fatal(err)
}
n, err := io.Copy(ioutil.Discard, dec)
if err != nil {
b.Fatal(err)
}

b.SetBytes(n)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = dec.Reset(bytes.NewBuffer(data))
if err != nil {
b.Fatal(err)
}
_, err := io.CopyN(ioutil.Discard, dec, n)
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkDecoderEnwik9(b *testing.B) {
fn := "testdata/enwik8.zst"
data, err := ioutil.ReadFile(fn)
data, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
b.Skip("Missing " + fn)
return
}
b.Fatal(err)
}
dec, err := NewReader(nil, WithDecoderLowmem(false))
Expand Down Expand Up @@ -1505,6 +1470,14 @@ func BenchmarkDecoderEnwik9(b *testing.B) {
}
}

func BenchmarkDecoderSilesia(b *testing.B) {
benchmarkDecoderWithFile("testdata/silesia.tar.zst", b)
}

func BenchmarkDecoderEnwik9(b *testing.B) {
benchmarkDecoderWithFile("testdata/enwik9.zst", b)
}

func testDecoderDecodeAll(t *testing.T, fn string, dec *Decoder) {
data, err := ioutil.ReadFile(fn)
if err != nil {
Expand Down

0 comments on commit 2d315d3

Please sign in to comment.