summaryrefslogtreecommitdiffstats
path: root/event/adapter/zerolog/bench_test.go
blob: 480754dac608ac98f4842ef72e56c3ea61c139b0 (plain)
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
// 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 zerolog_test

import (
	"context"
	"io"
	"testing"

	"github.com/rs/zerolog"
	"golang.org/x/exp/event/eventtest"
)

var (
	zerologMsg = eventtest.Hooks{
		AStart: func(ctx context.Context, a int) context.Context {
			zerolog.Ctx(ctx).Info().Int(eventtest.A.Name, a).Msg(eventtest.A.Msg)
			return ctx
		},
		AEnd: func(ctx context.Context) {},
		BStart: func(ctx context.Context, b string) context.Context {
			zerolog.Ctx(ctx).Info().Str(eventtest.B.Name, b).Msg(eventtest.B.Msg)
			return ctx
		},
		BEnd: func(ctx context.Context) {},
	}

	zerologMsgf = eventtest.Hooks{
		AStart: func(ctx context.Context, a int) context.Context {
			zerolog.Ctx(ctx).Info().Msgf(eventtest.A.Msgf, a)
			return ctx
		},
		AEnd: func(ctx context.Context) {},
		BStart: func(ctx context.Context, b string) context.Context {
			zerolog.Ctx(ctx).Info().Msgf(eventtest.B.Msgf, b)
			return ctx
		},
		BEnd: func(ctx context.Context) {},
	}
)

func zerologPrint(w io.Writer) context.Context {
	zerolog.TimeFieldFormat = eventtest.TimeFormat
	zerolog.TimestampFunc = eventtest.ExporterOptions().Now
	logger := zerolog.New(zerolog.SyncWriter(w)).With().Timestamp().Logger()
	return logger.WithContext(context.Background())
}

func BenchmarkZerologLogDiscard(b *testing.B) {
	eventtest.RunBenchmark(b, zerologPrint(io.Discard), zerologMsg)
}

func BenchmarkZerologLogfDiscard(b *testing.B) {
	eventtest.RunBenchmark(b, zerologPrint(io.Discard), zerologMsgf)
}

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