39 lines
1.0 KiB
Go
39 lines
1.0 KiB
Go
package gotool
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
const performanceDiagnosticSize = 8 << 20
|
|
|
|
func BenchmarkPerformanceCombinedOutput(b *testing.B) {
|
|
for _, testCase := range []struct {
|
|
name string
|
|
size int
|
|
}{
|
|
{name: "modest", size: 4 << 10},
|
|
{name: "diagnostic-8MiB", size: performanceDiagnosticSize},
|
|
} {
|
|
input := bytes.Repeat([]byte("go tool diagnostic output\n"), (testCase.size+25)/26)
|
|
input = input[:testCase.size]
|
|
b.Run(testCase.name+"/discard-forwarding", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(input)))
|
|
b.ResetTimer()
|
|
b.ReportMetric(float64(len(input)), "bytes/op")
|
|
for i := 0; i < b.N; i++ {
|
|
writer := &combinedWriter{output: io.Discard}
|
|
if written, err := writer.Write(input); err != nil || written != len(input) {
|
|
b.Fatalf("write captured output = %d, err %v; want %d bytes", written, err, len(input))
|
|
}
|
|
captured := writer.Bytes()
|
|
if len(captured) != len(input) {
|
|
b.Fatalf("captured output = %d bytes, want %d", len(captured), len(input))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|