Debian Codegraph @

slog/benchmarks/zap_benchmarks/zap_encoders.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package zap_benchmarks
6
7 import (
8 "io"
9 "sync"
10 "time"
11
12 "go.uber.org/zap"
13 "go.uber.org/zap/buffer"
14 "go.uber.org/zap/zapcore"
15 )
16
17 // from zap/internal/ztest
18
19 // A syncer is a spy for the Sync portion of zapcore.WriteSyncer.
20 type syncer struct {
21 err error
22 called bool
23 }
24
25 // SetError sets the error that the Sync method will return.
26 func (s *syncer) SetError(err error) {
27 s.err = err
28 }
29
30 // Sync records that it was called, then returns the user-supplied error (if
31 // any).
32 func (s *syncer) Sync() error {
33 s.called = true
34 return s.err
35 }
36
37 // Called reports whether the Sync method was called.
38 func (s *syncer) Called() bool {
39 return s.called
40 }
41
42 // A discarder sends all writes to ioutil.Discard.
43 type discarder struct{ syncer }
44
45 // Write implements io.Writer.
46 func (d *discarder) Write(b []byte) (int, error) {
47 return io.Discard.Write(b)
48 }
49
50 // fastTextEncoder mimics slog/benchmarks.fastTextHandler.
51 type fastTextEncoder struct {
52 buf *buffer.Buffer
53 zapcore.ObjectEncoder
54 }
55
56 var bufferPool = buffer.NewPool()
57
58 var tePool = sync.Pool{
59 New: func() any { return &fastTextEncoder{} },
60 }
61
62 func newFastTextEncoder() *fastTextEncoder {
63 e := tePool.Get().(*fastTextEncoder)
64 e.buf = bufferPool.Get()
65 return e
66 }
67
68 func (c *fastTextEncoder) Clone() zapcore.Encoder {
69 clone := newFastTextEncoder()
70 if c.buf != nil {
71 panic("buf should be nil")
72 clone.buf.Write(c.buf.Bytes())
73 }
74 return clone
75 }
76
77 func (c *fastTextEncoder) EncodeEntry(e zapcore.Entry, fs []zap.Field) (*buffer.Buffer, error) {
78 c2 := newFastTextEncoder()
79 if !e.Time.IsZero() {
80 c2.buf.AppendString("time=")
81 c2.appendTime(e.Time)
82 c2.buf.AppendByte(' ')
83 }
84 c2.buf.AppendString("level=")
85 c2.buf.AppendInt(int64(e.Level))
86 c2.buf.AppendByte(' ')
87 c2.buf.AppendString("msg=")
88 c2.buf.AppendString(e.Message)
89 for _, f := range fs {
90 c2.buf.AppendByte(' ')
91 f.AddTo(c2)
92 }
93 c2.buf.AppendString("\n")
94 buf := c2.buf
95 tePool.Put(c2)
96 return buf, nil
97 }
98
99 func (c *fastTextEncoder) AddString(key, value string) {
100 c.buf.AppendString(key)
101 c.buf.AppendByte('=')
102 c.buf.AppendString(value)
103 }
104
105 func (c *fastTextEncoder) AddTime(key string, value time.Time) {
106 c.buf.AppendString(key)
107 c.buf.AppendByte('=')
108 c.appendTime(value)
109 }
110
111 func (c *fastTextEncoder) AddDuration(key string, value time.Duration) {
112 c.buf.AppendString(key)
113 c.buf.AppendByte('=')
114 c.buf.AppendInt(value.Nanoseconds())
115 }
116
117 func (c *fastTextEncoder) AddInt64(key string, value int64) {
118 c.buf.AppendString(key)
119 c.buf.AppendByte('=')
120 c.buf.AppendInt(value)
121 }
122 func (c *fastTextEncoder) appendTime(t time.Time) {
123 c.buf.AppendInt(t.Unix())
124 }
125
126 // asyncCore mimics slog/benchmarks.asyncHandler.
127 type asyncCore struct {
128 ringBuffer [100]entryAndFields
129 next int
130 }
131
132 type entryAndFields struct {
133 e zapcore.Entry
134 f []zap.Field
135 }
136
137 func (*asyncCore) Enabled(zapcore.Level) bool { return true }
138 func (c *asyncCore) With([]zap.Field) zapcore.Core { return c }
139 func (*asyncCore) Sync() error { return nil }
140
141 // Also needed to make this non-trivial.
142 func (c *asyncCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
143 return ce.AddCore(ent, c)
144 }
145
146 func (c *asyncCore) Write(e zapcore.Entry, f []zap.Field) error {
147 c.ringBuffer[c.next] = entryAndFields{e, f}
148 c.next = (c.next + 1) % len(c.ringBuffer)
149 return nil
150 }