Skip to content

Commit

Permalink
Add a benchmark
Browse files Browse the repository at this point in the history
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%
```
  • Loading branch information
abhinav committed Jun 24, 2021
1 parent 0072bc4 commit 535076a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions zapio/writer_test.go
Expand Up @@ -22,6 +22,7 @@ package zapio

import (
"io"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
}
}
})
}
}

0 comments on commit 535076a

Please sign in to comment.