Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
fix: fix benchmark bug
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Apr 16, 2024
1 parent 93660fe commit 773974f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
21 changes: 17 additions & 4 deletions benchmark_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down
6 changes: 2 additions & 4 deletions benchmark_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit 773974f

Please sign in to comment.