Debian Codegraph @

event/adapter/logrus/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 logrus_test
6
7 import (
8 "context"
9 "io"
10 "testing"
11 "time"
12
13 "github.com/sirupsen/logrus"
14 "golang.org/x/exp/event/eventtest"
15 )
16
17 var (
18 logrusLog = eventtest.Hooks{
19 AStart: func(ctx context.Context, a int) context.Context {
20 logrusCtx(ctx).WithField(eventtest.A.Name, a).Info(eventtest.A.Msg)
21 return ctx
22 },
23 AEnd: func(ctx context.Context) {},
24 BStart: func(ctx context.Context, b string) context.Context {
25 logrusCtx(ctx).WithField(eventtest.B.Name, b).Info(eventtest.B.Msg)
26 return ctx
27 },
28 BEnd: func(ctx context.Context) {},
29 }
30
31 logrusLogf = eventtest.Hooks{
32 AStart: func(ctx context.Context, a int) context.Context {
33 logrusCtx(ctx).Infof(eventtest.A.Msgf, a)
34 return ctx
35 },
36 AEnd: func(ctx context.Context) {},
37 BStart: func(ctx context.Context, b string) context.Context {
38 logrusCtx(ctx).Infof(eventtest.B.Msgf, b)
39 return ctx
40 },
41 BEnd: func(ctx context.Context) {},
42 }
43 )
44
45 type logrusKey struct{}
46 type logrusTimeFormatter struct {
47 now func() time.Time
48 wrapped logrus.Formatter
49 }
50
51 func (f *logrusTimeFormatter) Format(entry *logrus.Entry) ([]byte, error) {
52 entry.Time = f.now()
53 return f.wrapped.Format(entry)
54 }
55
56 func logrusCtx(ctx context.Context) *logrus.Logger {
57 return ctx.Value(logrusKey{}).(*logrus.Logger)
58 }
59
60 func logrusPrint(w io.Writer) context.Context {
61 logger := &logrus.Logger{
62 Out: w,
63 Level: logrus.InfoLevel,
64 Formatter: &logrusTimeFormatter{
65 now: eventtest.ExporterOptions().Now,
66 wrapped: &logrus.TextFormatter{
67 FullTimestamp: true,
68 TimestampFormat: eventtest.TimeFormat,
69 DisableSorting: true,
70 DisableColors: true,
71 },
72 },
73 }
74 return context.WithValue(context.Background(), logrusKey{}, logger)
75 }
76
77 func BenchmarkLogrusLogDiscard(b *testing.B) {
78 eventtest.RunBenchmark(b, logrusPrint(io.Discard), logrusLog)
79 }
80
81 func BenchmarkLogrusLogfDiscard(b *testing.B) {
82 eventtest.RunBenchmark(b, logrusPrint(io.Discard), logrusLogf)
83 }
84
85 func TestLogrusf(t *testing.T) {
86 eventtest.TestBenchmark(t, logrusPrint, logrusLogf, eventtest.LogfOutput)
87 }