forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #322 from pohly/kobjs-performance
replace KObjs with KObjSlice
- Loading branch information
Showing
5 changed files
with
301 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
Copyright 2022 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 benchmarks | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io" | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/go-logr/zapr" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
const ( | ||
verbosityThreshold = 10 | ||
) | ||
|
||
func init() { | ||
// klog gets configured so that it writes to a single output file that | ||
// will be set during tests with SetOutput. | ||
klog.InitFlags(nil) | ||
flag.Set("v", fmt.Sprintf("%d", verbosityThreshold)) | ||
flag.Set("log_file", "/dev/null") | ||
flag.Set("logtostderr", "false") | ||
flag.Set("alsologtostderr", "false") | ||
flag.Set("stderrthreshold", "10") | ||
} | ||
|
||
type testcase struct { | ||
name string | ||
generate func() interface{} | ||
} | ||
|
||
func BenchmarkOutput(b *testing.B) { | ||
// We'll run each benchmark for different output formatting. | ||
configs := map[string]struct { | ||
init, cleanup func() | ||
}{ | ||
"klog": { | ||
init: func() { klog.SetOutput(discard{}) }, | ||
}, | ||
"zapr": { | ||
init: func() { klog.SetLogger(newZaprLogger()) }, | ||
cleanup: func() { klog.ClearLogger() }, | ||
}, | ||
} | ||
|
||
// Each benchmark tests formatting of one key/value pair, with | ||
// different values. The order is relevant here. | ||
var tests []testcase | ||
for length := 0; length <= 100; length += 10 { | ||
arg := make([]interface{}, length) | ||
for i := 0; i < length; i++ { | ||
arg[i] = KMetadataMock{Name: "a", NS: "a"} | ||
} | ||
tests = append(tests, testcase{ | ||
name: fmt.Sprintf("objects/%d", length), | ||
generate: func() interface{} { | ||
return klog.KObjSlice(arg) | ||
}, | ||
}) | ||
} | ||
|
||
// Verbosity checks may influence the result. | ||
verbosity := map[string]func(value interface{}){ | ||
"no-verbosity-check": func(value interface{}) { | ||
klog.InfoS("test", "key", value) | ||
}, | ||
"pass-verbosity-check": func(value interface{}) { | ||
klog.V(verbosityThreshold).InfoS("test", "key", value) | ||
}, | ||
"fail-verbosity-check": func(value interface{}) { | ||
klog.V(verbosityThreshold+1).InfoS("test", "key", value) | ||
}, | ||
} | ||
|
||
for name, config := range configs { | ||
b.Run(name, func(b *testing.B) { | ||
if config.cleanup != nil { | ||
defer config.cleanup() | ||
} | ||
config.init() | ||
|
||
for name, logCall := range verbosity { | ||
b.Run(name, func(b *testing.B) { | ||
for _, testcase := range tests { | ||
b.Run(testcase.name, func(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
logCall(testcase.generate()) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func newZaprLogger() logr.Logger { | ||
encoderConfig := &zapcore.EncoderConfig{ | ||
MessageKey: "msg", | ||
CallerKey: "caller", | ||
NameKey: "logger", | ||
EncodeDuration: zapcore.StringDurationEncoder, | ||
EncodeCaller: zapcore.ShortCallerEncoder, | ||
} | ||
encoder := zapcore.NewJSONEncoder(*encoderConfig) | ||
zapV := -zapcore.Level(verbosityThreshold) | ||
core := zapcore.NewCore(encoder, zapcore.AddSync(discard{}), zapV) | ||
l := zap.New(core, zap.WithCaller(true)) | ||
logger := zapr.NewLoggerWithOptions(l, zapr.LogInfoLevel("v"), zapr.ErrorKey("err")) | ||
return logger | ||
} | ||
|
||
type KMetadataMock struct { | ||
Name, NS string | ||
} | ||
|
||
func (m KMetadataMock) GetName() string { | ||
return m.Name | ||
} | ||
func (m KMetadataMock) GetNamespace() string { | ||
return m.NS | ||
} | ||
|
||
type discard struct{} | ||
|
||
var _ io.Writer = discard{} | ||
|
||
func (discard) Write(p []byte) (int, error) { | ||
return len(p), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters