mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 20:17:41 +00:00
logs benchmark: really write through pipe
While the benchmark is focused on encoding, it becomes a bit more realistic when actually passing the encoded data to the Linux kernel. Features like output buffering are more likely to have a visible effect when invoking syscalls.
This commit is contained in:
parent
7f1a30f8d5
commit
04b772c661
@ -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) {
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user