diff --git a/reader_test.go b/reader_test.go index a14f229..a476074 100644 --- a/reader_test.go +++ b/reader_test.go @@ -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) + } + } +}