Package list golang-github-go-kit-kit / 795adb1
log: More detailed benchmarks for experimental_level package. Chris Hines 4 years ago
1 changed file(s) with 57 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
77 "github.com/go-kit/kit/log/experimental_level"
88 )
99
10 func BenchmarkNopBaseline(b *testing.B) {
11 benchmarkRunner(b, log.NewNopLogger())
12 }
10 func Benchmark(b *testing.B) {
11 contexts := []struct {
12 name string
13 context func(log.Logger) log.Logger
14 }{
15 {"NoContext", func(l log.Logger) log.Logger {
16 return l
17 }},
18 {"TimeContext", func(l log.Logger) log.Logger {
19 return log.NewContext(l).With("time", log.DefaultTimestampUTC)
20 }},
21 {"CallerContext", func(l log.Logger) log.Logger {
22 return log.NewContext(l).With("caller", log.DefaultCaller)
23 }},
24 {"TimeCallerReqIDContext", func(l log.Logger) log.Logger {
25 return log.NewContext(l).With("time", log.DefaultTimestampUTC, "caller", log.DefaultCaller, "reqID", 29)
26 }},
27 }
1328
14 func BenchmarkNopDisallowedLevel(b *testing.B) {
15 benchmarkRunner(b, level.New(log.NewNopLogger(),
16 level.Allowed(level.AllowInfoAndAbove())))
17 }
29 loggers := []struct {
30 name string
31 logger log.Logger
32 }{
33 {"Nop", log.NewNopLogger()},
34 {"Logfmt", log.NewLogfmtLogger(ioutil.Discard)},
35 {"JSON", log.NewJSONLogger(ioutil.Discard)},
36 }
1837
19 func BenchmarkNopAllowedLevel(b *testing.B) {
20 benchmarkRunner(b, level.New(log.NewNopLogger(),
21 level.Allowed(level.AllowAll())))
22 }
38 filters := []struct {
39 name string
40 filter func(log.Logger) log.Logger
41 }{
42 {"Baseline", func(l log.Logger) log.Logger {
43 return l
44 }},
45 {"DisallowedLevel", func(l log.Logger) log.Logger {
46 return level.New(l, level.Allowed(level.AllowInfoAndAbove()))
47 }},
48 {"AllowedLevel", func(l log.Logger) log.Logger {
49 return level.New(l, level.Allowed(level.AllowAll()))
50 }},
51 }
2352
24 func BenchmarkJSONBaseline(b *testing.B) {
25 benchmarkRunner(b, log.NewJSONLogger(ioutil.Discard))
26 }
27
28 func BenchmarkJSONDisallowedLevel(b *testing.B) {
29 benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard),
30 level.Allowed(level.AllowInfoAndAbove())))
31 }
32
33 func BenchmarkJSONAllowedLevel(b *testing.B) {
34 benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard),
35 level.Allowed(level.AllowAll())))
36 }
37
38 func BenchmarkLogfmtBaseline(b *testing.B) {
39 benchmarkRunner(b, log.NewLogfmtLogger(ioutil.Discard))
40 }
41
42 func BenchmarkLogfmtDisallowedLevel(b *testing.B) {
43 benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard),
44 level.Allowed(level.AllowInfoAndAbove())))
45 }
46
47 func BenchmarkLogfmtAllowedLevel(b *testing.B) {
48 benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard),
49 level.Allowed(level.AllowAll())))
50 }
51
52 func benchmarkRunner(b *testing.B, logger log.Logger) {
53 b.ResetTimer()
54 b.ReportAllocs()
55 for i := 0; i < b.N; i++ {
56 level.Debug(logger).Log("foo", "bar")
53 for _, c := range contexts {
54 b.Run(c.name, func(b *testing.B) {
55 for _, f := range filters {
56 b.Run(f.name, func(b *testing.B) {
57 for _, l := range loggers {
58 b.Run(l.name, func(b *testing.B) {
59 logger := c.context(f.filter(l.logger))
60 b.ResetTimer()
61 b.ReportAllocs()
62 for i := 0; i < b.N; i++ {
63 level.Debug(logger).Log("foo", "bar")
64 }
65 })
66 }
67 })
68 }
69 })
5770 }
5871 }