From a9a2346e194e960abce7f52163cb449a5610e2bf Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Sat, 5 Jun 2021 11:40:32 +0200 Subject: [PATCH] Cleanup json logging benchmarks * Write logs to sink instead of stderr * Remove json marshall tests --- .../logs/json/json_benchmark_test.go | 83 ++++++------------- 1 file changed, 24 insertions(+), 59 deletions(-) diff --git a/staging/src/k8s.io/component-base/logs/json/json_benchmark_test.go b/staging/src/k8s.io/component-base/logs/json/json_benchmark_test.go index f01c3fad346..2eeebfcae6f 100644 --- a/staging/src/k8s.io/component-base/logs/json/json_benchmark_test.go +++ b/staging/src/k8s.io/component-base/logs/json/json_benchmark_test.go @@ -17,50 +17,18 @@ limitations under the License. package logs import ( - "encoding/json" "fmt" "testing" - "time" + + "go.uber.org/zap/zapcore" ) -var record = struct { - Error error `json:"err"` - Level int `json:"v"` - Message string `json:"msg"` - Time time.Time `json:"ts"` - Fields map[string]interface{} `json:"fields"` -}{ - Error: fmt.Errorf("test for error:%s", "default"), - Level: 2, - Message: "test", - Time: time.Unix(0, 123), - Fields: map[string]interface{}{ - "str": "foo", - "int64-1": int64(1), - "int64-2": int64(1), - "float64": float64(1.0), - "string1": "\n", - "string2": "💩", - "string3": "🤔", - "string4": "🙊", - "bool": true, - "request": struct { - Method string `json:"method"` - Timeout int `json:"timeout"` - secret string `json:"secret"` - }{ - Method: "GET", - Timeout: 10, - secret: "pony", - }, - }, -} - func BenchmarkInfoLoggerInfo(b *testing.B) { + logger := NewJSONLogger(zapcore.AddSync(&writeSyncer{})) + b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { - jLogger := NewJSONLogger(nil) - jLogger.Info("test", + logger.Info("test", "str", "foo", "int64-1", int64(1), "int64-2", int64(1), @@ -84,20 +52,12 @@ func BenchmarkInfoLoggerInfo(b *testing.B) { }) } -func BenchmarkInfoLoggerInfoStandardJSON(b *testing.B) { +func BenchmarkZapLoggerError(b *testing.B) { + logger := NewJSONLogger(zapcore.AddSync(&writeSyncer{})) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { - json.Marshal(record) - } - }) -} - -func BenchmarkZapLoggerError(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - jLogger := NewJSONLogger(nil) - jLogger.Error(fmt.Errorf("test for error:%s", "default"), + logger.Error(fmt.Errorf("test for error:%s", "default"), "test", "str", "foo", "int64-1", int64(1), @@ -121,20 +81,13 @@ func BenchmarkZapLoggerError(b *testing.B) { } }) } -func BenchmarkZapLoggerErrorStandardJSON(b *testing.B) { + +func BenchmarkZapLoggerV(b *testing.B) { + logger := NewJSONLogger(zapcore.AddSync(&writeSyncer{})) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { - json.Marshal(record) - } - }) -} - -func BenchmarkZapLoggerV(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - jLogger := NewJSONLogger(nil) - jLogger.V(1).Info("test", + logger.V(1).Info("test", "str", "foo", "int64-1", int64(1), "int64-2", int64(1), @@ -157,3 +110,15 @@ func BenchmarkZapLoggerV(b *testing.B) { } }) } + +type writeSyncer struct{} + +var _ zapcore.WriteSyncer = (*writeSyncer)(nil) + +func (w writeSyncer) Write(p []byte) (n int, err error) { + return len(p), nil +} + +func (w writeSyncer) Sync() error { + return nil +}