From 93e69ea5e4783465a2cc2e88dd99e452e86c9c77 Mon Sep 17 00:00:00 2001 From: phuslu Date: Mon, 3 Jun 2024 14:11:58 +0800 Subject: [PATCH] add async writer benchmark --- async_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/async_test.go b/async_test.go index deeb3ff7..fbc1d7bf 100644 --- a/async_test.go +++ b/async_test.go @@ -32,19 +32,60 @@ func TestAsyncWriterSmall(t *testing.T) { } } -func BenchmarkAsyncWriter(b *testing.B) { +func BenchmarkSyncFileWriter(b *testing.B) { + logger := Logger{ + Writer: &FileWriter{ + Filename: "sync_file_test.log", + }, + } + defer logger.Writer.(io.Closer).Close() + + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(b *testing.PB) { + for b.Next() { + logger.Info().Msg("hello file writer") + } + }) +} + +func BenchmarkAsyncFileWriter(b *testing.B) { + logger := Logger{ + Writer: &AsyncWriter{ + ChannelSize: 4096, + Writer: &FileWriter{ + Filename: "async_file_test.log", + }, + }, + } + defer logger.Writer.(io.Closer).Close() + + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(b *testing.PB) { + for b.Next() { + logger.Info().Msg("hello file writer") + } + }) +} + +func BenchmarkAsyncFileWriterWriteV(b *testing.B) { logger := Logger{ Writer: &AsyncWriter{ - ChannelSize: 100, - Writer: IOWriter{io.Discard}, + ChannelSize: 4096, + WritevEnabled: true, + Writer: &FileWriter{ + Filename: "async_file_test2.log", + }, }, } - b.SetParallelism(1000) + defer logger.Writer.(io.Closer).Close() + b.ReportAllocs() b.ResetTimer() b.RunParallel(func(b *testing.PB) { for b.Next() { - logger.Info().Msg("hello async writer") + logger.Info().Msg("hello file writer") } }) }