diff --git a/README.md b/README.md index ffec9efe..8ccd7562 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,6 @@ - `AsyncWriter`, *asynchronously & performant* - `MultiLevelWriter`, *multiple level dispatch* - `SyslogWriter`, *memory efficient syslog* - file - `JournalWriter`, *linux systemd logging* - - `EventlogWriter`, *windows system event* * Stdlib Log Adapter - `Logger.Std`, *transform to std log instances* - `Logger.Slog`, *transform to log/slog instances* diff --git a/async.go b/async.go index 043d9f89..984c1699 100644 --- a/async.go +++ b/async.go @@ -8,7 +8,7 @@ import ( "unsafe" ) -// AsyncWriter is an Writer that writes asynchronously. +// AsyncWriter is a Writer that writes asynchronously. type AsyncWriter struct { // Writer specifies the writer of output. Writer Writer @@ -42,6 +42,22 @@ func (w *AsyncWriter) Close() (err error) { var ErrAsyncWriterFull = errors.New("async writer is full") +var eepool = sync.Pool{ + New: func() any { + return new(Entry) + }, +} + +// Write implements io.Writer. +func (w *AsyncWriter) Write(p []byte) (n int, err error) { + e := eepool.Get().(*Entry) + e.buf = p + n, err = w.Writer.WriteEntry(e) + e.buf = nil + eepool.Put(e) + return +} + // WriteEntry implements Writer. func (w *AsyncWriter) WriteEntry(e *Entry) (int, error) { w.once.Do(func() { @@ -87,3 +103,4 @@ func (w *AsyncWriter) writer() { } var _ Writer = (*AsyncWriter)(nil) +var _ io.Writer = (*AsyncWriter)(nil) diff --git a/async_test.go b/async_test.go index bca37793..9565a9e2 100644 --- a/async_test.go +++ b/async_test.go @@ -118,3 +118,25 @@ func BenchmarkAsyncFileWriter(b *testing.B) { } }) } + +func BenchmarkAsyncIOWriter(b *testing.B) { + logger := TSVLogger{ + Writer: &AsyncWriter{ + ChannelSize: 4096, + WritevDisabled: false, + DiscardOnFull: false, + Writer: &FileWriter{ + Filename: "async_file_test3.log", + }, + }, + } + defer logger.Writer.(io.Closer).Close() + + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(b *testing.PB) { + for b.Next() { + logger.New().Str("hello file writer").Msg() + } + }) +}