Debian Codegraph @

slog/benchmarks/zap_benchmarks/zap_encoders_test.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 "fmt"
9 "strings"
10 "testing"
11
12 "go.uber.org/zap"
13 "go.uber.org/zap/zapcore"
14 "golang.org/x/exp/slog"
15 slogbench "golang.org/x/exp/slog/benchmarks"
16 )
17
18 func TestEncoders(t *testing.T) {
19 entry := zapcore.Entry{Time: slogbench.TestTime, Message: slogbench.TestMessage}
20 fields := attrsToFields(slogbench.TestAttrs)
21 t.Run("text", func(t *testing.T) {
22 te := newFastTextEncoder()
23 defer tePool.Put(te)
24 buf, err := te.EncodeEntry(entry, fields)
25 if err != nil {
26 t.Fatal(err)
27 }
28 defer buf.Free()
29 got := strings.ToLower(buf.String())
30 want := strings.ToLower(slogbench.WantText)
31 if got != want {
32 t.Errorf("\ngot %s\nwant %s", got, want)
33 }
34 })
35 }
36
37 func attrsToFields(attrs []slog.Attr) []zap.Field {
38 var fields []zap.Field
39 for _, a := range slogbench.TestAttrs {
40 var f zap.Field
41 k := a.Key
42 v := a.Value
43 switch v.Kind() {
44 case slog.KindString:
45 f = zap.String(k, v.String())
46 case slog.KindInt64:
47 f = zap.Int64(k, v.Int64())
48 case slog.KindDuration:
49 f = zap.Duration(k, v.Duration())
50 case slog.KindTime:
51 f = zap.Time(k, v.Time())
52 case slog.KindAny:
53 f = zap.Any(k, v)
54 default:
55 panic(fmt.Sprintf("unknown kind %d", v.Kind()))
56 }
57 fields = append(fields, f)
58 }
59 return fields
60 }