diff --git a/benchmark_test.go b/benchmark_json_test.go similarity index 74% rename from benchmark_test.go rename to benchmark_json_test.go index e7d5fd6..65b9a43 100644 --- a/benchmark_test.go +++ b/benchmark_json_test.go @@ -10,7 +10,17 @@ import ( "go.uber.org/zap/zapcore" ) -func Benchmark_NativeLogger(b *testing.B) { +func newDiscardLogger(format string) *log.Log { + return log.NewLogger( + log.WithAdapter("custom", io.Discard), + log.WithFormat(format), + ) +} +func dfltCtx(ctx context.Context) log.Field { + return zap.String("dflt_key", "dflt_value") +} + +func Benchmark_Json_NativeLogger(b *testing.B) { b.ReportAllocs() b.StopTimer() cfg := zap.NewProductionConfig() @@ -29,20 +39,10 @@ func Benchmark_NativeLogger(b *testing.B) { } } -func newDiscardLogger() *log.Log { - return log.NewLogger( - log.WithAdapter("custom", io.Discard), - // log.WithFormat("console"), - ) -} -func dfltCtx(ctx context.Context) log.Field { - return zap.String("dflt_key", "dflt_value") -} - -func Benchmark_Logger(b *testing.B) { +func Benchmark_Json_Logger(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { @@ -57,10 +57,10 @@ func Benchmark_Logger(b *testing.B) { } } -func Benchmark_Logger_Use_Hook(b *testing.B) { +func Benchmark_Json_Logger_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) logger.SetDefaultValuer(dfltCtx) ctx := context.Background() b.StartTimer() @@ -75,7 +75,7 @@ func Benchmark_Logger_Use_Hook(b *testing.B) { } } -func Benchmark_NativeSugar(b *testing.B) { +func Benchmark_Json_NativeSugar(b *testing.B) { b.ReportAllocs() b.StopTimer() cfg := zap.NewProductionConfig() @@ -94,10 +94,10 @@ func Benchmark_NativeSugar(b *testing.B) { } } -func Benchmark_SugarKeyValuePair(b *testing.B) { +func Benchmark_Json_SugarKeyValuePair(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { @@ -110,10 +110,10 @@ func Benchmark_SugarKeyValuePair(b *testing.B) { } } -func Benchmark_SugarKeyValuePair_Use_Hook(b *testing.B) { +func Benchmark_Json_SugarKeyValuePair_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) logger.SetDefaultValuer(dfltCtx) ctx := context.Background() b.StartTimer() @@ -126,10 +126,10 @@ func Benchmark_SugarKeyValuePair_Use_Hook(b *testing.B) { } } -func Benchmark_SugarKeyValuePair_Use_WithFields(b *testing.B) { +func Benchmark_Json_SugarKeyValuePair_Use_WithFields(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { @@ -141,10 +141,10 @@ func Benchmark_SugarKeyValuePair_Use_WithFields(b *testing.B) { } } -func Benchmark_SugarKeyValuePair_Use_WithFields_Hook(b *testing.B) { +func Benchmark_Json_SugarKeyValuePair_Use_WithFields_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) logger.SetDefaultValuer(dfltCtx) ctx := context.Background() b.StartTimer() @@ -156,10 +156,10 @@ func Benchmark_SugarKeyValuePair_Use_WithFields_Hook(b *testing.B) { } } -func Benchmark_SugarKeyValuePair_Use_WithValuer(b *testing.B) { +func Benchmark_Json_SugarKeyValuePair_Use_WithValuer(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { @@ -171,10 +171,10 @@ func Benchmark_SugarKeyValuePair_Use_WithValuer(b *testing.B) { } } -func Benchmark_SugarKeyValuePair_Use_WithValuer_Hook(b *testing.B) { +func Benchmark_Json_SugarKeyValuePair_Use_WithValuer_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) logger.SetDefaultValuer(dfltCtx) ctx := context.Background() b.StartTimer() @@ -186,10 +186,10 @@ func Benchmark_SugarKeyValuePair_Use_WithValuer_Hook(b *testing.B) { } } -func Benchmark_SugarFormat(b *testing.B) { +func Benchmark_Json_SugarFormat(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { @@ -207,10 +207,10 @@ func Benchmark_SugarFormat(b *testing.B) { } } -func Benchmark_SugarFormat_Use_Hook(b *testing.B) { +func Benchmark_Json_SugarFormat_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() - logger := newDiscardLogger() + logger := newDiscardLogger(log.FormatJson) logger.SetDefaultValuer(dfltCtx) ctx := context.Background() b.StartTimer() diff --git a/benchmark_text_test.go b/benchmark_text_test.go new file mode 100644 index 0000000..f72c26c --- /dev/null +++ b/benchmark_text_test.go @@ -0,0 +1,215 @@ +package log_test + +import ( + "context" + "io" + "testing" + + "github.com/things-go/log" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +func Benchmark_Text_NativeLogger(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + cfg := zap.NewDevelopmentConfig() + core := zapcore.NewCore( + zapcore.NewJSONEncoder(cfg.EncoderConfig), + zapcore.AddSync(io.Discard), + zapcore.InfoLevel, + ) + logger := zap.New(core) + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.Info("success", + zap.String("name", "jack"), + zap.Int("age", 18), + ) + } +} + +func Benchmark_Text_Logger(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger. + InfoxContext( + ctx, + "success", + log.String("name", "jack"), + log.Int("age", 18), + dfltCtx(ctx), + ) + } +} + +func Benchmark_Text_Logger_Use_Hook(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + logger.SetDefaultValuer(dfltCtx) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger. + InfoxContext( + ctx, + "success", + log.String("name", "jack"), + log.Int("age", 18), + ) + } +} + +func Benchmark_Text_NativeSugar(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + cfg := zap.NewProductionConfig() + core := zapcore.NewCore( + zapcore.NewJSONEncoder(cfg.EncoderConfig), + zapcore.AddSync(io.Discard), + zapcore.InfoLevel, + ) + logger := zap.New(core).Sugar() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.Infow("success", + "name", "jack", + "age", 18, + ) + } +} + +func Benchmark_Text_SugarKeyValuePair(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.InfowContext(ctx, + "success", + log.String("name", "jack"), + log.Int("age", 18), + dfltCtx(ctx), + ) + } +} + +func Benchmark_Text_SugarKeyValuePair_Use_Hook(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + logger.SetDefaultValuer(dfltCtx) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.InfowContext(ctx, + "success", + log.String("name", "jack"), + log.Int("age", 18), + ) + } +} + +func Benchmark_Text_SugarKeyValuePair_Use_WithFields(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.With( + log.String("name", "jack"), + log.Int("age", 18), + dfltCtx(ctx), + ).InfowContext(ctx, "success") + } +} + +func Benchmark_Text_SugarKeyValuePair_Use_WithFields_Hook(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + logger.SetDefaultValuer(dfltCtx) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.With( + log.String("name", "jack"), + log.Int("age", 18), + ).InfowContext(ctx, "success") + } +} + +func Benchmark_Text_SugarKeyValuePair_Use_WithValuer(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.WithValuer( + log.ImmutString("name", "jack"), + log.ImmutInt("age", 18), + dfltCtx, + ).InfowContext(ctx, "success") + } +} + +func Benchmark_Text_SugarKeyValuePair_Use_WithValuer_Hook(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + logger.SetDefaultValuer(dfltCtx) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.WithValuer( + log.ImmutString("name", "jack"), + log.ImmutInt("age", 18), + ).InfowContext(ctx, "success") + } +} + +func Benchmark_Text_SugarFormat(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.WithValuer( + func(ctx context.Context) log.Field { + return log.String("name", "jack") + }, + func(ctx context.Context) log.Field { + return log.Int("age", 18) + }, + dfltCtx, + ).InfofContext(ctx, + "success", + ) + } +} + +func Benchmark_Text_SugarFormat_Use_Hook(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + logger.SetDefaultValuer(dfltCtx) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.WithValuer( + log.ImmutString("name", "jack"), + log.ImmutInt("age", 18), + ).InfofContext(ctx, + "success", + ) + } +} diff --git a/zap.go b/zap.go index 5cf9f40..33018ff 100644 --- a/zap.go +++ b/zap.go @@ -21,6 +21,12 @@ const ( AdapterMultiCustom = "multi-custom" // file, console and custom io.Writer ) +// format defined +const ( + FormatJson = "json" + FormatConsole = "console" +) + // New constructs a new Log func New(opts ...Option) (*zap.Logger, zap.AtomicLevel) { c := &Config{}