From 535076ad512ae0cccbc90dcfe4ad45f617f20f74 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Thu, 24 Jun 2021 13:32:09 -0700 Subject: [PATCH] Add a benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a benchmark that measures the performance of the writer when writes are split across multiple lines, as well as when it's one log per write. ``` name time/op Writer/single-4 652ns ± 4% Writer/splits-4 736ns ±11% name alloc/op Writer/single-4 16.0B ± 0% Writer/splits-4 16.0B ± 0% name allocs/op Writer/single-4 2.00 ± 0% Writer/splits-4 2.00 ± 0% ``` --- zapio/writer_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/zapio/writer_test.go b/zapio/writer_test.go index e731f8ca3..d1994ba7a 100644 --- a/zapio/writer_test.go +++ b/zapio/writer_test.go @@ -22,6 +22,7 @@ package zapio import ( "io" + "io/ioutil" "testing" "github.com/stretchr/testify/assert" @@ -156,3 +157,49 @@ func TestWriter(t *testing.T) { }) } } + +func BenchmarkWriter(b *testing.B) { + tests := []struct { + name string + writes [][]byte + }{ + { + name: "single", + writes: [][]byte{ + []byte("foobar\n"), + []byte("bazqux\n"), + }, + }, + { + name: "splits", + writes: [][]byte{ + []byte("foo"), + []byte("bar\nbaz"), + []byte("qux\n"), + }, + }, + } + + writer := Writer{ + Log: zap.New( + zapcore.NewCore( + zapcore.NewJSONEncoder(zapcore.EncoderConfig{MessageKey: "m"}), + zapcore.AddSync(ioutil.Discard), + zapcore.DebugLevel, + ), + ), + Level: zapcore.DebugLevel, + } + + for _, tt := range tests { + b.Run(tt.name, func(b *testing.B) { + b.ResetTimer() + + for i := 0; i < b.N; i++ { + for _, bs := range tt.writes { + writer.Write(bs) + } + } + }) + } +}