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
|
|
|
|
2017-07-26 01:41:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-07-25 23:55:47 +00:00
|
|
|
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")}
|
2017-07-25 23:55:47 +00:00
|
|
|
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)
|
|
|
|
},
|
2017-07-25 23:55:47 +00:00
|
|
|
"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-25 23:55:47 +00:00
|
|
|
},
|
2017-07-26 00:25:40 +00:00
|
|
|
"Floats": func(e *Event) *Event {
|
|
|
|
return e.Floats64("k", floats)
|
2017-07-25 23:55:47 +00:00
|
|
|
},
|
|
|
|
"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)
|
2017-07-25 23:55:47 +00:00
|
|
|
},
|
|
|
|
"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)
|
2017-07-25 23:55:47 +00:00
|
|
|
},
|
|
|
|
"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)
|
2017-07-25 23:55:47 +00:00
|
|
|
},
|
|
|
|
"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)
|
2017-07-25 23:55:47 +00:00
|
|
|
},
|
2017-07-26 01:41:05 +00:00
|
|
|
"Object": func(e *Event) *Event {
|
|
|
|
return e.Object("k", o)
|
|
|
|
},
|
2017-07-25 23:55:47 +00:00
|
|
|
}
|
|
|
|
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("")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|