Skip to content

Commit f26e945

Browse files
committed
add benchmark
1 parent 4bd7fce commit f26e945

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed

benchmark_test.go

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,94 @@ import (
44
"testing"
55
)
66

7-
// go test -run=benchmark -bench=. -benchtime="3s"
7+
// go test -run=benchmark -cpu=1,2,4 -benchmem -benchtime=3s -bench="ConsoleText"
8+
func BenchmarkLoggerConsoleText(b *testing.B) {
9+
logger := NewLogger()
10+
b.ResetTimer()
11+
b.RunParallel(func(pb *testing.PB) {
12+
for pb.Next() {
13+
logger.Info("benchmark logger message")
14+
}
15+
})
16+
}
817

9-
func BenchmarkLoggerConsole(b *testing.B) {
10-
b.ReportAllocs()
18+
// go test -run=benchmark -cpu=1,2,4 -benchmem -benchtime=3s -bench="ConsoleAsyncText"
19+
func BenchmarkLoggerConsoleAsyncText(b *testing.B) {
1120
logger := NewLogger()
21+
logger.SetAsync()
22+
1223
b.ResetTimer()
1324
b.RunParallel(func(pb *testing.PB) {
1425
for pb.Next() {
1526
logger.Info("benchmark logger message")
1627
}
1728
})
29+
logger.Flush()
1830
}
31+
32+
// go test -run=benchmark -cpu=1,2,4 -benchmem -benchtime=3s -bench="ConsoleJson"
33+
func BenchmarkLoggerConsoleJson(b *testing.B) {
34+
logger := NewLogger()
35+
logger.Detach("console")
36+
logger.Attach("console", LOGGER_LEVEL_DEBUG, &ConsoleConfig{
37+
JsonFormat: true,
38+
})
39+
b.ResetTimer()
40+
b.RunParallel(func(pb *testing.PB) {
41+
for pb.Next() {
42+
logger.Info("benchmark logger message")
43+
}
44+
})
45+
}
46+
47+
// go test -run=benchmark -cpu=1,2,4 -benchmem -benchtime=3s -bench="FileText"
48+
func BenchmarkLoggerFileText(b *testing.B) {
49+
logger := NewLogger()
50+
logger.Detach("console")
51+
logger.Attach("file", LOGGER_LEVEL_DEBUG, &FileConfig{
52+
Filename:"./test.log",
53+
DateSlice: "d",
54+
})
55+
b.ResetTimer()
56+
b.RunParallel(func(pb *testing.PB) {
57+
for pb.Next() {
58+
logger.Info("benchmark logger message")
59+
}
60+
})
61+
}
62+
63+
// go test -run=benchmark -cpu=1,2,4 -benchmem -benchtime=3s -bench="AsyncText"
64+
func BenchmarkLoggerFileAsyncText(b *testing.B) {
65+
logger := NewLogger()
66+
logger.Detach("console")
67+
logger.Attach("file", LOGGER_LEVEL_DEBUG, &FileConfig{
68+
Filename:"./test.log",
69+
DateSlice: "d",
70+
})
71+
logger.SetAsync()
72+
73+
b.ResetTimer()
74+
b.RunParallel(func(pb *testing.PB) {
75+
for pb.Next() {
76+
logger.Info("benchmark logger message")
77+
}
78+
})
79+
logger.Flush()
80+
}
81+
82+
// go test -run=benchmark -cpu=1,2,4 -benchmem -benchtime=3s -bench="FileJson"
83+
func BenchmarkLoggerFileJson(b *testing.B) {
84+
logger := NewLogger()
85+
logger.Detach("console")
86+
logger.Attach("file", LOGGER_LEVEL_DEBUG, &FileConfig{
87+
Filename:"./test.log",
88+
DateSlice: "d",
89+
JsonFormat: true,
90+
})
91+
b.ResetTimer()
92+
b.RunParallel(func(pb *testing.PB) {
93+
for pb.Next() {
94+
logger.Info("benchmark logger message")
95+
}
96+
})
97+
}

logger.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (logger *Logger) Detach(adapterName string) error {
159159
func (logger *Logger) detach(adapterName string) error {
160160
outputs := []*outputLogger{}
161161
for _, output := range logger.outputs {
162-
if(output.Name == adapterName) {
162+
if output.Name == adapterName {
163163
continue
164164
}
165165
outputs = append(outputs, output)
@@ -212,13 +212,11 @@ func (logger *Logger) Writer(level int, msg string) error {
212212
file = "null"
213213
line = 0
214214
}else {
215-
fun := runtime.FuncForPC(pc)
216-
funcName = fun.Name()
215+
funcName = runtime.FuncForPC(pc).Name()
217216
}
218217
_, filename := path.Split(file)
219218

220-
levelString := levelStringMapping[level]
221-
if(levelString == "") {
219+
if levelStringMapping[level] == "" {
222220
printError("logger: level " + strconv.Itoa(level) + " is illegal!")
223221
}
224222

@@ -228,7 +226,7 @@ func (logger *Logger) Writer(level int, msg string) error {
228226
Millisecond : time.Now().UnixNano()/1e6,
229227
MillisecondFormat : time.Now().Format("2006-01-02 15:04:05.999"),
230228
Level :level,
231-
LevelString: levelString,
229+
LevelString: levelStringMapping[level],
232230
Body: msg,
233231
File : filename,
234232
Line : line,

logger_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ func TestNewLogger(t *testing.T) {
1313
func TestLogger_Attach(t *testing.T) {
1414

1515
logger := NewLogger()
16-
fileConfig := &FileConfig{}
16+
fileConfig := &FileConfig{
17+
Filename:"./test.log",
18+
}
1719
logger.Attach("file", LOGGER_LEVEL_DEBUG, fileConfig)
1820
outputs := logger.outputs
1921
for _, outputLogger := range outputs {

0 commit comments

Comments
 (0)