log/experimental_level: add basic benchmarks
Peter Bourgon
7 years ago
0 | package level_test | |
1 | ||
2 | import ( | |
3 | "io/ioutil" | |
4 | "testing" | |
5 | ||
6 | "github.com/go-kit/kit/log" | |
7 | "github.com/go-kit/kit/log/experimental_level" | |
8 | ) | |
9 | ||
10 | func BenchmarkNopBaseline(b *testing.B) { | |
11 | benchmarkRunner(b, log.NewNopLogger()) | |
12 | } | |
13 | ||
14 | func BenchmarkNopDisallowedLevel(b *testing.B) { | |
15 | benchmarkRunner(b, level.New(log.NewNopLogger(), level.Config{ | |
16 | Allowed: level.AllowInfoAndAbove, | |
17 | })) | |
18 | } | |
19 | ||
20 | func BenchmarkNopAllowedLevel(b *testing.B) { | |
21 | benchmarkRunner(b, level.New(log.NewNopLogger(), level.Config{ | |
22 | Allowed: level.AllowAll, | |
23 | })) | |
24 | } | |
25 | ||
26 | func BenchmarkJSONBaseline(b *testing.B) { | |
27 | benchmarkRunner(b, log.NewJSONLogger(ioutil.Discard)) | |
28 | } | |
29 | ||
30 | func BenchmarkJSONDisallowedLevel(b *testing.B) { | |
31 | benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard), level.Config{ | |
32 | Allowed: level.AllowInfoAndAbove, | |
33 | })) | |
34 | } | |
35 | ||
36 | func BenchmarkJSONAllowedLevel(b *testing.B) { | |
37 | benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard), level.Config{ | |
38 | Allowed: level.AllowAll, | |
39 | })) | |
40 | } | |
41 | ||
42 | func BenchmarkLogfmtBaseline(b *testing.B) { | |
43 | benchmarkRunner(b, log.NewLogfmtLogger(ioutil.Discard)) | |
44 | } | |
45 | ||
46 | func BenchmarkLogfmtDisallowedLevel(b *testing.B) { | |
47 | benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard), level.Config{ | |
48 | Allowed: level.AllowInfoAndAbove, | |
49 | })) | |
50 | } | |
51 | ||
52 | func BenchmarkLogfmtAllowedLevel(b *testing.B) { | |
53 | benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard), level.Config{ | |
54 | Allowed: level.AllowAll, | |
55 | })) | |
56 | } | |
57 | ||
58 | func benchmarkRunner(b *testing.B, logger log.Logger) { | |
59 | b.ResetTimer() | |
60 | b.ReportAllocs() | |
61 | for i := 0; i < b.N; i++ { | |
62 | level.Debug(logger).Log("foo", "bar") | |
63 | } | |
64 | } |