Debian Codegraph @

slog/benchmarks/zap_benchmarks/zap_benchmarks_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 "testing"
9 "time"
10
11 "go.uber.org/zap"
12 "go.uber.org/zap/zapcore"
13 slogbench "golang.org/x/exp/slog/benchmarks"
14 )
15
16 // Keep in sync (same names and behavior) as the
17 // benchmarks in the parent directory.
18
19 func BenchmarkAttrs(b *testing.B) {
20 for _, logger := range []struct {
21 name string
22 l *zap.Logger
23 }{
24 {
25 "async discard",
26 zap.New(&asyncCore{}),
27 },
28 {
29 "fastText discard",
30 zap.New(zapcore.NewCore(
31 &fastTextEncoder{},
32 &discarder{},
33 zap.DebugLevel)),
34 },
35 {
36 "JSON discard",
37 zap.New(zapcore.NewCore(
38 zapcore.NewJSONEncoder(zapcore.EncoderConfig{
39 MessageKey: "msg",
40 LevelKey: "level",
41 TimeKey: "time",
42 EncodeTime: zapcore.TimeEncoderOfLayout(time.RFC3339Nano),
43 }),
44 &discarder{},
45 zap.DebugLevel)),
46 }} {
47 l := logger.l
48 b.Run(logger.name, func(b *testing.B) {
49 for _, call := range []struct {
50 name string
51 f func()
52 }{
53 {
54 "5 args",
55 func() {
56 l.Info(slogbench.TestMessage,
57 zap.String("string", slogbench.TestString),
58 zap.Int("status", slogbench.TestInt),
59 zap.Duration("duration", slogbench.TestDuration),
60 zap.Time("time", slogbench.TestTime),
61 zap.Any("error", slogbench.TestError))
62 },
63 },
64 {
65 "10 args",
66 func() {
67 l.Info(slogbench.TestMessage,
68 zap.String("string", slogbench.TestString),
69 zap.Int("status", slogbench.TestInt),
70 zap.Duration("duration", slogbench.TestDuration),
71 zap.Time("time", slogbench.TestTime),
72 zap.Any("error", slogbench.TestError),
73 zap.String("string", slogbench.TestString),
74 zap.Int("status", slogbench.TestInt),
75 zap.Duration("duration", slogbench.TestDuration),
76 zap.Time("time", slogbench.TestTime),
77 zap.Any("error", slogbench.TestError))
78 },
79 },
80 {
81 "40 args",
82 func() {
83 l.Info(slogbench.TestMessage,
84 zap.String("string", slogbench.TestString),
85 zap.Int("status", slogbench.TestInt),
86 zap.Duration("duration", slogbench.TestDuration),
87 zap.Time("time", slogbench.TestTime),
88 zap.Any("error", slogbench.TestError),
89 zap.String("string", slogbench.TestString),
90 zap.Int("status", slogbench.TestInt),
91 zap.Duration("duration", slogbench.TestDuration),
92 zap.Time("time", slogbench.TestTime),
93 zap.Any("error", slogbench.TestError),
94 zap.String("string", slogbench.TestString),
95 zap.Int("status", slogbench.TestInt),
96 zap.Duration("duration", slogbench.TestDuration),
97 zap.Time("time", slogbench.TestTime),
98 zap.Any("error", slogbench.TestError),
99 zap.String("string", slogbench.TestString),
100 zap.Int("status", slogbench.TestInt),
101 zap.Duration("duration", slogbench.TestDuration),
102 zap.Time("time", slogbench.TestTime),
103 zap.Any("error", slogbench.TestError),
104 zap.String("string", slogbench.TestString),
105 zap.Int("status", slogbench.TestInt),
106 zap.Duration("duration", slogbench.TestDuration),
107 zap.Time("time", slogbench.TestTime),
108 zap.Any("error", slogbench.TestError),
109 zap.String("string", slogbench.TestString),
110 zap.Int("status", slogbench.TestInt),
111 zap.Duration("duration", slogbench.TestDuration),
112 zap.Time("time", slogbench.TestTime),
113 zap.Any("error", slogbench.TestError),
114 zap.String("string", slogbench.TestString),
115 zap.Int("status", slogbench.TestInt),
116 zap.Duration("duration", slogbench.TestDuration),
117 zap.Time("time", slogbench.TestTime),
118 zap.Any("error", slogbench.TestError),
119 zap.String("string", slogbench.TestString),
120 zap.Int("status", slogbench.TestInt),
121 zap.Duration("duration", slogbench.TestDuration),
122 zap.Time("time", slogbench.TestTime),
123 zap.Any("error", slogbench.TestError))
124 },
125 },
126 } {
127 b.Run(call.name, func(b *testing.B) {
128 b.ReportAllocs()
129 b.RunParallel(func(pb *testing.PB) {
130 for pb.Next() {
131 call.f()
132 }
133 })
134 })
135 }
136 })
137 }
138 }