forked from open-telemetry/opentelemetry-ebpf-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_bench_test.go
70 lines (63 loc) · 1.46 KB
/
log_bench_test.go
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Apache License 2.0.
* See the file "LICENSE" for details.
*/
package log_test
import (
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/elastic/otel-profiling-agent/debug/log"
)
func BenchmarkBaselineLogrus(b *testing.B) {
cases := []struct {
name string
run func(string)
}{
{"Infof", func(s string) { logrus.Infof(s) }},
{"With_Dot_Infof", func(s string) {
logrus.WithFields(logrus.Fields{"a": "b"}).Infof(s)
}},
}
for i := range cases {
bench := cases[i]
b.Run(bench.name, func(b *testing.B) {
loggingCall(b, bench.run, logrus.StandardLogger())
})
}
}
func BenchmarkLogger(b *testing.B) {
cases := []struct {
name string
run func(string)
}{
{"Infof",
func(s string) { log.Infof(s) },
},
{"With_Dot_Infof",
func(s string) {
log.With(log.Labels{"a": "b"}).Infof(s)
},
},
}
for i := range cases {
bench := cases[i]
b.Run(bench.name, func(b *testing.B) {
loggingCall(b, bench.run, log.StandardLogger())
})
}
}
func loggingCall(b *testing.B, underTest func(string), logger log.Logger) {
b.StopTimer()
output := setupLogger(logger, b)
for i := 0; i < b.N; i++ {
b.StartTimer()
underTest("AAA")
b.StopTimer()
if !assert.Contains(b, output.String(), "AAA") {
b.Fatalf("mismatch in output text")
}
}
output.Reset()
}