Debian Codegraph @

benchmarks/zap_test.go

raw symbols SCIP
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/zapcore"
31 )
32
33 var (
34 errExample = errors.New("fail")
35
36 _messages = fakeMessages(1000)
37 _tenInts = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
38 _tenStrings = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
39 _tenTimes = []time.Time{
40 time.Unix(0, 0),
41 time.Unix(1, 0),
42 time.Unix(2, 0),
43 time.Unix(3, 0),
44 time.Unix(4, 0),
45 time.Unix(5, 0),
46 time.Unix(6, 0),
47 time.Unix(7, 0),
48 time.Unix(8, 0),
49 time.Unix(9, 0),
50 }
51 _oneUser = &user{
52 Name: "Jane Doe",
53 Email: "[email protected]",
54 CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC),
55 }
56 _tenUsers = users{
57 _oneUser,
58 _oneUser,
59 _oneUser,
60 _oneUser,
61 _oneUser,
62 _oneUser,
63 _oneUser,
64 _oneUser,
65 _oneUser,
66 _oneUser,
67 }
68 )
69
70 func fakeMessages(n int) []string {
71 messages := make([]string, n)
72 for i := range messages {
73 messages[i] = fmt.Sprintf("Test logging, but use a somewhat realistic message length. (#%v)", i)
74 }
75 return messages
76 }
77
78 func getMessage(iter int) string {
79 return _messages[iter%1000]
80 }
81
82 type users []*user
83
84 func (uu users) MarshalLogArray(arr zapcore.ArrayEncoder) error {
85 var err error
86 for i := range uu {
87 err = multierr.Append(err, arr.AppendObject(uu[i]))
88 }
89 return err
90 }
91
92 type user struct {
93 Name string `json:"name"`
94 Email string `json:"email"`
95 CreatedAt time.Time `json:"created_at"`
96 }
97
98 func (u *user) MarshalLogObject(enc zapcore.ObjectEncoder) error {
99 enc.AddString("name", u.Name)
100 enc.AddString("email", u.Email)
101 enc.AddInt64("createdAt", u.CreatedAt.UnixNano())
102 return nil
103 }
104
105 func fakeFields() []zap.Field {
106 return []zap.Field{
107 zap.Int("int", _tenInts[0]),
108 zap.Ints("ints", _tenInts),
109 zap.String("string", _tenStrings[0]),
110 zap.Strings("strings", _tenStrings),
111 zap.Time("time", _tenTimes[0]),
112 zap.Times("times", _tenTimes),
113 zap.Object("user1", _oneUser),
114 zap.Object("user2", _oneUser),
115 zap.Array("users", _tenUsers),
116 zap.Error(errExample),
117 }
118 }
119
120 func fakeSugarFields() []interface{} {
121 return []interface{}{
122 "int", _tenInts[0],
123 "ints", _tenInts,
124 "string", _tenStrings[0],
125 "strings", _tenStrings,
126 "time", _tenTimes[0],
127 "times", _tenTimes,
128 "user1", _oneUser,
129 "user2", _oneUser,
130 "users", _tenUsers,
131 "error", errExample,
132 }
133 }
134
135 func fakeFmtArgs() []interface{} {
136 // Need to keep this a function instead of a package-global var so that we
137 // pay the cast-to-interface{} penalty on each call.
138 return []interface{}{
139 _tenInts[0],
140 _tenInts,
141 _tenStrings[0],
142 _tenStrings,
143 _tenTimes[0],
144 _tenTimes,
145 _oneUser,
146 _oneUser,
147 _tenUsers,
148 errExample,
149 }
150 }