forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
83 lines (65 loc) · 1.82 KB
/
benchmark_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
71
72
73
74
75
76
77
78
79
80
81
82
83
package slog_test
import (
"io/ioutil"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
)
// go test -v -cpu=4 -run=none -bench=. -benchtime=10s -benchmem bench_test.go
//
// code refer:
// https://github.com/phuslu/log
var msg = "The quick brown fox jumps over the lazy dog"
func BenchmarkGookitSlogNegative(b *testing.B) {
logger := slog.NewWithHandlers(
handler.NewIOWriter(ioutil.Discard, []slog.Level{slog.ErrorLevel}),
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
}
}
func TestLogger_Info_Negative(t *testing.T) {
logger := slog.NewWithHandlers(
handler.NewIOWriter(ioutil.Discard, []slog.Level{slog.ErrorLevel}),
)
logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
}
func BenchmarkGookitSlogPositive(b *testing.B) {
logger := slog.NewWithHandlers(
handler.NewIOWriter(ioutil.Discard, slog.NormalLevels),
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
}
}
func BenchmarkTextFormatter_Format(b *testing.B) {
r := newLogRecord("TEST_LOG_MESSAGE")
f := slog.NewTextFormatter()
// 1284 ns/op 456 B/op 11 allocs/op
// On use DefaultTemplate
// 304.4 ns/op 200 B/op 2 allocs/op
// f.SetTemplate("{{datetime}} {{message}}")
// 271.3 ns/op 200 B/op 2 allocs/op
// f.SetTemplate("{{datetime}}")
// f.SetTemplate("{{message}}")
dump.P(f.Template)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := f.Format(r)
if err != nil {
panic(err)
}
}
}
func TestLogger_Info_Positive(t *testing.T) {
logger := slog.NewWithHandlers(
handler.NewIOWriter(ioutil.Discard, slog.NormalLevels),
)
logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
}