zlog/benchmark_test.go

170 lines
3.6 KiB
Go
Raw Normal View History

2017-05-12 05:24:39 +00:00
package zerolog
import (
"errors"
"io/ioutil"
"testing"
"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!").
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!").
Time("time", time.Time{}).
Int("int", 123).
Float32("float", -2.203230293249593).
2017-05-12 05:24:39 +00:00
Msg(fakeMessage)
}
})
}
type obj struct {
Pub string
Tag string `json:"tag"`
priv int
}
func (o obj) MarshalZerologObject(e *Event) {
e.Str("Pub", o.Pub).
Str("Tag", o.Tag).
Int("priv", o.priv)
}
func BenchmarkLogFieldType(b *testing.B) {
2017-07-26 00:25:40 +00:00
bools := []bool{true, false, true, false, true, false, true, false, true, false}
ints := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
floats := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
strings := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
durations := []time.Duration{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
times := []time.Time{
time.Unix(0, 0),
time.Unix(1, 0),
time.Unix(2, 0),
time.Unix(3, 0),
time.Unix(4, 0),
time.Unix(5, 0),
time.Unix(6, 0),
time.Unix(7, 0),
time.Unix(8, 0),
time.Unix(9, 0),
}
o := obj{"a", "a", 0}
errs := []error{errors.New("a"), errors.New("b"), errors.New("c"), errors.New("d"), errors.New("e")}
types := map[string]func(e *Event) *Event{
2017-07-26 00:25:40 +00:00
"Bool": func(e *Event) *Event {
return e.Bool("k", bools[0])
},
"Bools": func(e *Event) *Event {
return e.Bools("k", bools)
},
"Int": func(e *Event) *Event {
2017-07-26 00:25:40 +00:00
return e.Int("k", ints[0])
},
"Ints": func(e *Event) *Event {
return e.Ints("k", ints)
},
"Float": func(e *Event) *Event {
return e.Float64("k", floats[0])
},
2017-07-26 00:25:40 +00:00
"Floats": func(e *Event) *Event {
return e.Floats64("k", floats)
},
"Str": func(e *Event) *Event {
2017-07-26 00:25:40 +00:00
return e.Str("k", strings[0])
},
"Strs": func(e *Event) *Event {
return e.Strs("k", strings)
},
"Err": func(e *Event) *Event {
2017-07-26 00:25:40 +00:00
return e.Err(errs[0])
},
"Errs": func(e *Event) *Event {
return e.Errs("k", errs)
},
"Time": func(e *Event) *Event {
2017-07-26 00:25:40 +00:00
return e.Time("k", times[0])
},
"Times": func(e *Event) *Event {
return e.Times("k", times)
},
"Dur": func(e *Event) *Event {
2017-07-26 00:25:40 +00:00
return e.Dur("k", durations[0])
},
"Durs": func(e *Event) *Event {
return e.Durs("k", durations)
},
"Interface": func(e *Event) *Event {
return e.Interface("k", o)
},
"Object": func(e *Event) *Event {
return e.Object("k", o)
},
}
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("")
}
})
})
}
}