2017-05-12 05:24:39 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2017-05-20 02:43:59 +00:00
|
|
|
"time"
|
2017-05-12 05:24:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errExample = errors.New("fail")
|
|
|
|
fakeMessage = "Test logging, but use a somewhat realistic message length."
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkLogEmpty(b *testing.B) {
|
|
|
|
logger := New(ioutil.Discard)
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
logger.Log().Msg("")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDisabled(b *testing.B) {
|
|
|
|
logger := New(ioutil.Discard).Level(Disabled)
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
logger.Info().Msg(fakeMessage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkInfo(b *testing.B) {
|
|
|
|
logger := New(ioutil.Discard)
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
logger.Info().Msg(fakeMessage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkContextFields(b *testing.B) {
|
|
|
|
logger := New(ioutil.Discard).With().
|
|
|
|
Str("string", "four!").
|
2017-05-20 02:43:59 +00:00
|
|
|
Time("time", time.Time{}).
|
|
|
|
Int("int", 123).
|
|
|
|
Float32("float", -2.203230293249593).
|
2017-05-12 05:24:39 +00:00
|
|
|
Logger()
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
logger.Info().Msg(fakeMessage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLogFields(b *testing.B) {
|
|
|
|
logger := New(ioutil.Discard)
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
logger.Info().
|
|
|
|
Str("string", "four!").
|
2017-05-20 02:43:59 +00:00
|
|
|
Time("time", time.Time{}).
|
|
|
|
Int("int", 123).
|
|
|
|
Float32("float", -2.203230293249593).
|
2017-05-12 05:24:39 +00:00
|
|
|
Msg(fakeMessage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-07-25 23:55:47 +00:00
|
|
|
|
|
|
|
func BenchmarkLogFieldType(b *testing.B) {
|
|
|
|
types := map[string]func(e *Event) *Event{
|
|
|
|
"Int": func(e *Event) *Event {
|
|
|
|
return e.Int("int", 1)
|
|
|
|
},
|
|
|
|
"Float32": func(e *Event) *Event {
|
|
|
|
return e.Float32("float", 1)
|
|
|
|
},
|
|
|
|
"Str": func(e *Event) *Event {
|
|
|
|
return e.Str("str", "foo")
|
|
|
|
},
|
|
|
|
"Err": func(e *Event) *Event {
|
|
|
|
return e.Err(errExample)
|
|
|
|
},
|
|
|
|
"Time": func(e *Event) *Event {
|
|
|
|
return e.Time("time", time.Time{})
|
|
|
|
},
|
|
|
|
"Dur": func(e *Event) *Event {
|
|
|
|
return e.Dur("dur", 1*time.Millisecond)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
logger := New(ioutil.Discard)
|
|
|
|
b.ResetTimer()
|
|
|
|
for name := range types {
|
|
|
|
f := types[name]
|
|
|
|
b.Run(name, func(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
f(logger.Info()).Msg("")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|