Skip to content

Commit

Permalink
perf!: 添加 Handler.WithAttrs 方法
Browse files Browse the repository at this point in the history
WithAttrs 可以缓存处理后的内容,对于具有固定属性值的实现,性能提升明显。
  • Loading branch information
caixw committed Nov 6, 2023
1 parent f9fa2e4 commit 92a08b8
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 308 deletions.
65 changes: 21 additions & 44 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,38 @@ import (

"github.com/issue9/assert/v3"
"github.com/issue9/localeutil"
"github.com/issue9/term/v3/colors"
)

func BenchmarkTextHandler(b *testing.B) {
a := assert.New(b, false)

buf := new(bytes.Buffer)
w := NewTextHandler(buf)
l := New(w, WithLocation(true), WithCreated(MilliLayout))
e := newRecord(a, l, LevelWarn)

for i := 0; i < b.N; i++ {
w.Handle(e)
}
benchHandler(NewTextHandler(new(bytes.Buffer)), b)
}

func BenchmarkJSONHandler(b *testing.B) {
a := assert.New(b, false)

buf := new(bytes.Buffer)
w := NewJSONHandler(buf)
l := New(w, WithLocation(true), WithCreated(MicroLayout))
e := newRecord(a, l, LevelWarn)

for i := 0; i < b.N; i++ {
w.Handle(e)
}
benchHandler(NewJSONHandler(new(bytes.Buffer)), b)
}

func BenchmarkTermHandler(b *testing.B) {
a := assert.New(b, false)
benchHandler(NewTermHandler(new(bytes.Buffer), nil), b)
}

buf := new(bytes.Buffer)
w := NewTermHandler(buf, map[Level]colors.Color{LevelWarn: colors.Blue})
l := New(w, WithLocation(true), WithCreated(MilliLayout))
e := newRecord(a, l, LevelWarn)
func benchHandler(h Handler, b *testing.B) {
b.Run("default", func(b *testing.B) {
l := New(h)
e := l.ERROR().With("k1", "v1").With("k2", 2).With("k3", localeutil.Phrase("lang"))

for i := 0; i < b.N; i++ {
w.Handle(e)
}
for i := 0; i < b.N; i++ {
e.String("err")
}
})

b.Run("withAttr", func(b *testing.B) {
l := New(h)
err := l.ERROR().New(map[string]any{"k1": "v1", "k2": 2, "k3": localeutil.Phrase("lang")})

for i := 0; i < b.N; i++ {
err.String("err")
}
})
}

func BenchmarkRecord_Printf(b *testing.B) {
Expand All @@ -64,21 +56,6 @@ func BenchmarkRecord_Printf(b *testing.B) {
}
}

func BenchmarkLogger_withAttrs(b *testing.B) {
a := assert.New(b, false)
buf := new(bytes.Buffer)
l := New(NewTextHandler(buf))
a.NotNil(l)
l.Enable(LevelError)

err := l.ERROR()
err.AppendAttrs(map[string]any{"k1": "v1", "k2": 2, "k3": localeutil.Phrase("lang")})

for i := 0; i < b.N; i++ {
err.String("err")
}
}

func BenchmarkLogger(b *testing.B) {
a := assert.New(b, false)
buf := new(bytes.Buffer)
Expand Down
2 changes: 2 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func (w *Buffer) Detail() bool { return w.detail }

func (w *Buffer) Bytes() []byte { return w.data }

func (w *Buffer) Len() int { return len(w.data) }

func (w *Buffer) Free() {
const buffersPoolMaxSize = 1 << 10
if len(w.data) < buffersPoolMaxSize {
Expand Down
Loading

0 comments on commit 92a08b8

Please sign in to comment.