Skip to content

Commit

Permalink
convert to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbbit committed Jul 26, 2023
1 parent f764b9b commit a9764db
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package zap

import (
"errors"
"runtime"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -256,7 +257,7 @@ func stackGrower(n int) *sync.WaitGroup {
return &wg
}

func BenchmarkAnyInGoroutine(b *testing.B) {
func BenchmarkAny(b *testing.B) {
logger := New(
zapcore.NewCore(
zapcore.NewJSONEncoder(NewProductionConfig().EncoderConfig),
Expand All @@ -265,69 +266,87 @@ func BenchmarkAnyInGoroutine(b *testing.B) {
),
)

b.Run("any", func(b *testing.B) {
b.Run("any-no-logger", func(b *testing.B) {
errs := []error{errors.New("this error")}
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("", Any("one", 1))
f := Any("some-string-longer-than-16", errs)
runtime.KeepAlive(f)
}
})
b.Run("int", func(b *testing.B) {
b.Run("errs-no-logger", func(b *testing.B) {
errs := []error{errors.New("this error")}
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("", Int("one", 1))
f := Errors("some-string-longer-than-16", errs)
runtime.KeepAlive(f)
}
})
// Just to see a cost of this.
b.Run("goroutine", func(b *testing.B) {
b.Run("any-with-logger", func(b *testing.B) {
errs := []error{errors.New("this error")}
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("", Any("some-string-longer-than-16", errs))
}
})
b.Run("errs-with-logger", func(b *testing.B) {
errs := []error{errors.New("this error")}
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {}()
logger.Info("", Errors("some-string-longer-than-16", errs))
}
})
b.Run("int-in-go", func(b *testing.B) {
b.Run("errs-in-go", func(b *testing.B) {
errs := []error{errors.New("this error")}
wg := sync.WaitGroup{}
wg.Add(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", Int("one", 1))
logger.Info("", Errors("some-string-longer-than-16", errs))
wg.Done()
}()
}
wg.Wait()
})
b.Run("any-in-go", func(b *testing.B) {
errs := []error{errors.New("this error")}
wg := sync.WaitGroup{}
wg.Add(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", Any("one", 1))
logger.Info("", Any("some-string-longer-than-16", errs))
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
})
b.Run("int-in-go-with-stack", func(b *testing.B) {
b.Run("errs-in-go-with-stack", func(b *testing.B) {
errs := []error{errors.New("this error")}
wg := sync.WaitGroup{}
wg.Add(b.N)
defer stackGrower(1000).Done()
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", Int("one", 1))
logger.Info("", Errors("some-string-longer-than-16", errs))
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
})
b.Run("any-in-go-with-stack", func(b *testing.B) {
errs := []error{errors.New("this error")}
wg := sync.WaitGroup{}
wg.Add(b.N)
defer stackGrower(1000).Done()
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", Any("one", 1))
logger.Info("", Any("some-string-longer-than-16", errs))
wg.Done()
}()
}
Expand Down

0 comments on commit a9764db

Please sign in to comment.