From 773974fe0b974840e11246bfaa05a53fd9650918 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Tue, 16 Apr 2024 11:22:51 +0800 Subject: [PATCH] fix: fix benchmark bug --- benchmark_json_test.go | 21 +++++++++++++++++---- benchmark_text_test.go | 6 ++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/benchmark_json_test.go b/benchmark_json_test.go index 65b9a43..23f603a 100644 --- a/benchmark_json_test.go +++ b/benchmark_json_test.go @@ -10,6 +10,21 @@ import ( "go.uber.org/zap/zapcore" ) +var testNativeZapEncoderConfig = zapcore.EncoderConfig{ + TimeKey: "ts", + LevelKey: "level", + NameKey: "logger", + CallerKey: "caller", + FunctionKey: zapcore.OmitKey, + MessageKey: "msg", + StacktraceKey: "stacktrace", + LineEnding: zapcore.DefaultLineEnding, + EncodeLevel: zapcore.LowercaseLevelEncoder, + EncodeTime: zapcore.RFC3339TimeEncoder, + EncodeDuration: zapcore.StringDurationEncoder, + EncodeCaller: zapcore.ShortCallerEncoder, +} + func newDiscardLogger(format string) *log.Log { return log.NewLogger( log.WithAdapter("custom", io.Discard), @@ -23,9 +38,8 @@ func dfltCtx(ctx context.Context) log.Field { func Benchmark_Json_NativeLogger(b *testing.B) { b.ReportAllocs() b.StopTimer() - cfg := zap.NewProductionConfig() core := zapcore.NewCore( - zapcore.NewJSONEncoder(cfg.EncoderConfig), + zapcore.NewJSONEncoder(testNativeZapEncoderConfig), zapcore.AddSync(io.Discard), zapcore.InfoLevel, ) @@ -78,9 +92,8 @@ func Benchmark_Json_Logger_Use_Hook(b *testing.B) { func Benchmark_Json_NativeSugar(b *testing.B) { b.ReportAllocs() b.StopTimer() - cfg := zap.NewProductionConfig() core := zapcore.NewCore( - zapcore.NewJSONEncoder(cfg.EncoderConfig), + zapcore.NewJSONEncoder(testNativeZapEncoderConfig), zapcore.AddSync(io.Discard), zapcore.InfoLevel, ) diff --git a/benchmark_text_test.go b/benchmark_text_test.go index f72c26c..64a547c 100644 --- a/benchmark_text_test.go +++ b/benchmark_text_test.go @@ -13,9 +13,8 @@ import ( func Benchmark_Text_NativeLogger(b *testing.B) { b.ReportAllocs() b.StopTimer() - cfg := zap.NewDevelopmentConfig() core := zapcore.NewCore( - zapcore.NewJSONEncoder(cfg.EncoderConfig), + zapcore.NewConsoleEncoder(testNativeZapEncoderConfig), zapcore.AddSync(io.Discard), zapcore.InfoLevel, ) @@ -68,9 +67,8 @@ func Benchmark_Text_Logger_Use_Hook(b *testing.B) { func Benchmark_Text_NativeSugar(b *testing.B) { b.ReportAllocs() b.StopTimer() - cfg := zap.NewProductionConfig() core := zapcore.NewCore( - zapcore.NewJSONEncoder(cfg.EncoderConfig), + zapcore.NewConsoleEncoder(testNativeZapEncoderConfig), zapcore.AddSync(io.Discard), zapcore.InfoLevel, )