-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbenchmark_test.go
86 lines (72 loc) · 2.02 KB
/
benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package benchmarks
import (
"errors"
"io/ioutil"
"os"
"testing"
"time"
log "github.com/go-playground/log/v8"
"github.com/go-playground/log/v8/handlers/json"
)
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),
}
// NOTE: log is a singleton, which means handlers need to be
// setup only once otherwise each test just adds another log
// handler and results are cumulative... makes benchmarking
// annoying because you have to manipulate the TestMain before
// running the benchmark you want.
func TestMain(m *testing.M) {
cLog := log.NewConsoleBuilder().WithWriter(ioutil.Discard).Build()
log.AddHandler(cLog, log.AllLevels...)
os.Exit(m.Run())
}
func BenchmarkLogConsoleTenFieldsParallel(b *testing.B) {
log.AddHandler(log.NewConsoleBuilder().WithWriter(ioutil.Discard).Build(), log.AllLevels...)
b.ResetTimer()
// log setup in TestMain
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.WithFields(
log.F("int", 1),
log.F("int64", int64(1)),
log.F("float", 3.0),
log.F("string", "four!"),
log.F("bool", true),
log.F("time", time.Unix(0, 0)),
log.F("error", errExample.Error()),
log.F("duration", time.Second),
log.F("user-defined type", _jane),
log.F("another string", "done!"),
).Info("Go fast.")
}
})
}
func BenchmarkLogConsoleSimpleParallel(b *testing.B) {
log.AddHandler(log.NewConsoleBuilder().WithWriter(ioutil.Discard).Build(), log.AllLevels...)
b.ResetTimer()
// log setup in TestMain
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Info("Go fast.")
}
})
}
func BenchmarkLogJSONSimpleParallel(b *testing.B) {
log.AddHandler(json.New(ioutil.Discard), log.AllLevels...)
b.ResetTimer()
// log setup in TestMain
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Info("Go fast.")
}
})
}