Debian Codegraph @

event/adapter/zerolog/bench_test.go

raw symbols SCIP
1 // Copyright 2020 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 zerolog_test
6
7 import (
8 "context"
9 "io"
10 "testing"
11
12 "github.com/rs/zerolog"
13 "golang.org/x/exp/event/eventtest"
14 )
15
16 var (
17 zerologMsg = eventtest.Hooks{
18 AStart: func(ctx context.Context, a int) context.Context {
19 zerolog.Ctx(ctx).Info().Int(eventtest.A.Name, a).Msg(eventtest.A.Msg)
20 return ctx
21 },
22 AEnd: func(ctx context.Context) {},
23 BStart: func(ctx context.Context, b string) context.Context {
24 zerolog.Ctx(ctx).Info().Str(eventtest.B.Name, b).Msg(eventtest.B.Msg)
25 return ctx
26 },
27 BEnd: func(ctx context.Context) {},
28 }
29
30 zerologMsgf = eventtest.Hooks{
31 AStart: func(ctx context.Context, a int) context.Context {
32 zerolog.Ctx(ctx).Info().Msgf(eventtest.A.Msgf, a)
33 return ctx
34 },
35 AEnd: func(ctx context.Context) {},
36 BStart: func(ctx context.Context, b string) context.Context {
37 zerolog.Ctx(ctx).Info().Msgf(eventtest.B.Msgf, b)
38 return ctx
39 },
40 BEnd: func(ctx context.Context) {},
41 }
42 )
43
44 func zerologPrint(w io.Writer) context.Context {
45 zerolog.TimeFieldFormat = eventtest.TimeFormat
46 zerolog.TimestampFunc = eventtest.ExporterOptions().Now
47 logger := zerolog.New(zerolog.SyncWriter(w)).With().Timestamp().Logger()
48 return logger.WithContext(context.Background())
49 }
50
51 func BenchmarkZerologLogDiscard(b *testing.B) {
52 eventtest.RunBenchmark(b, zerologPrint(io.Discard), zerologMsg)
53 }
54
55 func BenchmarkZerologLogfDiscard(b *testing.B) {
56 eventtest.RunBenchmark(b, zerologPrint(io.Discard), zerologMsgf)
57 }
58
59 func TestLogZerologf(t *testing.T) {
60 eventtest.TestBenchmark(t, zerologPrint, zerologMsgf, `
61 {"level":"info","time":"2020/03/05 14:27:48","message":"a where A=0"}
62 {"level":"info","time":"2020/03/05 14:27:49","message":"b where B=\"A value\""}
63 {"level":"info","time":"2020/03/05 14:27:50","message":"a where A=1"}
64 {"level":"info","time":"2020/03/05 14:27:51","message":"b where B=\"Some other value\""}
65 {"level":"info","time":"2020/03/05 14:27:52","message":"a where A=22"}
66 {"level":"info","time":"2020/03/05 14:27:53","message":"b where B=\"Some other value\""}
67 {"level":"info","time":"2020/03/05 14:27:54","message":"a where A=333"}
68 {"level":"info","time":"2020/03/05 14:27:55","message":"b where B=\" \""}
69 {"level":"info","time":"2020/03/05 14:27:56","message":"a where A=4444"}
70 {"level":"info","time":"2020/03/05 14:27:57","message":"b where B=\"prime count of values\""}
71 {"level":"info","time":"2020/03/05 14:27:58","message":"a where A=55555"}
72 {"level":"info","time":"2020/03/05 14:27:59","message":"b where B=\"V\""}
73 {"level":"info","time":"2020/03/05 14:28:00","message":"a where A=666666"}
74 {"level":"info","time":"2020/03/05 14:28:01","message":"b where B=\"A value\""}
75 {"level":"info","time":"2020/03/05 14:28:02","message":"a where A=7777777"}
76 {"level":"info","time":"2020/03/05 14:28:03","message":"b where B=\"A value\""}
77 `)
78 }