Skip to content

Commit

Permalink
xz: add reader benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ulikunitz committed Dec 10, 2022
1 parent 5535077 commit 0b7c695
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,43 @@ func TestCheckNone(t *testing.T) {
t.Fatalf("io.Copy error %s", err)
}
}

func BenchmarkReader(b *testing.B) {
const testFile = "testdata/enwik7"
data, err := os.ReadFile(testFile)
if err != nil {
b.Fatalf("os.ReadFile(%q) error %s", testFile, err)
}
buf := new(bytes.Buffer)
uncompressedLen := int64(len(data))
b.SetBytes(int64(uncompressedLen))
b.ReportAllocs()
buf.Reset()
w, err := NewWriter(buf)
if err != nil {
b.Fatalf("NewWriter(buf) error %s", err)
}
if _, err = w.Write(data); err != nil {
b.Fatalf("w.Write(data) error %s", err)
}
if err = w.Close(); err != nil {
b.Fatalf("w.Write(data)")
}
data = make([]byte, buf.Len())
copy(data, buf.Bytes())
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
r, err := NewReader(bytes.NewReader(data))
if err != nil {
b.Fatalf("NewReader(data) error %s", err)
}
n, err := io.Copy(buf, r)
if err != nil {
b.Fatalf("io.Copy(buf, r) error %s", err)
}
if n != uncompressedLen {
b.Fatalf("io.Copy got %d; want %d", n, uncompressedLen)
}
}
}

0 comments on commit 0b7c695

Please sign in to comment.