Debian Codegraph @

benchmarks/zap_test.go

raw
1 // Copyright (c) 2016 Uber Technologies, Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 package benchmarks
22
23 import (
24 "errors"
25 "fmt"
26 "time"
27
28 "go.uber.org/multierr"
29 "go.uber.org/zap"
30 "go.uber.org/zap/internal/ztest"
31 "go.uber.org/zap/zapcore"
32 )
33
34 var (
35 errExample = errors.New("fail")
36
37 _messages = fakeMessages(1000)
38 _tenInts = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
39 _tenStrings = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
40 _tenTimes = []time.Time{
41 time.Unix(0, 0),
42 time.Unix(1, 0),
43 time.Unix(2, 0),
44 time.Unix(3, 0),
45 time.Unix(4, 0),
46 time.Unix(5, 0),
47 time.Unix(6, 0),
48 time.Unix(7, 0),
49 time.Unix(8, 0),
50 time.Unix(9, 0),
51 }
52 _oneUser = &user{
53 Name: "Jane Doe",
54 Email: "[email protected]",
55 CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC),
56 }
57 _tenUsers = users{
58 _oneUser,
59 _oneUser,
60 _oneUser,
61 _oneUser,
62 _oneUser,
63 _oneUser,
64 _oneUser,
65 _oneUser,
66 _oneUser,
67 _oneUser,
68 }
69 )
70
71 func fakeMessages(n int) []string {
72 messages := make([]string, n)
73 for i := range messages {
74 messages[i] = fmt.Sprintf("Test logging, but use a somewhat realistic message length. (#%v)", i)
75 }
76 return messages
77 }
78
79 func getMessage(iter int) string {
80 return _messages[iter%1000]
81 }
82
83 type users []*user
84
85 func (uu users) MarshalLogArray(arr zapcore.ArrayEncoder) error {
86 var err error
87 for i := range uu {
88 err = multierr.Append(err, arr.AppendObject(uu[i]))
89 }
90 return err
91 }
92
93 type user struct {
94 Name string `json:"name"`
95 Email string `json:"email"`
96 CreatedAt time.Time `json:"created_at"`
97 }
98
99 func (u *user) MarshalLogObject(enc zapcore.ObjectEncoder) error {
100 enc.AddString("name", u.Name)
101 enc.AddString("email", u.Email)
102 enc.AddInt64("createdAt", u.CreatedAt.UnixNano())
103 return nil
104 }
105
106 func newZapLogger(lvl zapcore.Level) *zap.Logger {
107 ec := zap.NewProductionEncoderConfig()
108 ec.EncodeDuration = zapcore.NanosDurationEncoder
109 ec.EncodeTime = zapcore.EpochNanosTimeEncoder
110 enc := zapcore.NewJSONEncoder(ec)
111 return zap.New(zapcore.NewCore(
112 enc,
113 &ztest.Discarder{},
114 lvl,
115 ))
116 }
117
118 func newSampledLogger(lvl zapcore.Level) *zap.Logger {
119 return zap.New(zapcore.NewSamplerWithOptions(
120 newZapLogger(zap.DebugLevel).Core(),
121 100*time.Millisecond,
122 10, // first
123 10, // thereafter
124 ))
125 }
126
127 func fakeFields() []zap.Field {
128 return []zap.Field{
129 zap.Int("int", _tenInts[0]),
130 zap.Ints("ints", _tenInts),
131 zap.String("string", _tenStrings[0]),
132 zap.Strings("strings", _tenStrings),
133 zap.Time("time", _tenTimes[0]),
134 zap.Times("times", _tenTimes),
135 zap.Object("user1", _oneUser),
136 zap.Object("user2", _oneUser),
137 zap.Array("users", _tenUsers),
138 zap.Error(errExample),
139 }
140 }
141
142 func fakeSugarFields() []interface{} {
143 return []interface{}{
144 "int", _tenInts[0],
145 "ints", _tenInts,
146 "string", _tenStrings[0],
147 "strings", _tenStrings,
148 "time", _tenTimes[0],
149 "times", _tenTimes,
150 "user1", _oneUser,
151 "user2", _oneUser,
152 "users", _tenUsers,
153 "error", errExample,
154 }
155 }
156
157 func fakeFmtArgs() []interface{} {
158 // Need to keep this a function instead of a package-global var so that we
159 // pay the cast-to-interface{} penalty on each call.
160 return []interface{}{
161 _tenInts[0],
162 _tenInts,
163 _tenStrings[0],
164 _tenStrings,
165 _tenTimes[0],
166 _tenTimes,
167 _oneUser,
168 _oneUser,
169 _tenUsers,
170 errExample,
171 }
172 }