forked from elastic/ecs-logging-go-zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_bench_test.go
123 lines (110 loc) · 3.01 KB
/
core_bench_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package ecszap
import (
"bytes"
"errors"
"runtime"
"testing"
pkgerrors "github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func BenchmarkCore(b *testing.B) {
fields := []zapcore.Field{
zap.String("string", "foo"),
zap.Int64("int64", 1),
zap.Int("int", 2),
zap.Float64("float64", 1.0),
zap.Bool("bool", true),
}
cores := map[string]func(ws zapcore.WriteSyncer) zapcore.Core{
"zap": func(ws zapcore.WriteSyncer) zapcore.Core {
enc := zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig())
return zapcore.NewCore(enc, ws, zap.DebugLevel)
},
"ecs": func(ws zapcore.WriteSyncer) zapcore.Core {
return NewCore(NewDefaultEncoderConfig(), ws, zap.DebugLevel)
},
}
for name, new := range cores {
b.Run(name+"/fields", func(b *testing.B) {
out := testWriteSyncer{}
core := new(&out)
b.ResetTimer()
for i := 0; i < b.N; i++ {
core.Write(zapcore.Entry{
Message: "fake",
Level: zapcore.DebugLevel,
}, fields)
out.reset()
}
})
b.Run(name+"/caller", func(b *testing.B) {
caller := zapcore.NewEntryCaller(runtime.Caller(0))
out := testWriteSyncer{}
core := new(&out)
b.ResetTimer()
for i := 0; i < b.N; i++ {
core.Write(zapcore.Entry{
Message: "fake",
Level: zapcore.DebugLevel,
Caller: caller,
}, fields)
out.reset()
}
})
b.Run(name+"/errors", func(b *testing.B) {
err1 := errors.New("boom")
err2 := pkgerrors.Wrap(err1, "crash")
err3 := testErr{msg: "boom/crash", errors: []error{err1, err2}}
fieldsWithErr := append(fields,
zap.Error(err1),
zap.Error(err2),
zap.Error(err3),
)
out := testWriteSyncer{}
core := new(&out)
b.ResetTimer()
for i := 0; i < b.N; i++ {
core.Write(zapcore.Entry{
Message: "fake",
Level: zapcore.DebugLevel,
}, fieldsWithErr)
out.reset()
}
})
}
}
type testWriteSyncer struct {
b bytes.Buffer
}
func (o *testWriteSyncer) Write(p []byte) (int, error) {
return o.b.Write(p)
}
func (o *testWriteSyncer) Sync() error { return nil }
func (o *testWriteSyncer) reset() { o.b.Reset() }
type testErr struct {
msg string
errors []error
}
func (e testErr) Error() string {
return e.msg
}
func (e testErr) Errors() []error {
return e.errors
}