Skip to content

Commit

Permalink
Add benchmark for Nest, remove extra alloc in BenchmarkMarshalerField (
Browse files Browse the repository at this point in the history
…#90)

* Remove unnecessary alloc in BenchmarkMarshalerField

* json encoder: Add a benchmark for Nest

When we replace Nest with some sort of LogMarshalerfunc (#87), we should
compare performance against the existing implementation.
  • Loading branch information
prashantv authored Jun 28, 2016
1 parent 8898396 commit ef2f601
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 13 additions & 0 deletions json_encoder_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ type logRecord struct {
Fields map[string]interface{} `json:"fields"`
}

var s string

func BenchmarkJSONEncoderNest(b *testing.B) {
for i := 0; i < b.N; i++ {
enc := newJSONEncoder()
enc.Nest("nested", func(kv KeyValue) error {
kv.AddInt("i", i)
return nil
})
enc.Free()
}
}

func BenchmarkZapJSON(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand Down
6 changes: 2 additions & 4 deletions logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ type user struct {
CreatedAt time.Time
}

func (u user) MarshalLog(kv zap.KeyValue) error {
func (u *user) MarshalLog(kv zap.KeyValue) error {
kv.AddString("name", u.Name)
kv.AddString("email", u.Email)
kv.AddInt64("created_at", u.CreatedAt.UnixNano())
return nil
}

var _jane = user{
var _jane = &user{
Name: "Jane Doe",
Email: "jane@test.com",
CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC),
Expand Down Expand Up @@ -126,8 +126,6 @@ func BenchmarkStackField(b *testing.B) {
}

func BenchmarkMarshalerField(b *testing.B) {
// Expect an extra allocation here, since casting the user struct to the
// zap.Marshaler interface costs an alloc.
withBenchedLogger(b, func(log zap.Logger) {
log.Info("Arbitrary zap.LogMarshaler.", zap.Marshaler("user", _jane))
})
Expand Down

0 comments on commit ef2f601

Please sign in to comment.