Skip to content

Commit

Permalink
Add the benchmarks from uber-go/zap
Browse files Browse the repository at this point in the history
Here are the results:

```
BenchmarkLog15AddingFields-4             	   50000	     29492 ns/op
BenchmarkLog15WithAccumulatedContext-4   	   50000	     33599 ns/op
BenchmarkLog15WithoutFields-4            	  200000	      9417 ns/op
```

The benchmarks use testing.PB which was only added in Go 1.3, so this makes the
benchmarks conditional on you running Go 1.3 or later.
  • Loading branch information
kevinburke committed Jun 22, 2016
1 parent c94d940 commit 8ae585a
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// +build go1.3

package log15

import (
"bytes"
"errors"
"io/ioutil"
"testing"
"time"
)
Expand Down Expand Up @@ -127,3 +131,77 @@ func BenchmarkDescendant8(b *testing.B) {
lg.Info("test message")
}
}

// Copied from https://github.com/uber-go/zap/blob/master/benchmarks/log15_bench_test.go
// (MIT License)
func newLog15() Logger {
logger := New()
logger.SetHandler(StreamHandler(ioutil.Discard, JsonFormat()))
return logger
}

var errExample = errors.New("fail")

type user struct {
Name string `json:"name"`
Email string `json:"email"`
CreatedAt time.Time `json:"created_at"`
}

var _jane = user{
Name: "Jane Doe",
Email: "jane@test.com",
CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC),
}

func BenchmarkLog15AddingFields(b *testing.B) {
logger := newLog15()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info("Go fast.",
"int", 1,
"int64", int64(1),
"float", 3.0,
"string", "four!",
"bool", true,
"time", time.Unix(0, 0),
"error", errExample.Error(),
"duration", time.Second,
"user-defined type", _jane,
"another string", "done!",
)
}
})
}

func BenchmarkLog15WithAccumulatedContext(b *testing.B) {
logger := newLog15().New(
"int", 1,
"int64", int64(1),
"float", 3.0,
"string", "four!",
"bool", true,
"time", time.Unix(0, 0),
"error", errExample.Error(),
"duration", time.Second,
"user-defined type", _jane,
"another string", "done!",
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info("Go really fast.")
}
})
}

func BenchmarkLog15WithoutFields(b *testing.B) {
logger := newLog15()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info("Go fast.")
}
})
}

0 comments on commit 8ae585a

Please sign in to comment.