File: bench_test.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20231006.7918f67-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 6,492 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 27
file content (115 lines) | stat: -rw-r--r-- 3,506 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zap_test

import (
	"context"
	"io"
	"testing"
	"time"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"golang.org/x/exp/event/eventtest"
)

var (
	zapLog = eventtest.Hooks{
		AStart: func(ctx context.Context, a int) context.Context {
			zapCtx(ctx).Info(eventtest.A.Msg, zap.Int(eventtest.A.Name, a))
			return ctx
		},
		AEnd: func(ctx context.Context) {},
		BStart: func(ctx context.Context, b string) context.Context {
			zapCtx(ctx).Info(eventtest.B.Msg, zap.String(eventtest.B.Name, b))
			return ctx
		},
		BEnd: func(ctx context.Context) {},
	}
	zapLogf = eventtest.Hooks{
		AStart: func(ctx context.Context, a int) context.Context {
			zapCtx(ctx).Sugar().Infof(eventtest.A.Msgf, a)
			return ctx
		},
		AEnd: func(ctx context.Context) {},
		BStart: func(ctx context.Context, b string) context.Context {
			zapCtx(ctx).Sugar().Infof(eventtest.B.Msgf, b)
			return ctx
		},
		BEnd: func(ctx context.Context) {},
	}
)

type zapKey struct{}

func zapCtx(ctx context.Context) *zap.Logger {
	return ctx.Value(zapKey{}).(*zap.Logger)
}

func zapPrint(w io.Writer) context.Context {
	now := eventtest.ExporterOptions().Now
	ec := zap.NewProductionEncoderConfig()
	ec.EncodeDuration = zapcore.NanosDurationEncoder
	timeEncoder := zapcore.TimeEncoderOfLayout(eventtest.TimeFormat)
	ec.EncodeTime = func(_ time.Time, a zapcore.PrimitiveArrayEncoder) {
		timeEncoder(now(), a)
	}
	enc := zapcore.NewConsoleEncoder(ec)
	logger := zap.New(zapcore.NewCore(
		enc,
		zapcore.AddSync(w),
		zap.InfoLevel,
	))
	return context.WithValue(context.Background(), zapKey{}, logger)
}

func BenchmarkZapLogDiscard(b *testing.B) {
	eventtest.RunBenchmark(b, zapPrint(io.Discard), zapLog)
}

func BenchmarkZapLogfDiscard(b *testing.B) {
	eventtest.RunBenchmark(b, zapPrint(io.Discard), zapLogf)
}

func TestZapLogfDiscard(t *testing.T) {
	eventtest.TestBenchmark(t, zapPrint, zapLogf, `
2020/03/05 14:27:48	info	a where A=0
2020/03/05 14:27:49	info	b where B="A value"
2020/03/05 14:27:50	info	a where A=1
2020/03/05 14:27:51	info	b where B="Some other value"
2020/03/05 14:27:52	info	a where A=22
2020/03/05 14:27:53	info	b where B="Some other value"
2020/03/05 14:27:54	info	a where A=333
2020/03/05 14:27:55	info	b where B=" "
2020/03/05 14:27:56	info	a where A=4444
2020/03/05 14:27:57	info	b where B="prime count of values"
2020/03/05 14:27:58	info	a where A=55555
2020/03/05 14:27:59	info	b where B="V"
2020/03/05 14:28:00	info	a where A=666666
2020/03/05 14:28:01	info	b where B="A value"
2020/03/05 14:28:02	info	a where A=7777777
2020/03/05 14:28:03	info	b where B="A value"
`)
}
func TestLogZap(t *testing.T) {
	eventtest.TestBenchmark(t, zapPrint, zapLog, `
2020/03/05 14:27:48	info	a	{"A": 0}
2020/03/05 14:27:49	info	b	{"B": "A value"}
2020/03/05 14:27:50	info	a	{"A": 1}
2020/03/05 14:27:51	info	b	{"B": "Some other value"}
2020/03/05 14:27:52	info	a	{"A": 22}
2020/03/05 14:27:53	info	b	{"B": "Some other value"}
2020/03/05 14:27:54	info	a	{"A": 333}
2020/03/05 14:27:55	info	b	{"B": " "}
2020/03/05 14:27:56	info	a	{"A": 4444}
2020/03/05 14:27:57	info	b	{"B": "prime count of values"}
2020/03/05 14:27:58	info	a	{"A": 55555}
2020/03/05 14:27:59	info	b	{"B": "V"}
2020/03/05 14:28:00	info	a	{"A": 666666}
2020/03/05 14:28:01	info	b	{"B": "A value"}
2020/03/05 14:28:02	info	a	{"A": 7777777}
2020/03/05 14:28:03	info	b	{"B": "A value"}
`)
}