package git import ( "context" "runtime" "testing" ) func BenchmarkPerformanceBlobReader(b *testing.B) { fixture := newRemoteFixture(b) objectID := gitText(b, fixture.work, "rev-parse", "HEAD:source.txt") const expectedBytes = int64(len("candidate")) b.Run("new-session-per-read", func(b *testing.B) { b.ReportAllocs() b.SetBytes(expectedBytes) b.ResetTimer() b.ReportMetric(float64(expectedBytes), "bytes/op") b.ReportMetric(1, "blobs/op") for i := 0; i < b.N; i++ { reader := NewBlobReader(context.Background(), fixture.work) contents, err := reader.Read(context.Background(), objectID) if err != nil { b.Fatal(err) } if int64(len(contents)) != expectedBytes { b.Fatalf("read %d bytes, want %d", len(contents), expectedBytes) } if err := reader.Close(); err != nil { b.Fatal(err) } runtime.KeepAlive(contents) } }) b.Run("shared-session", func(b *testing.B) { reader := NewBlobReader(context.Background(), fixture.work) b.Cleanup(func() { if err := reader.Close(); err != nil { b.Error(err) } }) b.ReportAllocs() b.SetBytes(expectedBytes) b.ResetTimer() b.ReportMetric(float64(expectedBytes), "bytes/op") b.ReportMetric(1, "blobs/op") for i := 0; i < b.N; i++ { contents, err := reader.Read(context.Background(), objectID) if err != nil { b.Fatal(err) } if int64(len(contents)) != expectedBytes { b.Fatalf("read %d bytes, want %d", len(contents), expectedBytes) } runtime.KeepAlive(contents) } }) }