-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathctxlog_bench_test.go
More file actions
39 lines (35 loc) · 992 Bytes
/
ctxlog_bench_test.go
File metadata and controls
39 lines (35 loc) · 992 Bytes
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
package ctxlog
import (
"context"
"testing"
)
// BenchmarkFromContextWithLoggerGetEveryTime benches when we retrieve logger from context every time
// 14850 ns/op
func BenchmarkFromContextWithLoggerGetEveryTime(b *testing.B) {
ctx := AddLoggerForRequest(testRequest())
for i := 0; i < b.N; i++ {
for i := 0; i < 20; i++ {
FromContext(ctx).Info("hello")
}
}
}
// BenchmarkFromContextWithLoggerGetOnlyOnce benches when we retrieve logger only once and keep using it
// 17059 ns/op
func BenchmarkFromContextWithLoggerGetOnlyOnce(b *testing.B) {
ctx := AddLoggerForRequest(testRequest())
for i := 0; i < b.N; i++ {
logger := FromContext(ctx)
for i := 0; i < 20; i++ {
logger.Info("hello")
}
}
}
// BenchmarkFromContextWithoutLogger benches what happens if logger is not initialized
// 22060 ns/op
func BenchmarkFromContextWithoutLogger(b *testing.B) {
for i := 0; i < b.N; i++ {
for i := 0; i < 20; i++ {
FromContext(context.Background()).Info("hello")
}
}
}