-
Notifications
You must be signed in to change notification settings - Fork 26
/
benchmark2_test.go
116 lines (95 loc) · 2.74 KB
/
benchmark2_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package slog
import (
"fmt"
"io"
"testing"
"github.com/gookit/goutil/dump"
)
func TestLogger_newRecord_AllocTimes(_ *testing.T) {
l := Std()
l.Output = io.Discard
defer l.Reset()
// output: 0 times
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
// logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
r := l.newRecord()
// do something...
l.releaseRecord(r)
})))
}
func Test_formatArgsWithSpaces_oneElem_AllocTimes(_ *testing.T) {
// output: 1 times -> 0 times
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(10, func() {
// logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
formatArgsWithSpaces([]any{
"msg", // 2343, -23, 123.2,
})
})))
}
func Test_AllocTimes_formatArgsWithSpaces_manyElem(_ *testing.T) {
l := Std()
l.Output = io.Discard
defer l.Reset()
// TIP:
// `float` will alloc 2 times memory
// `int <0`, `int > 100` will alloc 1 times memory
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
// logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
formatArgsWithSpaces([]any{
"rate", -23, true, 106, "high", 123.2,
})
})))
}
func Test_AllocTimes_stringsPool(_ *testing.T) {
l := Std()
l.Output = io.Discard
l.LowerLevelName = true
defer l.Reset()
var ln, cp int
// output: 0 times
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
// logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
// oldnew := stringsPool.Get().([]string)
// defer stringsPool.Put(oldnew)
oldnew := make([]string, 0, len(map[string]string{"a": "b"})*2+1)
oldnew = append(oldnew, "a")
oldnew = append(oldnew, "b")
oldnew = append(oldnew, "c")
// oldnew = append(oldnew, "d")
ln = len(oldnew)
cp = cap(oldnew)
})))
dump.P(ln, cp)
}
func TestLogger_Info_oneElem_AllocTimes(_ *testing.T) {
l := Std()
// l.Output = io.Discard
l.ReportCaller = false
l.LowerLevelName = true
// 启用 color 会导致多次(10次左右)内存分配
l.Formatter.(*TextFormatter).EnableColor = false
defer l.Reset()
// output: 2 times
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(5, func() {
// l.Info("rate", "15", "low", 16, "high", 123.2, "msg")
l.Info("msg")
})))
}
func TestLogger_Info_moreElem_AllocTimes(_ *testing.T) {
l := NewStdLogger()
// l.Output = io.Discard
l.ReportCaller = false
l.LowerLevelName = true
// 启用 color 会导致多次(10次左右)内存分配
l.Formatter.(*TextFormatter).EnableColor = false
defer l.Reset()
// output: 5 times
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(5, func() {
l.Info("rate", "15", "low", 16, "high", 123.2, "msg")
})))
// output: 5 times
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(5, func() {
l.Info("rate", "15", "low", 16, "high")
// l.Info("msg")
})))
}