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

Commit

Permalink
feat: add json and text benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Apr 16, 2024
1 parent 42ac11e commit 66305b1
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 32 deletions.
64 changes: 32 additions & 32 deletions benchmark_test.go → benchmark_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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++ {
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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++ {
Expand All @@ -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()
Expand All @@ -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++ {
Expand All @@ -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()
Expand All @@ -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++ {
Expand All @@ -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()
Expand All @@ -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++ {
Expand All @@ -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()
Expand Down
215 changes: 215 additions & 0 deletions benchmark_text_test.go
Original file line number Diff line number Diff line change
@@ -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",
)
}
}
6 changes: 6 additions & 0 deletions zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 66305b1

Please sign in to comment.