Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
❯  go test -bench BenchmarkAnyInGoroutine -benchmem -run Any   -test.cpuprofile ~/cpuprof

goos: darwin
goarch: arm64
pkg: go.uber.org/zap
BenchmarkAnyInGoroutine1/int-in-goroutine-with-stack-12                  3125690               376.5 ns/op            88 B/op          2 allocs/op
BenchmarkAnyInGoroutine1/any-in-goroutine-with-stack-12                  1763952               665.0 ns/op            88 B/op          2 allocs/op
BenchmarkAnyInGoroutine1/any-int-in-goroutine-with-stack-12              2904816               398.3 ns/op            88 B/op          2 allocs/op
BenchmarkAnyInGoroutine1/any-12                                          4141492               289.2 ns/op            64 B/op          1 allocs/op
BenchmarkAnyInGoroutine1/int-12                                          4395013               274.5 ns/op            64 B/op          1 allocs/op
BenchmarkAnyInGoroutine1/goroutine-12                                    5005946               217.9 ns/op             0 B/op          0 allocs/op
BenchmarkAnyInGoroutine1/int-in-goroutine-12                             2079958               594.9 ns/op            88 B/op          2 allocs/op
BenchmarkAnyInGoroutine1/any-in-goroutine-12                             1773802               649.9 ns/op            88 B/op          2 allocs/op
  • Loading branch information
rabbbit committed Jul 23, 2023
1 parent 382e251 commit 64bf990
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
10 changes: 10 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,13 @@ func Any(key string, value interface{}) Field {
return Reflect(key, val)
}
}

// AnyInt is a dummified version of Any that can handle ints.
func AnyInt(key string, value interface{}) Field {
switch val := value.(type) {
case int:
return Int(key, val)
default:
return Reflect(key, val)
}
}
110 changes: 110 additions & 0 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"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -238,3 +239,112 @@ func Benchmark100Fields(b *testing.B) {
logger.With(first...).Info("Child loggers with lots of context.", second...)
}
}

func dummy(wg *sync.WaitGroup, s string, i int) string {
if i == 0 {
wg.Wait()
return "1" + s
}
return dummy(wg, s, i-1)
}

func stackGrower(n int) *sync.WaitGroup {
wg := sync.WaitGroup{}
wg.Add(1)

go dummy(&wg, "hi", n)
return &wg
}

func BenchmarkAnyInGoroutine1(b *testing.B) {
logger := New(
zapcore.NewCore(
zapcore.NewJSONEncoder(NewProductionConfig().EncoderConfig),
&ztest.Discarder{},
DebugLevel,
),
)

b.Run("int-in-goroutine-with-stack", func(b *testing.B) {
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))
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
})
b.Run("any-in-goroutine-with-stack", func(b *testing.B) {
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))
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
})
b.Run("any-int-in-goroutine-with-stack", func(b *testing.B) {
wg := sync.WaitGroup{}
wg.Add(b.N)
defer stackGrower(1000).Done()
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", AnyInt("one", 1))
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
})
b.Run("any", func(b *testing.B) {
for i := 0; i < b.N; i++ {
logger.Info("", Any("one", 1))
}
})
b.Run("int", func(b *testing.B) {
for i := 0; i < b.N; i++ {
logger.Info("", Int("one", 1))
}
})
b.Run("goroutine", func(b *testing.B) {
for i := 0; i < b.N; i++ {
go func() {}()
}
})
b.Run("int-in-goroutine", func(b *testing.B) {
wg := sync.WaitGroup{}
wg.Add(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", Int("one", 1))
wg.Done()
}()
}
wg.Wait()
})
b.Run("any-in-goroutine", func(b *testing.B) {
wg := sync.WaitGroup{}
wg.Add(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", Any("one", 1))
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
})
}

0 comments on commit 64bf990

Please sign in to comment.