Files
kubernetes/test/integration/logs/benchmark/common_test.go
Patrick Ohly 9a867c555c logs: benchmark write performance
The recent regression https://github.com/kubernetes/kubernetes/issues/107033
shows that we need a way to automatically measure different logging
configurations (structured text, JSON with and without split streams) under
realistic conditions (time stamping, caller identification).

System calls may affect the performance and thus writing into actual files is
useful. A temp dir under /tmp (usually a tmpfs) is used, so the actual IO
bandwidth shouldn't affect the outcome. The "normal" json.Factory code is used
to construct the JSON logger when we have actual files that can be set as
os.Stderr and os.Stdout, thus making this as realistic as possible.

When discarding the output instead of writing it, the focus is more on the rest
of the pipeline and changes there can be investigated more reliably.

The benchmarks automatically gather "log entries per second" and "bytes per
second", which is useful to know when considering requirements like the ones
from https://github.com/kubernetes/kubernetes/issues/107029.
2022-01-11 09:56:22 +01:00

89 lines
2.1 KiB
Go

/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package benchmark
import (
"flag"
"io"
"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"
logsjson "k8s.io/component-base/logs/json"
"k8s.io/klog/v2"
)
func init() {
// Cause all klog output to be discarded with minimal overhead.
// We don't include time stamps and caller information.
// Individual tests can change that by calling flag.Set again,
// but should always restore this state here.
klog.InitFlags(nil)
flag.Set("alsologtostderr", "false")
flag.Set("logtostderr", "false")
flag.Set("skip_headers", "true")
flag.Set("one_output", "true")
flag.Set("stderrthreshold", "FATAL")
klog.SetOutput(&output)
}
type bytesWritten int64
func (b *bytesWritten) Write(data []byte) (int, error) {
l := len(data)
*b += bytesWritten(l)
return l, nil
}
func (b *bytesWritten) Sync() error {
return nil
}
var output bytesWritten
var jsonLogger = newJSONLogger(&output)
func newJSONLogger(out io.Writer) logr.Logger {
encoderConfig := &zapcore.EncoderConfig{
MessageKey: "msg",
}
logger, _ := logsjson.NewJSONLogger(zapcore.AddSync(out), nil, encoderConfig)
return logger
}
func printf(item logMessage) {
if item.isError {
klog.Errorf("%s: %v %s", item.msg, item.err, item.kvs)
} else {
klog.Infof("%s: %v", item.msg, item.kvs)
}
}
// These variables are a workaround for logcheck complaining about the dynamic
// parameters.
var (
errorS = klog.ErrorS
infoS = klog.InfoS
)
func prints(item logMessage) {
if item.isError {
errorS(item.err, item.msg, item.kvs...)
} else {
infoS(item.msg, item.kvs...)
}
}