forked from xiaomi-tc/log15
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
c94d940
commit 8ae585a
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
|
@@ -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: "[email protected]", | ||
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.") | ||
} | ||
}) | ||
} |