Cleanup json logging benchmarks

* Write logs to sink instead of stderr
* Remove json marshall tests
This commit is contained in:
Marek Siarkowicz 2021-06-05 11:40:32 +02:00
parent 5c137f1af9
commit a9a2346e19

View File

@ -17,50 +17,18 @@ limitations under the License.
package logs package logs
import ( import (
"encoding/json"
"fmt" "fmt"
"testing" "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) { func BenchmarkInfoLoggerInfo(b *testing.B) {
logger := NewJSONLogger(zapcore.AddSync(&writeSyncer{}))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
jLogger := NewJSONLogger(nil) logger.Info("test",
jLogger.Info("test",
"str", "foo", "str", "foo",
"int64-1", int64(1), "int64-1", int64(1),
"int64-2", 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.ResetTimer()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
json.Marshal(record) logger.Error(fmt.Errorf("test for error:%s", "default"),
}
})
}
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"),
"test", "test",
"str", "foo", "str", "foo",
"int64-1", int64(1), "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.ResetTimer()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
json.Marshal(record) logger.V(1).Info("test",
}
})
}
func BenchmarkZapLoggerV(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
jLogger := NewJSONLogger(nil)
jLogger.V(1).Info("test",
"str", "foo", "str", "foo",
"int64-1", int64(1), "int64-1", int64(1),
"int64-2", 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
}