-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogformat_test.go
137 lines (118 loc) · 3.69 KB
/
logformat_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"unsafe"
log "github.com/sirupsen/logrus"
)
const (
testTimeFormat = "2006-01-02 15:04:05 -0700"
)
type BufferFormatter struct {
timeFormat string
}
func (f *BufferFormatter) Format(entry *log.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
b.WriteString(entry.Time.Format(f.timeFormat))
b.WriteString(" [")
b.WriteString(entry.Level.String())
b.WriteString("]: ")
b.WriteString(entry.Message)
b.WriteByte('\n')
return b.Bytes(), nil
}
type StringAddFormatter struct {
timeFormat string
}
func (f *StringAddFormatter) Format(entry *log.Entry) ([]byte, error) {
var b string
b = entry.Time.Format(f.timeFormat) + " [" + entry.Level.String() + "]: " + entry.Message + "\n"
return []byte(b), nil
}
type StringAddUnsafeFormatter struct {
timeFormat string
}
func (f *StringAddUnsafeFormatter) Format(entry *log.Entry) ([]byte, error) {
var s string
s = entry.Time.Format(f.timeFormat) + " [" + entry.Level.String() + "]: " + entry.Message + "\n"
return *(*[]byte)(unsafe.Pointer(&s)), nil
}
type StringBuilderFormatter struct {
timeFormat string
}
func (f *StringBuilderFormatter) Format(entry *log.Entry) ([]byte, error) {
var builder strings.Builder
builder.WriteString(entry.Time.Format(f.timeFormat))
builder.WriteString(" [")
builder.WriteString(entry.Level.String())
builder.WriteString("]: ")
builder.WriteString(entry.Message)
builder.WriteByte('\n')
return []byte(builder.String()), nil
}
type StringBuilderUnsafeFormatter struct {
timeFormat string
}
func (f *StringBuilderUnsafeFormatter) Format(entry *log.Entry) ([]byte, error) {
var builder strings.Builder
builder.WriteString(entry.Time.Format(f.timeFormat))
builder.WriteString(" [")
builder.WriteString(entry.Level.String())
builder.WriteString("]: ")
builder.WriteString(entry.Message)
builder.WriteByte('\n')
s := builder.String()
return *(*[]byte)(unsafe.Pointer(&s)), nil
}
func BenchmarkFormatBuffer(b *testing.B) {
formatter := &BufferFormatter{timeFormat: "2006-01-02 15:04:05 -0700"}
log.SetFormatter(formatter)
log.SetLevel(log.DebugLevel)
log.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
log.Debugf("%d This is a test message blah blah blah blah blah blah blah blah blah blah ...", i)
}
}
func BenchmarkFormatStringAdd(b *testing.B) {
formatter := &StringAddFormatter{timeFormat: "2006-01-02 15:04:05 -0700"}
log.SetFormatter(formatter)
log.SetLevel(log.DebugLevel)
log.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
log.Debugf("%d This is a test message blah blah blah blah blah blah blah blah blah blah ...", i)
}
}
func BenchmarkFormatStringAddUnsafe(b *testing.B) {
formatter := &StringAddUnsafeFormatter{timeFormat: "2006-01-02 15:04:05 -0700"}
log.SetFormatter(formatter)
log.SetLevel(log.DebugLevel)
log.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
log.Debugf("%d This is a test message blah blah blah blah blah blah blah blah blah blah ...", i)
}
}
func BenchmarkFormatBuilder(b *testing.B) {
formatter := &StringBuilderFormatter{timeFormat: "2006-01-02 15:04:05 -0700"}
log.SetFormatter(formatter)
log.SetLevel(log.DebugLevel)
log.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
log.Debugf("%d This is a test message blah blah blah blah blah blah blah blah blah blah ...", i)
}
}
func BenchmarkFormatBuilderUnsafe(b *testing.B) {
formatter := &StringBuilderUnsafeFormatter{timeFormat: "2006-01-02 15:04:05 -0700"}
log.SetFormatter(formatter)
log.SetLevel(log.DebugLevel)
log.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
log.Debugf("%d This is a test message blah blah blah blah blah blah blah blah blah blah ...", i)
}
}