diff --git a/test/integration/logs/benchmark/benchmark_test.go b/test/integration/logs/benchmark/benchmark_test.go index 079c198a665..f7f38822dd4 100644 --- a/test/integration/logs/benchmark/benchmark_test.go +++ b/test/integration/logs/benchmark/benchmark_test.go @@ -74,14 +74,16 @@ func BenchmarkEncoding(b *testing.B) { state := klog.CaptureState() defer state.Restore() - var output bytesWritten + // To make the tests a bit more realistic, at + // least do system calls during each write. + output := newBytesWritten(b, "/dev/null") c := logsapi.NewLoggingConfiguration() c.Format = format o := logsapi.LoggingOptions{ - ErrorStream: &output, - InfoStream: &output, + ErrorStream: output, + InfoStream: output, } - klog.SetOutput(&output) + klog.SetOutput(output) defer func() { if err := logsapi.ResetForTest(nil); err != nil { b.Errorf("error resetting logsapi: %v", err) @@ -108,7 +110,7 @@ func BenchmarkEncoding(b *testing.B) { // Report messages/s instead of ns/op because "op" varies. b.ReportMetric(0, "ns/op") b.ReportMetric(float64(total)/duration.Seconds(), "msgs/s") - fileSizes[filepath.Base(b.Name())] = int(output) + fileSizes[filepath.Base(b.Name())] = int(output.bytesWritten) } b.Run("printf", func(b *testing.B) { diff --git a/test/integration/logs/benchmark/common_test.go b/test/integration/logs/benchmark/common_test.go index 76d851a44ce..a6c4ace4b0a 100644 --- a/test/integration/logs/benchmark/common_test.go +++ b/test/integration/logs/benchmark/common_test.go @@ -18,6 +18,8 @@ package benchmark import ( "flag" + "os" + "testing" "k8s.io/klog/v2" ) @@ -34,12 +36,25 @@ func init() { flag.Set("stderrthreshold", "FATAL") } -type bytesWritten int64 +func newBytesWritten(tb testing.TB, filename string) *bytesWritten { + out, err := os.Create(filename) + if err != nil { + tb.Fatalf("open fake output: %v", err) + } + tb.Cleanup(func() { _ = out.Close() }) + return &bytesWritten{ + out: out, + } +} + +type bytesWritten struct { + out *os.File + bytesWritten int64 +} func (b *bytesWritten) Write(data []byte) (int, error) { - l := len(data) - *b += bytesWritten(l) - return l, nil + b.bytesWritten += int64(len(data)) + return b.out.Write(data) } func printf(item logMessage) {